nginx之root alias proxy_pass测试

3 篇文章 0 订阅


proxy_pass官网:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

参考链接:

  • https://blog.csdn.net/wyl9527/article/details/89671506
  • https://blog.csdn.net/u010433704/article/details/99945557

1,配置文件

[root@k8s-node02 conf.d]# cat test.conf 
#######第一段
server {
        listen       80;
        root   /usr/share/nginx/html;        
#
location / {
           index index.html;
        }
#
location /test1 {
          root /usr/share/nginx/html;
          index test1.html;
         }

location /test2/ {
          root /usr/share/nginx/html;
          index test2.html;
         }

#
location /alias {
           alias /usr/share/nginx/html/alias/a/;
           index alias.html;
        }
#

location /proxyf/ {
           proxy_pass http://192.168.128.221/;
        }
location /proxyg/ {
           proxy_pass http://192.168.128.221;
        }

#
location /proxya {
           proxy_pass http://192.168.128.221:8001/;
        }
location /proxyb {
           proxy_pass http://192.168.128.221:8001;
        }

location /proxyc/ {
           proxy_pass http://192.168.128.221:8001/passc/;
        }
location /proxyd/ {
           proxy_pass http://192.168.128.221:8001/passd;
        }

location /proxye/ {
           proxy_pass http://192.168.128.221:8001;
        }
    }

#######第二段
server {
        listen  8001;
        root   /usr/share/nginx/8001/;        
location / {
           index index.html;
        }
    }

2,静态文件

2.1 目录结构

[root@k8s-node02 nginx]# pwd
/usr/share/nginx

[root@k8s-node02 nginx]# tree html
html
├── alias
│   └── a
│       └── alias.html
├── alias.html
├── index.html
├── proxyg
│   └── index.html
├── test1
│   └── test1.html
├── test1.html
├── test2
│   └── test2.html
└── test2.html

5 directories, 8 files
[root@k8s-node02 nginx]# tree 8001/
8001/
├── index.html
├── passc
│   └── index.html
├── passdindex.html
├── proxyb
│   └── index.html
└── proxye
    └── index.html

2.2 具体内容

[root@k8s-node02 nginx]# cat html/alias/a/alias.html 
alias/a/alias.html
[root@k8s-node02 nginx]# cat html/alias.html 
alias.html
[root@k8s-node02 nginx]# cat html/index.html 
index
[root@k8s-node02 nginx]# cat html/proxyg/index.html 
proxyg/index.html
[root@k8s-node02 nginx]# cat html/test1/test1.html 
test1/test1.html
[root@k8s-node02 nginx]# cat html/test1.html 
test1.html
[root@k8s-node02 nginx]# cat html/test2/test2.html 
test2/test2.html
[root@k8s-node02 nginx]# cat html/test2.html 
test2.html
[root@k8s-node02 nginx]# cat 8001/index.html 
8001.html
[root@k8s-node02 nginx]# cat 8001/passc/index.html 
80/passc/passc.html
[root@k8s-node02 nginx]# cat 8001/passdindex.html 
80/passdindex.html
[root@k8s-node02 nginx]# cat 8001/proxyb/index.html 
8001/proxyb/proxyb.html
[root@k8s-node02 nginx]# cat 8001/proxye/index.html 
80/proxye/proxye.html

3,测试

3.1 root和alias的区别

[root@k8s-node02 ~]# curl 192.168.128.221
index
[root@k8s-node02 ~]# curl 192.168.128.221:8001
8001.html
[root@k8s-node02 ~]# curl 192.168.128.221:80/test1/
test1/test1.html
[root@k8s-node02 ~]# curl 192.168.128.221:80/alias/
alias/a/alias.html

结论:

  • 当我们访问 http://192.168.128.221:80/test1/,实际访问的是/usr/share/nginx/html/test1/test1.html
  • 当我们访问 http://192.168.128.221:80/alias/,实际访问的是/usr/share/nginx/html/alias/a/alias.html

3.2 proxy_pass 中的url带不带/的区别

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

3.2.1 带 /

location /proxya {
           proxy_pass http://192.168.128.221:8001/;
        }
[root@k8s-node02 ~]# curl 192.168.128.221:80/proxya/index.html
8001.html

结论:

  • 会被代理到 http://192.168.128.221:8001/index.html 这个url

3.2.2 不带 /

location /proxyb {
           proxy_pass http://192.168.128.221:8001;
        }
[root@k8s-node02 ~]# curl 192.168.128.221:80/proxyb/index.html
8001/proxyb/proxyb.html

结论:

  • 会被代理到 http://192.168.128.221:8001/proxyb/index.html 这个url

3.2.3 带有 /passc/

location /proxyc/ {
           proxy_pass http://192.168.128.221:8001/passc/;
        }
[root@k8s-node02 ~]# curl 192.168.128.221:80/proxyc/index.html
80/passc/passc.html

结论:

  • 会被代理到 http://192.168.128.221:8001/passc/index.html 这个url

3.2.4 带有 /passd

location /proxyd/ {
           proxy_pass http://192.168.128.221:8001/passd;
        }
[root@k8s-node02 ~]# curl 192.168.128.221:80/proxyd/index.html
80/passdindex.html

结论:

  • 会被代理到 http://192.168.128.221:8001/passdindex.html 这个url

补充:

location /proxye/ {
           proxy_pass http://192.168.128.221:8001;
        }
[root@k8s-node02 ~]# curl 192.168.128.221:80/proxye/index.html
80/proxye/proxye.html

结论:

  • location 后面接的,带不带/区别不大,为规范,建议带 /
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值