Question

I've got 2 files with proxy_pass :

server {
    listen 80;
    server_name www.domain1.fr;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }
}

and

server {
    listen 80;
    server_name www.domain2.fr;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2369;
        proxy_redirect off;
    }
}

They both work fine.

Now, if I go to this server through another domain name (defined on my DNS server targeting the same IP address), eg. www.domain3.fr, instead of a 404, I've got the same result as www.domain2.fr !

I guess it's because of the last location /, but how can I do a 404 error if the domain name is not the same than server_name ?

Was it helpful?

Solution

I've answered this question so many times I'm thinking of finding a way to make it generic so I could share it when there's a question is asked.

Here's the other answers if you would like to look at it

you can read on any one why this is happening, on this reference link How nginx processes a request

Simple answer is a small server block to prevent it

server {
  listen 80 default_server;
  # return a code maybe or do any thing
  return 404;
}

of course reload nginx after that to reflect the settings.

OTHER TIPS

Well, I had to add a new config :

server {
    listen 80 default_server;
    server_name _;
    access_log off;
    return 404;
}

To be a "catch all".
Source : NGINX multiple server blocks with reverse proxy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top