1. 对外开放本地封闭Server
本地server无法对外开放,nginx做反向代理,对外开发,使得外部可以访问封闭服务。
upstream npm { server 127.0.0.1:4873; keepalive 64; } server { listen 4898; #server_name 127.0.0.1; #charset koi8-r; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_pass http://npm; } }
2. 本地80端口被nginx占用,使用nginx进行端口转发至9000,外部以80端口访问有效
server{ listen 80; server_name www.blog.com; location / { proxy_pass http://localhost:9000; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$ { proxy_pass http://localhost:9000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log /www/wwwlogs/ti3.cc.log; }
3. nginx在80端口转发至多个本地端口
http { #gzip on; #添加upstream映射9080端口 upstream api{ server 127.0.0.1:9080; } #添加upstream映射8080端口 upstream tomcat{ server 127.0.0.1:8080; } server { listen 80; server_name localhost; #添加location匹配路由规则:/api location /api { #代理使用上面upstream定义名称为api的端口映射方式; #注意:最后一定要加斜杆,否则不能访问到启动在该端口项目,只能转发到端口,但端口后的路由匹配均不生效; #错误格式proxy_pass http://api; #正确格式proxy_pass http://api/; proxy_pass http://api/; } location / { root html; index index.html index.htm; #一般非特殊请求,转发到tomcat所在的端口 proxy_pass http://tomcat/; } } }
4. 同一个主域名,不同子域名 访问不同的服务
nginx在客户端本地,代理客户端向外访问,可以进行实现客户端,域名转发等功能
server { listen 80; server_name a.test.com; location / { proxy_pass http://127.0.0.1:8808; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name b.test.com; location / { proxy_pass http://192.168.1.166:6281; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
参考:https://www.cnblogs.com/acelance/p/10396821.html