- 最常用的语法 proxy_pass
Syntax: proxy_pass URL;Default: --Context:location.if in location,limit_exception
-
反向代理
例:/etc/nginx/conf.d/default.conf 反向代理(代理服务端):80/test_proxy.html -> 8080/test_proxy.htmlserver { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/nginx/test_proxy.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~ /test_proxy.html$ { proxy_pass http://127.0.0.1:8080; }... ...}server { listen 8080; server_name localhost; #charset koi8-r; access_log /var/log/nginx/server.access.log main; location / { root /opt/app/code2; index index.html index.htm; }}
- 正向代理
例:正向代理(代理客户端):server { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/nginx/test_proxy.access.log main; resolver 8.8.8.8; location / { proxy_pass http://$http_host$request_uri; }... ...}
-
proxy的其他语法扩展
proxy_buffering 缓冲区Syntax: proxy_buffering on | off;Default: proxy_buffering on;Context:http,server,location扩展: proxy_buffer_size 、proxy_buffers 、proxy_busy_buffers_size 、proxy_max_temp_file_sizeproxy_redirect 跳转重定向Syntax: proxy_redirect default; proxy_redirect off;proxy_redirect redirect replacement;Default: proxy_redirect default;Context:http,server,locationproxy_set_header 头信息Syntax:proxy_set_header field value;Default:proxy_set_header Host $proxy_host; proxy_set_header Connection close;Context:http,server,location扩展:proxy_hide_header 、proxy_set_bodyproxy_connect_timeout 超时;Syntax:proxy_connect_timeout time;Default:proxy_connect_timeout 60s;Context:http,server,location扩展:proxy_read_timeout 、proxy_send_timeout
例:location / { proxy_pass http://127.0.0.1:8080; proxy_redirect default; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_connect_timeout 30; proxy_read_timeout 60; proxy_send_timeout 60; proxy_buffer_size 32k; proxy_buffering on; proxy_buffers 4 128k; proxy_busy_buffers_size 256k; proxy_max_temp_file_size 256k;}