• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
秋天的童话
梦想从此启航
博客园    首页    新随笔    联系   管理    订阅  订阅

nginx之"/"结尾

一、配置127.0.0.1:8081的tomcat下的文件:

1、ROOT/a.html : this is ROOT page

2、ROOT/testa.html : nihao

3、ROOT/index.html : this is ROOT index page

4、test/a.html  :  this is test page

5、test/index.html  :  this is test index page

 

二、nginx 的server_name配置如下:

1、proxy_pass的URI不带路径:

server {

        listen 80;

        server_name www.test.com;

        location /nihao {            

            proxy_pass http://127.0.0.1:8081;

            index  index.html;

        } 

                #location 后无/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/nihao/a.html

                #curl http://www.test.com/nihao/a.html    因hello目录不存在

             #The requested resource is not available

 

        location /test {

            proxy_pass http://127.0.0.1:8081;

            index  index.html;

        }

                #location 后无/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/test/a.html

              #curl http://www.test.com/test/a.html

              #this is test page

 

        location /hello/ {

            proxy_pass http://127.0.0.1:8081;

            index  index.html;

        }

                #location 后有/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/hello/a.html

                #curl http://www.test.com/hello/     因hello目录不存在

             #The requested resource is not available

        location /test/ {

            proxy_pass http://127.0.0.1:8081;

            index  index.html;

        }

               #location 后有/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/test/a.html

               #curl http://www.test.com/test/

            #this is test index page

}

server {

        listen 80;

        server_name www.test1.com;

        location /nihao {

            proxy_pass http://127.0.0.1:8081/;

            index  index.html;

        }

              #location 后无/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/a.html

              #curl http://www.test1.com/nihao/a.html

            #this is ROOT page

 

        location /test {

            proxy_pass http://127.0.0.1:8081/;

            index  index.html;

        }

            #location 后无/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/a.html   

             #curl http://www.test1.com/test/a.html

           #this is ROOT page

 

        location /hello/ {

            proxy_pass http://127.0.0.1:8081/;

            index  index.html;

        }

            #location 后有/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/index.html

            #curl http://www.test1.com/hello/

          #this is ROOT index page

 

        location /test/ {

            proxy_pass http://127.0.0.1:8081/;

            index  index.html;

        }

            #location 后有/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/index.html

           #curl http://www.test1.com/test/

         #this is ROOT index page

}

 

2、proxy_pass的URI带路径:

server {

        listen 80;

        server_name www.test.com;

        location /nihao {

            proxy_pass http://127.0.0.1:8081/test;

            index  index.html;

        }

                #location 后无/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/test/a.html

             #curl http://www.test.com/nihao/a.html

             #this is test page

             #curl http://www.test.com/nihao

             #this is test index page

 

        location /test {

            proxy_pass http://127.0.0.1:8081/test;

            index  index.html;

        }

               #location 后无/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/test//

             #curl http://www.test.com/test  重定向为:http://www.test.com/test//

             #指向此网址的请求无限循环重定向

 

        location /hello/ {

            proxy_pass http://127.0.0.1:8081/test;

            index  index.html;

        }

               #location 后有/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/testa.html

             #curl http://www.test.com/hello/a.html

             #nihao

             #curl http://www.test.com/hello/   重定向为:http://www.test.com/hello//

             #指向此网址的请求无限循环重定向

 

        location /test/ {

            proxy_pass http://127.0.0.1:8081/test;

            index  index.html;

        }

               #location 后有/   proxy_pass 后无/  代理到 http://127.0.0.1:8081/testa.html

             #curl http://www.test.com/test/a.html

             #nihao

             #curl http://www.test.com/test/  重定向为:http://www.test.com/test//

             #指向此网址的请求无限循环重定向

}

server {

        listen 80;

        server_name www.test1.com;

        location /nihao {

            proxy_pass http://127.0.0.1:8081/test/;

            index  index.html;

        }

               #location 后无/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/test/a.html

             #curl http://www.test1.com/nihao

             #this is test index page

             #curl http://www.test1.com/nihao/a.html

             #this is test page

 

        location /test {

            proxy_pass http://127.0.0.1:8081/test/;

            index  index.html;

        }

               #location 后无/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/test/index.html

             #curl http://www.test1.com/test

             #this is test index page

 

        location /hello/ {

            proxy_pass http://127.0.0.1:8081/test/;

            index  index.html;

        }

               #location 后有/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/test/a.html

             #curl http://www.test1.com/hello

             #this is test index page

             #curl http://www.test1.com/hello/a.html

             #this is test page

        location /test/ {

            proxy_pass http://127.0.0.1:8081/test/;

            index  index.html;

        }

                #location 后有/   proxy_pass 后有/  代理到 http://127.0.0.1:8081/test/a.html

              #curl http://www.test1.com/test/a.html

              #this is test page

}

 

三、总结:

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

proxy_pass的URI带路径中如果location的不带/,最好proxy_pass也不带;

做人一定要靠自己
posted @ 2019-10-24 10:01  凯-子  阅读(1910)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3