Home > Article > Operation and Maintenance > nginx proxy_pass reverse proxy configuration example analysis
The following is a small example:
centos7 system library does not have nginx rpm package by default, so we need to update the rpm dependency library first
1) Use yum to install nginx and you need to include the nginx library. Install the nginx library
[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.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } } [root@localhost conf.d]# cat /var/www/html/index.html this is page of test!!!!
4) Start nginx
[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service
5) Test access (103.110.186.23 is the external network ip of the 192.168.1.23 machine)
[root@localhost conf.d]# curl http://192.168.1.23 this is page of test!!!!
Look at the following situations: use http://192.168.1.23/proxy/index.html for access testing
In order to facilitate testing, first test on another computer Deploy an nginx with port 8090 on machine 192.168.1.5. The configuration is as follows:
[root@bastion-idc ~]# cat /usr/local/nginx/conf/vhosts/haha.conf server { listen 8090; server_name localhost; location / { root /var/www/html; index index.html; } } [root@bastion-idc ~]# cat /var/www/html/index.html this is 192.168.1.5 [root@bastion-idc ~]# /usr/local/nginx/sbin/nginx -s reload
Test access (103.110.186.5 is the external network IP of 192.168.1.5):
[root@bastion-idc ~]# curl http://192.168.1.5:8090 this is 192.168.1.5
192.168.1.23 serves as the nginx reverse proxy machine, and the nginx configuration is as follows:
1) The first case:
[root@localhost conf.d]# cat test.conf server { listen 80; server_name 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 proxy to http://192.168.1.5:8090/. The proxy directory matching p does not need to exist in the root directory /var/www/html
Note that if you access http://192.168.1.23/proxy in the terminal (that is, without "/" after it) , the access will fail! Because "/" is added after the url configured by proxy_pass "/"), and reverse to the result of http://103.110.186.5:8090
2) In the second case, do not add after the url of proxy_pass configuration "/"
[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/proxy <html> <head><title>301 moved permanently</title></head> <body bgcolor="white"> <center><h1>301 moved permanently</h1></center> <hr><center>nginx/1.10.3</center> </body> </html>
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 reverse proxy to http://192.168.1.5:8090/proxy/
3) The third case
[root@localhost conf.d]# cat test.conf server { listen 80; server_name 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 restart redirecting to /bin/systemctl restart nginx.service
If configured like this, access http://103.110.186.23/proxy and proxy to http://192.168.1.5:8090/ haha/
4) The fourth situation: Compared with the third configuration, the url does not add "/"
[root@localhost conf.d]# cat test.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } location /proxy/ { proxy_pass http://192.168.1.5:8090/haha/; } } [root@localhost conf.d]# service nginx restart redirecting to /bin/systemctl restart nginx.service [root@localhost conf.d]# curl http://192.168.1.23/proxy/ 192.168.1.5 haha-index.html
After the above configuration, access http ://192.168.1.23/proxy/index.html will be proxy to http://192.168.1.5:8090/hahaindex.html
Similarly, visit http://192.168.1.23/proxy/test.html It will be proxied to http://192.168.1.5:8090/hahatest.html[root@localhost conf.d]# cat test.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } location /proxy/ { proxy_pass http://192.168.1.5:8090/haha; } } [root@localhost conf.d]# service nginx restart redirecting to /bin/systemctl restart nginx.service [root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html 192.168.1.5 hahaindex.html
-------------------------------- -------------------------------------------------- ---
1) The first case, proxy_pass The url is followed by "/":
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html 192.168.1.5 hahaindex.html
##2) In the second case, the url after proxy_pass is not followed by "/"
[root@localhost conf.d]# cat test.conf server { listen 80; server_name 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 restart redirecting to /bin/systemctl restart nginx.serviceIf configured in this way, accessing http://103.110.186.23/proxy will automatically add "/" (that is, it becomes http://103.110.186.23/proxy/), and the proxy will be 192.168.1.5: 8090/proxy/
3) The third case
[root@localhost conf.d]# cat test.conf server { listen 80; server_name 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 restart redirecting 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, it becomes http://103.110.186.23/proxy/), proxy to http://192.168.1.5:8090/haha/
4) The fourth situation: Compared with the third configuration, the url does not add "/"
[root@localhost conf.d]# cat test.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } location /proxy { proxy_pass http://192.168.1.5:8090/haha/; } } [root@localhost conf.d]# service nginx restart redirecting to /bin/systemctl restart nginx.service
. If configured in this way, access http://103.110.186.23/proxy, and The third result is the same, it is also proxied to http://192.168.1.5:8090/haha/
The above is the detailed content of nginx proxy_pass reverse proxy configuration example analysis. For more information, please follow other related articles on the PHP Chinese website!