nginx之root alias proxy_pass测试

本文详细介绍了Nginx配置文件中的root和alias指令的区别,并通过实例展示了proxy_pass在代理转发时URL带与不带/的不同效果。root指令根据请求的URI与指定目录结合返回文件,而alias则直接映射到指定目录。proxy_pass在URL带/时,视为绝对路径转发,不带/时,将请求路径与代理地址结合转发。


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

参考链接:

  • https://blog.youkuaiyun.com/wyl9527/article/details/89671506
  • https://blog.youkuaiyun.com/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 后面接的,带不带/区别不大,为规范,建议带 /
#load_module /usr/share/nginx/modules/ngx_stream_module.so; #user appyw; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; proxy_hide_header X-Powered-By; 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/host.access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; client_max_body_size 10m; #gzip_static on; server { server_tokens off; autoindex off; allow 127.0.0.1; allow 172.23.16.0/24; allow 172.23.68.219; allow 172.23.228.56; allow 218.76.15.23; allow ::1; deny all; listen 18081; listen 443 ssl; server_name localhost; if ($request_method !~ ^(GET|HEAD|POST)$) { return 444; } ssl_certificate ../jason.crt; ssl_certificate_key ../jason-np.key; ssl_session_timeout 5m; ssl_prefer_server_ciphers on; #charset koi8-r; #access_log logs/host.access.log main; location / { root /home/appyw/app/car; index index.html index.htm; try_files $uri $uri/ /index.html; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location /dev-api/ { proxy_pass http://172.23.228.56:18081/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /netty/ { proxy_pass http://127.0.0.1:18099/; } location /document/ { proxy_pass http://hn-cwhdfwpt-test.oss-cn-changsha-wwy-d01-a.opswan.sgmc.sgcc.com.cn/; #proxy_set_header Host $host; #proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /v2g-platform-web { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; alias /home/appyw/app/ls/v2g-platform-web; index index.html; try_files $uri $uri/ /v2g-platform-web/index.html; } location /v2g-charging-web { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; alias /home/appyw/app/ls/v2g-charging-web; index index.html; try_files $uri $uri/ /v2g-charging-web/index.html; } location /v2g-tjtf-web { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; alias /home/appyw/app/ls/v2g-tjtf-web; index index.html; try_files $uri $uri/ /v2g-tjtf-web/index.html; } location /vehicle-grid-h5 { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; alias /home/appyw/app/ls/vehicle-grid-h5; index index.html; try_files $uri $uri/ /vehicle-grid-h5/index.html; } location /market/api/ { proxy_pass http://172.23.228.56:18081/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /vehicle-grid-admin/ { proxy_pass http://172.23.228.56:18081/vehicle-grid-admin/vehicle-grid-admin/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /vehicle-grid-mobile/ { proxy_pass http://172.23.228.56:18081/vehicle-grid-mobile/vehicle-grid-mobile/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /vehicle-charge-server/ { proxy_pass http://172.23.228.56:18081/charge-server/charge-server/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /vehicle-charging-admin/ { proxy_pass http://172.23.228.56:18081/vehicle-charging-admin/charging-admin/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /vehicle-grid-system/ { proxy_pass http://172.23.228.56:34509/vehicle-grid-system/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /twoBatterySwapping/ { proxy_pass http://172.23.228.56:18081/twoBatterySwapping/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /travel/ { proxy_pass http://172.23.228.56:18081/travel/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /vehicle-grid-client-interconnect/ { proxy_pass http://172.23.228.56:34510/charging-client-interconnect/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /iot/ { proxy_pass http://172.23.228.56:34530; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /newiot/ { proxy_pass http://172.23.228.56:34530; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /xxl-job-admin/ { proxy_pass http://172.23.228.56:18892/xxl-job-admin/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #upstream dataide{ # server 172.23.228.33:34540; # check interval=5000 rise=2 fall=3 timeout=3000 type=http; # check_http_send "HEAD /dataide-server HTTP/1.0\r\n\r\n"; # check_http_expect_alive http_2xx http_3xx; # } location /dataide-server/ { #root html; index index.html index.htm; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://172.23.228.33:34540; } location ^~/dataide { alias /home/appyw/app/ls/ide; index index.html; try_files $uri $uri/ /dataide/index.html; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 比如我的这个配置文件,我想把iot,newiot,xxl-job都导到另一个18085端口上,该怎么做
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值