1. 反向代理
nginx代理一個本地的伺服器,nginx的服務對外開放,本地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. 正向代理
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; } }