SoFunction
Updated on 2025-04-06

Solutions for conflict between Nginx and background application ports

1. Problem background

(I) The role of Nginx

Nginx is a high-performance HTTP and reverse proxy server, which is often used to provide static file service, load balancing, SSL encryption and other functions. It can also act as a reverse proxy server, forwarding requests to the backend application server.

(II) Port requirements for background applications

Background applications (such as Python Flask, Java Spring Boot, etc.) usually need to listen to a port to receive HTTP requests. For example, a Python Flask application might run on port 8000:

from flask import Flask
app = Flask(name)

@(‘/')
def hello():
return ‘Hello, World!'

if name == ‘main':
(port=8000)

At the same time, Nginx also needs to listen to a port to receive external requests and forward the requests to the background application. For example, Nginx's configuration file might contain the following:

server {
listen 8000;
server_name ;

}
location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

When both Nginx and background applications try to listen to the same port 8000, a port conflict will occur, causing Nginx to fail to start.

2. Methods to resolve port conflicts

(I) Modify the listening port of Nginx

This is the easiest and most straightforward way. You can modify Nginx's listening port from 8000 to other unoccupied ports (such as 8080), and then forward the request to port 8000 of the backend application through a reverse proxy.

1. Modify the Nginx configuration file

Edit Nginx's configuration file (such as /etc/nginx//) and modify the listening port from 8000 to 8080:

server {
listen 8080; # Modify to a new portserver_name ;

location / {
proxy_pass http://127.0.0.1:8000; # Port of background applicationproxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

2. Restart Nginx

After saving the configuration file, restart the Nginx service to make the configuration take effect:

systemctl restart nginx

3. Verify the configuration

Access through the browser: 8080. If you can access the background application normally, it means that the configuration is successful.

(II) Use different IP addresses to monitor

If the server has multiple network interfaces or IP addresses, Nginx and background applications can be allowed to listen to different IP addresses separately. For example, the server has two IP addresses 192.168.1.100 and 192.168.1.101, allowing Nginx to listen for port 8000 of 192.168.1.100, and the background application listens for port 8000 of 192.168.1.101.

4..Modify Nginx configuration file

Edit Nginx's configuration file and specify the 8000 port of Nginx listening for 192.168.1.100:

server {
listen 192.168.1.100:8000; # Specify IP address and portserver_name ;

location / {
proxy_pass http://192.168.1.101:8000; # IP address and port of background applicationproxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

5. Modify the background application configuration

Modify the configuration of the background application to listen to port 8000 of 192.168.1.101. Take Python Flask application as an example:

from flask import Flask
app = Flask(name)
@(‘/')
def hello():
return ‘Hello, World!'

if name == ‘main':
(host=‘192.168.1.101', port=8000)

6. Restart Nginx and background applications

After saving the configuration file, restart the Nginx service and background applications:

systemctl restart nginx

Restart the background application (select the appropriate command according to the actual situation)

7. Verify the configuration

Access http://192.168.1.100:8000 through the browser. If you can access the background application normally, it means that the configuration is successful.

(III) Use iptables for port forwarding

If you want to forward the 8000 ports accessed by externally directly to the 8000 port of the backend application, you can use iptables for port forwarding. This method does not require modifying the configuration of Nginx or background applications.

1. Configure iptables

Execute the following command to forward the externally accessed port 8000 to the backend application's port 8000:

iptables -t nat -A PREROUTING -p tcp --dport 8000 -j REDIRECT --to-port 8000

2. Verify the configuration

Access through the browser: 8000. If you can access the background application normally, it means that the configuration is successful.

(IV) Modify the port of the background application

If it is inconvenient to modify the listening port of Nginx, you can consider modifying the port of the background application from 8000 to other unoccupied ports (such as 8001), and then proxying to the new port in Nginx.

3. Modify the background application configuration

Modify the configuration of the background application to listen for new port 8001. Take Python Flask application as an example:

from flask import Flask
app = Flask(name)
@(‘/')
def hello():
return ‘Hello, World!'

4. Modify Nginx configuration file

Edit Nginx's configuration file and modify the proxy target port to 8001:

server {
listen 8000;
server_name ;
}
location / {
    proxy_pass http://127.0.0.1:8001;  # Modify to a new port for the background application    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

5. Restart Nginx and background applications

After saving the configuration file, restart the Nginx service and background applications:

systemctl restart nginx

6. Verify the configuration

Access through the browser: 8000. If you can access the background application normally, it means that the configuration is successful.

3. Choose the right method

In practical applications, which method to choose depends on your specific needs and server environment. Here are some options:

(1) If it is more convenient to modify the listening port of Nginx, the recommended method is 1. This method is simple and direct, and does not require modifying the configuration of the background application, nor does it affect the server's network environment.

(2) If the server has multiple IP addresses, method 2 can be used. This method allows Nginx and background applications to listen to different IP addresses separately to avoid port conflicts, and also improve system security.

(3) If you want to forward directly through iptables, you can use Method 3. This method does not require modifying the configuration of Nginx or background applications, and is suitable for some special scenarios

This is the end of this article about the solution to the conflict between Nginx and backend application ports. For more information about the conflict between Nginx and backend application ports, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!