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
Note that in this case, you cannot directly access http://192.168.1.23/proxy/, which will be followed later Even the default index.html file must keep up, otherwise the access will fail!
-------------------------------- -------------------------------------------------- ---
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.service
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]#
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.
