Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the difference of whether or not to add / after url in nginx proxy_pass reverse proxy configuration

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

One of the important functions of nginx as a web server is reverse proxy. Nginx reverse proxy instructions do not need to add additional modules, the default comes with proxy_pass instructions, only need to modify the configuration file to achieve reverse proxy.

In the daily deployment of web websites, nginx's proxy_pass reverse proxy is often used, and there is a configuration to be clear: when configuring proxy_pass, when the url behind is added /, which is equivalent to the absolute root path, then nginx will not proxy part of the matching path in location; if there is no /, it will also give the matching path part to the agent (this configuration can refer to this article).

Here is a small example to illustrate:

There is no nginx rpm package in the centos7 system library by default, so we need to update the rpm dependent library first.

1) to install nginx using yum, you need to include Nginx libraries and install Nginx libraries.

[root@localhost ~] # rpm-Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2) use the following command to install nginx

[root@localhost ~] # yum install nginx

3) nginx configuration

[root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # cat test.confserver {listen 80th serversionname localhost;location / {root / var/www/html;index index.html;}} [root@localhost conf.d] # cat / var/www/html/index.htmlthis is page of testmakers

4) start Nginx

[root@localhost ~] # service nginx start / / or use systemctl start nginx.service

5) Test access (103.110.186.23 is the public network ip of 192.168.1.23 machine)

[root@localhost conf.d] # curl http://192.168.1.23this is page of test!!!!

Take a look at the following situations: access testing with http://192.168.1.23/proxy/index.html respectively

To facilitate testing, deploy a 8090 port nginx on another machine, 192.168.1.5, with the following configuration:

[root@bastion-IDC ~] # cat / usr/local/nginx/conf/vhosts/.confserver {listen 8090 var/www/html/index.htmlthis is serverSecretname localhost;location / {root / var/www/html;index index.html;}} [root@bastion-IDC ~] # cat / var/www/html/index.htmlthis is 192.168.1.5 [root@bastion-IDC ~] # / usr/local/nginx/sbin/nginx-s reload

Test access (103.110.186.5 is the public network ip of 192.168.1.5):

[root@bastion-IDC ~] # curl http://192.168.1.5:8090this is 192.168.1.5

192.168.1.23 as a nginx reverse proxy machine, nginx is configured as follows:

1) the first case:

[root@localhost conf.d] # cat test.confserver {listen 80th serverroomname localhost;location / {root / var/www/html;index index.html;} location / proxy/ {proxy_pass http://192.168.1.5:8090/;}}

In this way, access to http://192.168.1.23/proxy/ will be proxied to http://192.168.1.5:8090/. The p-matching proxy directory does not need to exist in the root directory / var/www/html

Note that if the terminal accesses http://192.168.1.23/proxy (that is, it is not followed by a "/"), the access will fail! Because the url configured by proxy_pass is followed by "/"

[root@localhost conf.d] # curl http://192.168.1.23/proxy/this is 192.168.1.5 [root@localhost conf.d] # curl http://192.168.1.23/proxy301 Moved Permanently301 Moved Permanentlynginx/1.10.3

When the page visits http://103.110.186.23/proxy, it will automatically add "/" (similarly, because the url configured by proxy_pass is followed by "/"), and replace it with the result of http://103.110.186.5:8090.

2) in the second case, the url configured by proxy_pass is not followed by "/"

[root@localhost conf.d] # cat test.confserver {listen 80th serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy/ {proxy_pass http://192.168.1.5:8090;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service

Then accessing http://192.168.1.23/proxy or http://192.168.1.23/proxy/ will fail!

After this configuration, access to http://192.168.1.23/proxy/ will be proxied to http://192.168.1.5:8090/proxy/ in reverse.

3) the third case

[root@localhost conf.d] # cat test.confserver {listen 80th serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy/ {proxy_pass http://192.168.1.5:8090//;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service [root@localhost conf.d] # curl http://192.168.1.23/proxy/192.168.1.5 -index.html

In this configuration, access the http://103.110.186.23/proxy proxy to http://192.168.1.5:8090//

4) the fourth case: do not add "/" to the url of the third configuration

[root@localhost conf.d] # cat test.confserver {listen 80th serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy/ {proxy_pass http://192.168.1.5:8090/;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service [root@localhost conf.d] # curl http://192.168.1.23/proxy/index.html192.168.1.5 index.html

After the above configuration, access to http://192.168.1.23/proxy/index.html will be proxied to http://192.168.1.5:8090/index.html

By the same token, accessing http://192.168.1.23/proxy/test.html will be proxied to http://192.168.1.5:8090/test.html

[root@localhost conf.d] # curl http://192.168.1.23/proxy/index.html192.168.1.5 index.html

Note that in this case, you cannot access http://192.168.1.23/proxy/ directly, and even the default index.html file must be kept up with later, otherwise the access will fail!

-

The above four methods all add "/" after the matching path path. The following is the case where there is no "/" after the path path:

1) in the first case, url is followed by proxy_pass with "/":

[root@localhost conf.d] # cat test.confserver {listen 80th serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy {proxy_pass http://192.168.1.5:8090/;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service

2) in the second case, the url after proxy_pass does not take "/"

[root@localhost conf.d] # cat test.confserver {listen 80 / serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy {proxy_pass http://192.168.1.5:8090;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service [root@localhost conf.d] #

If configured in this way, accessing http://103.110.186.23/proxy will automatically add "/" (that is, become http://103.110.186.23/proxy/) and proxy to 192.168.1.5:8090/proxy/

3) the third case

[root@localhost conf.d] # cat test.confserver {listen 80th serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy {proxy_pass http://192.168.1.5:8090//;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service

If configured in this way, accessing http://103.110.186.23/proxy will automatically add "/" (that is, become http://103.110.186.23/proxy/) and proxy to http://192.168.1.5:8090//

4) the fourth case: do not add "/" to the url of the third configuration

[root@localhost conf.d] # cat test.confserver {listen 80th serverSecretname localhost;location / {root / var/www/html;index index.html;} location / proxy {proxy_pass http://192.168.1.5:8090/;}}[root@localhost conf.d] # service nginx restartRedirecting to / bin/systemctl restart nginx.service

In this configuration, access to http://103.110.186.23/proxy, like the third result, is also proxied to http://192.168.1.5:8090//

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report