Nginx转发(代理)http请求
nginx可以转发所有http/https的请求和响应。在配置文件配置如下代码即可:
server {
listen 8020;
server_name test;
location / {
proxy_buffer_size 640k;
proxy_buffers 32 320k;
proxy_busy_buffers_size 1280k;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
add_header backendIP $upstream_addr;
proxy_cache_bypass $http_upgrade;
client_max_body_size 20m;
proxy_pass http://172.12.13.14:1500/;#http服务地址,复杂需要可配置upstream
}
}
配置完毕后,如上所示,则可以直接访问http://127.0.0.1:8020
等同于访问http://172.12.13.14:1500/
转发的时候如果不想被目标地址知道我们真实的ip可去除以下配置项:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header backendIP $upstream_addr;