nginx之root alias proxy_pass测试
[root@k8s-node02 conf.d]# cat test.confserver {listen80;listen[::]:80;server_name www.gudong.comroot/usr/share/nginx/html;location / {index index.html;}location /test1 {
·
文章目录
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 后面接的,带不带/区别不大,为规范,建议带 /
更多推荐
已为社区贡献3条内容
所有评论(0)