Home > Net >  nginx reverse proxy sends all traffic to first defined server
nginx reverse proxy sends all traffic to first defined server

Time:08-24

I have multiple servers running on the same host. I am trying to configure nginx to route traffic based on the server_name, but all traffic is sent to the first defined server.

I have two urls:

  1. example.domain.net
  2. domain.net

which I have configured nginx to proxy with configuration:

server {
        listen 3978;
        listen [::]:3978;
        server_name example.domain.net:3978 example.domain.net:3978;

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass https://127.0.0.1:8443;
         }
}
server {
        listen 3978;
        listen [::]:3978;
        server_name domain.net:3978 www.domain.net:3978;

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass https://127.0.0.1:8020;
        }
}

But all traffic to both example.domain.net:3978 and domain.net:3978 is being sent to whichever server is defined first in the file (in this case example.domain.net)

I've seen other examples where this worked like This post. Is this possible with one having a subdomain and another not?

I am using nginx version 1.18.0 with the default nginx.conf on Ubuntu 18.04

CodePudding user response:

server_name should not have ports. Try removing :3978 from the server_name.

Because you have the ports, the hostname does not match any of the server_name. So, the entire traffic is sent to the first server which is considered as a default for no matches.

  • Related