Nginx proxy_pass详解

假设server_name为www.test.com
当请求URL为http://www.test.com/zabbix/index.html时,以下示例的访问结果是
示例1:
location /zabbix/ {
      proxy_pass http://192.168.1.10;
}
结果1:http://192.168.1.10/zabbix/index.html
web服务器访问日志:
"GET /zabbix/index.html HTTP/1.0" 200 509

示例2:
location /zabbix/ {
      proxy_pass http://192.168.1.10/;
}
结果2:http://192.168.1.10/index.html
web服务器日志:
"GET /index.html HTTP/1.0" 404 555

示例3:
location /zabbix/ {
      proxy_pass http://192.168.1.10/linux;
}
结果3:http://192.168.1.10/linuxindex.html
web服务器日志:
"GET /linuxindex.html HTTP/1.0" 404 555

示例4:
location /zabbix/ {
      proxy_pass http://192.168.1.10/linux/;
}
结果4:http://192.168.1.10/linux/index.html
web服务器日志:
"GET /linux/index.html HTTP/1.0" 404 555

二、代理服务器配置示例:

server {
    listen  443  ssl;
    server_name  www.test.com bbs.test.com;

    charset utf-8;
    #access_log  /var/log/nginx/host.access.log  main;

    ssl_certificate ./crt/chain.crt;
    ssl_certificate_key ./crt/key.key;

#    location /zabbix/ {
#        proxy_pass http://192.168.1.10;
#    }
#    location /zabbix/ {
#        proxy_pass http://192.168.1.10/;
#    }
#    location /zabbix/ {
#        proxy_pass http://192.168.1.10/linux;
#    }
    location /zabbix/ {
        proxy_pass http://192.168.1.10/linux/;
    }
    #访问域名bbs.test.com/bbs
    location /bbs {
         root   /usr/share/nginx/html/;  #需要在html目录下创建bbs目录
         index  index.html index.htm;
    }
}

#访问域名http://www.test.com/zabbix
server {
    listen       80;
    server_name  www.test.com;
    location / {
        rewrite /(.*) https://www.test.com/$1 break;
    }
}

#访问域名http://bbs.test.com
server {
    listen       80;
    server_name  bbs.test.com;
    location / {
        rewrite /(.*) https://bbs.test.com/bbs break;
    }
}

 

server {
    listen       192.168.1.10:80;
    server_name  localhost;

    #charset koi8-r;
    charset utf-8;

    access_log  logs/proxy.access.log  main;

    location / {
        proxy_pass http://10.47.39.8:8080;  # 后端服务器的IP地址,也就是web服务器的IP地址
        proxy_set_header Host $http_host;  # 表示重新定义转发给web服务器的请求头部,即在请求头部添加客户端真实地址
        proxy_set_header X-Real-IP $remote_addr; # 表示记录客户端真实IP地址
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 表示记录服务端反向代理服务器的ip地址

        proxy_connect_timeout 30;  # 表示反向代理服务器对web服务器发起TCP三次握手的连接超时时间
        proxy_send_timeout 60;     # 表示web服务器给反向代理服务器返回资源过程中的超时时间
        proxy_read_timeout 60;     # 表示TCP长连接的超时时间

        proxy_buffering on;        # 表示开启缓存功能
        proxy_buffer_size 32k;     # 表示存放响应头部的缓冲区
        proxy_buffers 4 128k;      # 表示内容缓冲区大小
        proxy_busy_buffers_size 256k;   # 表示用于向客户端发送资源的缓冲区,反向代理服务器会划分出一部分用于发送资源的缓冲区
        proxy_max_temp_file_size 256k;  # 表示用于存储请求头部的缓冲区,反向代理服务器会将超大的请求头部在缓冲区内存储成文件。
        }
}

结论:
      如果proxy_pass配置值包含"/"就去掉匹配路径部分
      如果proxy_pass配置值不包含"/"就保留匹配路径部分
      建议所有的proxy_pass后的url都以“/”结尾(proxy_pass http://192.168.1.10/linux/;)

参考链接:
      https://www.cnblogs.com/goloving/p/13663843.html
      https://cloud.tencent.com/developer/article/1557504       # proxy_set_header问题梳理

posted @ 2020-07-15 19:30  風£飛  阅读(548)  评论(0)    收藏  举报