3

I have the following server config:

server {
  listen 80;
  server_name default;
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name default;
    root /home/forge/default/public;

    ... and so it continues

I also have two domain names, test.com and example.com. They both point to the IP adress from the server. If I go to test.com, it gets redirected to https fine, but if I go to example.com, it gets redirected to the https version of test.com instead of the https version of example.com. Why does this happen? I'm very new to server configs. On other Server Fault answers I only found to use the $host variable but it doesn't seem to work.

2
  • Could be the browser cache - clear the cache or use private/incognito mode. Also, analyse the HTTP headers using a suitable plugin. Sep 20 '16 at 13:52
  • That actually did the job. Didn't know the browser was capable of caching server settings! Thanks :-)
    – Markinson
    Sep 20 '16 at 14:18
1

I just had the same issue and indeed the browser cache was the problem!

Just a note on that: one of the best solution (not cached) to check the redirects would be to curl the server from command line:

curl -I http://my-server.com

About the browser cache, clearing the whole cache - refreshyourcache.com - would work but it's slightly overkill! Another way to ensure the browser is not using the cache is to request a slightly different url, ie:

http://my-server.com?randomString

Another point I wanted to quote after reading the server_name nginx doc: it feels that the recommended way to accept multiple domain names is to use the empty server_name "" rather than default, ie for your config example:

server {
  listen 80;
  server_name "";
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name "";
    root /home/forge/default/public;

    ... and so it continues

But please someone, correct me if I'm wrong! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.