search
HomeOperation and MaintenanceNginxnginx 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

nginx proxy_pass反向代理配置实例分析

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

nginx proxy_pass反向代理配置实例分析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/

nginx proxy_pass反向代理配置实例分析
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/

nginx proxy_pass反向代理配置实例分析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!

nginx proxy_pass反向代理配置实例分析
-------------------------------- -------------------------------------------------- ---

The above four methods all add "/" after the matching path. Let's talk about the situation without "/" after the path:


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

nginx proxy_pass反向代理配置实例分析

##2) In the second case, the url after proxy_pass is not followed by "/" nginx proxy_pass反向代理配置实例分析

[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 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 nginx proxy_pass反向代理配置实例分析

[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 "/"nginx proxy_pass反向代理配置实例分析

[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/nginx proxy_pass反向代理配置实例分析

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX: The Versatile Tool for Modern Web ApplicationsNGINX: The Versatile Tool for Modern Web ApplicationsApr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

Nginx SSL/TLS Configuration: Securing Your Website with HTTPSNginx SSL/TLS Configuration: Securing Your Website with HTTPSApr 10, 2025 am 09:38 AM

To ensure website security through Nginx, the following steps are required: 1. Create a basic configuration, specify the SSL certificate and private key; 2. Optimize the configuration, enable HTTP/2 and OCSPStapling; 3. Debug common errors, such as certificate path and encryption suite issues; 4. Application performance optimization suggestions, such as using Let'sEncrypt and session multiplexing.

Nginx Interview Questions: Ace Your DevOps/System Admin InterviewNginx Interview Questions: Ace Your DevOps/System Admin InterviewApr 09, 2025 am 12:14 AM

Nginx is a high-performance HTTP and reverse proxy server that is good at handling high concurrent connections. 1) Basic configuration: listen to the port and provide static file services. 2) Advanced configuration: implement reverse proxy and load balancing. 3) Debugging skills: Check the error log and test the configuration file. 4) Performance optimization: Enable Gzip compression and adjust cache policies.

Nginx Caching Techniques: Improving Website PerformanceNginx Caching Techniques: Improving Website PerformanceApr 08, 2025 am 12:18 AM

Nginx cache can significantly improve website performance through the following steps: 1) Define the cache area and set the cache path; 2) Configure the cache validity period; 3) Set different cache policies according to different content; 4) Optimize cache storage and load balancing; 5) Monitor and debug cache effects. Through these methods, Nginx cache can reduce back-end server pressure, improve response speed and user experience.

Nginx with Docker: Deploying and Scaling Containerized ApplicationsNginx with Docker: Deploying and Scaling Containerized ApplicationsApr 07, 2025 am 12:08 AM

Using DockerCompose can simplify the deployment and management of Nginx, and scaling through DockerSwarm or Kubernetes is a common practice. 1) Use DockerCompose to define and run Nginx containers, 2) implement cluster management and automatic scaling through DockerSwarm or Kubernetes.

Advanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyAdvanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyApr 06, 2025 am 12:05 AM

The advanced configuration of Nginx can be implemented through server blocks and reverse proxy: 1. Server blocks allow multiple websites to be run in one instance, each block is configured independently. 2. The reverse proxy forwards the request to the backend server to realize load balancing and cache acceleration.

Nginx Performance Tuning: Optimizing for Speed and Low LatencyNginx Performance Tuning: Optimizing for Speed and Low LatencyApr 05, 2025 am 12:08 AM

Nginx performance tuning can be achieved by adjusting the number of worker processes, connection pool size, enabling Gzip compression and HTTP/2 protocols, and using cache and load balancing. 1. Adjust the number of worker processes and connection pool size: worker_processesauto; events{worker_connections1024;}. 2. Enable Gzip compression and HTTP/2 protocol: http{gzipon;server{listen443sslhttp2;}}. 3. Use cache optimization: http{proxy_cache_path/path/to/cachelevels=1:2k

Nginx Security Hardening: Protecting Your Web Server From AttacksNginx Security Hardening: Protecting Your Web Server From AttacksApr 04, 2025 am 12:06 AM

Nginx security enhancement can be achieved through the following steps: 1) Ensure all traffic is transmitted through HTTPS, 2) Configure HTTP headers to enhance communication security, 3) Set up SSL/TLS encrypted data transmission, 4) Implement access control and rate limiting to prevent malicious traffic, 5) Use the ngx_http_secure_link_module module to prevent SQL injection attacks. These measures can effectively improve the security of Nginx servers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)