How to redirect port 80 to different server in nginx?

Member

by larissa , in category: Third Party Scripts , 3 months ago

How to redirect port 80 to different server in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 3 months ago

@larissa 

To redirect port 80 to a different server in nginx, you can use the 'proxy_pass' directive in your nginx configuration file. Here is a step-by-step guide on how to do this:

  1. Open your nginx configuration file, which is typically located at '/etc/nginx/nginx.conf' or '/etc/nginx/sites-available/default'.
  2. Inside the server block for port 80, add the following configuration:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://new-server-ip-or-url;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}


Replace 'yourdomain.com' with your actual domain name and 'http://new-server-ip-or-url' with the IP address or URL of the server you want to redirect port 80 to.

  1. Save the configuration file and reload nginx for the changes to take effect:
1
sudo nginx -s reload


Now, any request to port 80 on your domain will be redirected to the new server specified in the proxy_pass directive.