基础记录
worker_processes 1; events { #最大连接数 worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; access_log off; sendfile on; keepalive_timeout 600; #配置负载 upstream test { #weight 权重,越大分的越多,下面是两个应用服务器 server 192.168.1.1:9081 weight=1; server 192.168.1.2:9081 weight=1; } server { # nginx 监听端口 listen 80; server_name localhost; #关掉日志,不关日志文件会很大 access_log off; location / { #负载均衡 test 要和 upstream 相同 #proxy_pass http://test; proxy_pass http://localhost:8088/; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #以下是一些反向代理的配置,可选。 proxy_set_header Host $host:9080; #允许客户端请求的最大单文件字节数 client_max_body_size 500m; #缓冲区代理缓冲用户端请求的最大字节数, client_body_buffer_size 128k; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_connect_timeout 300; #后端服务器数据回传时间(代理发送超时) proxy_send_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时) proxy_read_timeout 600; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 4k; #proxy_buffers缓冲区,网页平均在32k以下的设置 proxy_buffers 4 32k; #高负荷下缓冲大小(proxy_buffers*2) proxy_busy_buffers_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 proxy_temp_file_write_size 64k; } #本地动静分离反向代理配置 #所有jsp的页面均交由weblogic或websphere处理 location ~ .(jsp|jspx|action)?$ { proxy_set_header Host $host:9080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://test; proxy_redirect off; } #所有静态文件由nginx直接读取不经过tomcat或resin location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; } location ~ .*.(js|css)?$ { expires 1h; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
使用Nginx配置多个域名
http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; #第一个域名 server { listen 80; server_name yaoyaovip.xyz; #第一个域名,需要域名供应商解析对应本服务器ip location / { root html; #映射的相对路径,也就是当前nginx根目录下的html文件 index index.html index.htm; #优先寻找命名页面。 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #第二个域名 server { listen 80; server_name test.yaoyaovip.xyz;#第二个域名,二级域名,需要域名供应商解析对应本服务器ip location / { root html/test; #映射的相对路径,也就是当前nginx根目录下的html里面的test文件夹 index index.html index.htm; #优先寻找命名页面。 } error_page 500 502 503 504 /50x.html; #当发生错误加载的页面 location = /50x.html { root html; #根据x来访问html文件夹里面对应的错误页面 } } }
Nginx显示目录列表
location / { charset utf-8; #解决中文乱码问题 autoindex on; #开启目录列表 root html; }
代理转发
# /是访问到根目录后进行代理转发请求 server { listen 80; server_name localhost; location / { #proxy_pass 需要转发的网址; proxy_pass http://note.youdao.com; } }
一个端口代理单个网址
server { listen 80; server_name localhost; location / { proxy_pass http://localhost:8080; } location /api { proxy_pass http://localhost:8081; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/api/buildTask location /api { proxy_pass http://localhost:8081/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost//buildTask location /api/ { proxy_pass http://localhost:8081; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/api/buildTask location /api/ { proxy_pass http://localhost:8081/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/buildTask location /api { proxy_pass http://localhost:8081/bar; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/bar/buildTask location /api { proxy_pass http://localhost:8081/bar/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/bar//buildTask location /api/ { proxy_pass http://localhost:8081/bar; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/barbuildTask location /api/ { proxy_pass http://localhost:8081/bar/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/bar/buildTask }
一个端口代理多网址
server { listen 80; server_name localhost; location / { proxy_pass http://localhost:8080; } location /api { proxy_pass http://localhost:8081; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/api/buildTask location /api { proxy_pass http://localhost:8081/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost//buildTask location /api/ { proxy_pass http://localhost:8081; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/api/buildTask location /api/ { proxy_pass http://localhost:8081/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/buildTask location /api { proxy_pass http://localhost:8081/bar; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/bar/buildTask location /api { proxy_pass http://localhost:8081/bar/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/bar//buildTask location /api/ { proxy_pass http://localhost:8081/bar; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/barbuildTask location /api/ { proxy_pass http://localhost:8081/bar/; } #访问地址:http://localhost/api/buildTask #实际地址:http://localhost/bar/buildTask }
设置文件上传大小限制
http{
#设置文件上传大小 20MB
client_max_body_size 20m
}
————————————————
版权声明:本文为CSDN博主「张童瑶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014641168/article/details/125114398
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统