nginx 正向代理配置
2022年5月31日大约 1 分钟约 323 字
正向代理服务
**注意:**使用http_proxy模块代理http协议,暂时不能支持HTTPS代理
配置语法
语法:proxy_pass URL;
该语法在location、if in location、limit_except下配置
URL写法:
http://localhost:8000/uri/
https://192.168.1.1:8000/uri/
http://unix:/tmp/backend.socket:/uri/;
代理某个页面:
vim /etc/nginx/conf.d/zx_proxy.conf
server {
listen 80;
server_name localhost jeson.t.imooc.io;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# http://jeson.t.imooc.io/test_proxy.html
location ~ /test_proxy.html$ {
proxy_pass http://127.0.0.1:8000; # 向该url请求
}
}
代理全部页面:
vim /etc/nginx/conf.d/zx_proxy.conf
server {
listen 80;
server_name localhost jeson.t.imooc.io;
resolver 8.8.8.8 # DNS服务器
location / {
proxy_pass http://$http_host$request_uri;
}
}
代理缓冲区
语法:proxy_buffering on | off;
默认:on
该语法在http、server、location下配置
相关模块:proxy_buffer_size、proxy_buffers、proxy_busy_buffers_size
跳转重定向
语法:proxy_rediredt off | redirect replacement;
该语法在http、server、location下配置
头信息
语法:proxy_set_header field value;
默认:proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
该语法在http、server、location下配置
相关模块:proxy_hide_header、proxy_set_body
超时
语法:proxy_connect_timeout time
默认:60s;
该语法在http、server、location下配置
相关模块:proxy_read_timeout、proxy_send_timeout
常用示例
vim /etc/nginx/conf.d/zx_proxy.conf
server {
listen 80;
server_name localhost jeson.t.imooc.io;
resolver 8.8.8.8 # DNS服务器
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect default;
proxy_set_header Host $http_host; # 添加Host头信息
proxy_set_header X-Real-IP $remote_addr; # 添加IP地址
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on; # 减少IO,尽可能等待
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
}