4

I have an nginx config that works as intended for one of my domains but there is a weird redirecting behaviour whenever I point another domain to my server.

Issue:
I want to redirect all request made to any non-www to the www version of my site.

Eg:
http://example.com should redirect to https://www.example.com
http://m.example.com should redirect to https://www.example.com

My config works as intended for example.com but whenever I cname, say cdn.test.com to my server ip with a separate config file. It redirects to www.cdn.test.com

This is my config for example.com:

    server{
        #redirect non-www to www
        listen 80;
        listen 443 ssl;
        server_name example.com;

        ssl_certificate .../cert.pem;
        ssl_certificate_key .../key.pem;   
        return 301 https://www.$host$request_uri;
    }


    server{
        #redirect request to the mobile site to the main site
        listen 80;
        server_name m.example.com;
        return 301 https://www.example.com$request_uri;
    }
    server{
        #redirect unsecure www to secure wwww
        listen 80;
        server_name www.example.com;
        return 301 https://$host$request_uri;
    }
    upstream backend {
        server 127.0.0.1:23042;
        server 127.0.0.1:23043 backup;   
     }
    server {
        listen 443 ssl;
        server_name www.example.com;

        access_log off;
        error_log off;

        ssl_certificate .../cert.pem;
        ssl_certificate_key .../key.pem;   

        location / {
            proxy_pass http://backend;
            proxy_redirect off;
            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 CF-IPCountry $http_cf_ipcountry;
        }

        location /static {
            alias .../app/static;
            expires 30d;
        }
    }

and this is the config for cdn.test.com:

    server {
      listen 80;
      server_name cdn.test.com;
       root /home/.../files;
    }
1
  • Use nginx -T (uppercase T) to view the entire configuration and ensure that the server_name cdn.test.com; server block is included. It behaves as if that last server block is being ignored. Commented Oct 7, 2019 at 9:53

2 Answers 2

5

In your config first server block is the default_server which will be used for all requests that don't match a more specific server.

Rather than return 301 https://www.$host$request_uri; in the default_server don't use the Host header from the request as a parameter to generate the redirect, as that can become all kinds of incorrect things, instead use return 301 https://www.example.com$request_uri; to always redirect to www.example.com

4
  • 1
    I would do return 302 https://www.$server_name$request_uri;.
    – uav
    Commented Oct 7, 2019 at 14:53
  • 1
    @uav : Whether to use permanent redirect with a HTTP 301 response or another of the possible HTTP redirect responses such as ` 302 | 303 | 307` depends a bit on your use case
    – HBruijn
    Commented Oct 7, 2019 at 14:59
  • Yes, I like to work with 302, because the redirects are not cached by the browser. I don't care if I get a worse rank in Google. Can you explain 303 and 307? Is that binary HTTP 2?
    – uav
    Commented Oct 7, 2019 at 17:55
  • Ah, now I see the WP article. Hmmm, why do they have to make this so complicated?
    – uav
    Commented Oct 7, 2019 at 17:58
0

You could set it up like me

 server {
#redirect all naked domains to https and www
        if ($host = aihello.com) {
            return 301 https://www.aihello.com$request_uri;
        } 
    
    
#redirect all unsecure www to secure www
        if ($host = www.aihello.com) {
            return 301 https://www.aihello.com$request_uri;
        } 
    
    
    server_name aihello.com www.aihello.com default_server;
    
    
        listen [::]:80 ipv6only=on;
        listen 80 ;
        return 404; 
    
    
    
    
    }
1
  • It is more performant to use separate blocks for each domain. Always try to avoid using if statements if possible.
    – jinyus
    Commented Oct 17, 2020 at 5:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .