Home Operation and Maintenance Nginx nginx proxy_pass reverse proxy configuration example analysis

nginx proxy_pass reverse proxy configuration example analysis

May 13, 2023 pm 11:19 PM
nginx proxy_pass

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
Copy after login

2) Use the following command to install nginx

[root@localhost ~]# yum install nginx
Copy after login

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!!!!
Copy after login

4) Start nginx

[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service
Copy after login

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!!!!
Copy after login

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
Copy after login

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
Copy after login

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/;
}
}
Copy after login

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>301 moved permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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]#
Copy after login

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
Copy after login

. 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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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 cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

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.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

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

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

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.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

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 How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

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 solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

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.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

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.

See all articles