How to map different domain names to the same port of the same server through nginx reverse proxy

To map different domain names to the same port on the same server in Nginx, you can use Nginx's proxy forwarding technology.

First, you need to understand how Nginx's proxy forwarding works. Nginx's proxy forwarding means that when the proxy server receives a request, it first forwards the request to the target server, then returns the server's response to the proxy server, and finally the proxy server returns the response to the client. end.

Now, suppose you have two domain names: example.com and example.net, both mapped to port 80 of the same server. You can use the following Nginx configuration to achieve this requirement:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://example.com:80;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

server {
    listen 80;
    server_name example.net;

    location / {
        proxy_pass http://example.net:80;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

In this configuration, the two servers listen to port 80 respectively. All requests from example.com will be forwarded to the example.com server, and then returned to the client by the example.com server. All requests from example.net will be forwarded to the example.net server, and then returned to the client by the example.net server.

In this way, the need for different domain names to be mapped to the same port on the same server is realized.

Guess you like

Origin blog.csdn.net/m0_59327517/article/details/132193443