ichengzi / ichengzi.github.io

ichengzi's blogs and learn notes
https://ichengzi.github.io
1 stars 0 forks source link

nginx转发配置 #24

Open ichengzi opened 4 years ago

ichengzi commented 4 years ago

nginx 转发配置

server {
    listen       1024;
    #server_name  localhost; 正向代理不要设置server_name
    resolver 114.114.114.114 valid=600s; #指定DNS服务器IP地址
    proxy_connect_timeout 5s;
    proxy_read_timeout 150s; 
    proxy_send_timeout 150s; 
    #规则1, 正向代理
    location / {
        proxy_pass http://$host$request_uri;  #设定代理服务器的协议和地址
    }
    #规则2
    location /api/ {
        proxy_pass http://www.demo.com/api/;
    }
}

假设 server ip: 10.10.0.10 // 无代理时的请求,remote-address由dns解析得到 GET http://www.demo.com/api/product/list  HOST www.demo.com Remote-Address 112.80.248.76:80

// 请求时添加了proxy,会走到规则2【推荐】 GET http://www.demo.com/api/product/list  HOST www.demo.com Remote-Address 10.10.0.10:80

// 请求时直接替换了URL的域名, 会走到规则2【不推荐】 GET http://10.10.0.10/api/product/list  HOST 10.10.0.10 Remote-Address 10.10.0.10:80