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;
    }
}

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

原文地址:https://www.cnblogs.com/xwupiaomiao/p/13307483.html