Nginx return 301 redirects all domains

6,025

You should read how nginx processes requests.

In your case, requests with unknown domain falls to default server. Usually first one, but could be any. To distinctly define default server you should use default_server flag in listen directive. So in your server block which should catch all other domains you should write:

listen 80 default_server;
Share:
6,025

Related videos on Youtube

Tomas
Author by

Tomas

Updated on September 18, 2022

Comments

  • Tomas
    Tomas almost 2 years

    I have multiple domains on my server that is running nginx setup as follow:

    server {
        listen 80;
        server_name domain1.com www.domain1.com;
        root /var/www/domain1/public_html;
        index index.php index.html index.html;
    }
    
    server {
        listen 80;
        server_name domain2.com www.domain2.com;
        return 301 https://plusgoogle.com/+somepage;
    }
    
    server {
        listen 80;
        root /var/www/other_domains/public_html;
        index index.php index.html index.html;
    }
    

    Each server block is in its separate file (domain1.conf, domain2.conf etc)

    Now to the problem:

    • Domain1 is working correctly as it should
    • Domain2 - unfortunately cannot test right now, have some problems with godaddy, but I am pretty sure it will work fine.
    • Other domains redirect to the 301 url from domain2 settings

    I am a little confused, I could understand if domain2 would be loading root of other domains, but how come other domains redirect to 301 when I specified server_name in the config together woth 301?

    Thanks for help

    Update

    • Have you made sure that your browser isn't caching - I tried to open the sites on different devices, I think that corevs that

    • Are the domains as distinct in reality as they are in your example above - Yes, actually I don't see why wouldn't I give the domain names: domain1 = tomasdostal.com, domain2=autodopravadostal.cz, other domains= casull.info, forgotmap.com etc.

    • Is the second server block config present anywhere else or only in this configuration file - I am setting up 100% new aws instance so I am pretty sure its only there.

    • Tomas
      Tomas over 9 years
      @BE77Y I updated the question
  • Tomas
    Tomas over 9 years
    I see, yes I didn't read the documentation throughly enough