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 id="nbsp-moved-nbsp-permanently">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.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!

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
