Nginx中proxy_pass 后有无/的区别

1、代理服务器配置文件proxy_pass带 /的情况

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
#     upstream backend {
#       server 10.0.0.50:8080;
#}

    server {
          listen       80;
          server_name  a.test.com;

        
    location /abc {
        proxy_pass http://10.0.0.50:80/;

       }
}
}

真实服务器配置

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name www.test.com aa.test.com;
      root /abc;

        location /abc {
       root html;
       index index.html index.htm;
}
}
}

创建目录

[root@master conf]# mkdir /abc
[root@master conf]# echo "ok,root" > /abc/index.html

访问测试

 说明此时并没有访问到/abc,也就是

http://10.0.0.50:80/ 中的 / 把/abc给替换掉了,简单来说,就是以proxy_pass指定的为准

2、修改代理服务器配置为不带 /

    server {
          listen       80;
          server_name  a.test.com;
           # if ( $host ~* "^a.test.com$" ) {
           # rewrite ^/(.*) /$1 permanent;
       # }    
        
    location /abc {
        proxy_pass http://10.0.0.50:80;
#        proxy_set_header X-Proxy-Host $proxy_host;
#        proxy_set_header Host $http_host;
       }
}
}

站点文件

[root@master html]# cat /app/nginx/html/abc/index.html 
abc

访问测试:

 匹配到 /abc的时候proxy_pass到了http://10.0.0.50:80,又匹配 了 /abc,所以根路径为html

当location 为 / 的时候,这两种配置无区别

原文地址:https://www.cnblogs.com/zh-dream/p/12834105.html