Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Below is a config file that I am using with Nginx to redirect all WWW and non-WWW to the https:/domain.com URL

server {
    listen 80 443;
    server_name domain.com www.domain.com;
    ssl_certificate /usr/share/ca-certificate/pvt/ssl-unified.crt;
    ssl_certificate_key /usr/share/ca-certificate/pvt/ssl.key;
    return 301 https://domain.com$request_uri;
}
server {
    server_name domain.com;
    ssl_certificate /usr/share/ca-certificate/pvt/ssl-unified.crt;
    ssl_certificate_key /usr/share/ca-certificate/pvt/ssl.key;
    listen *:443 ssl;
    index index.php index.html index.htm;
    root /usr/share/nginx/www;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/www;
    }
}

In the folder located at /usr/share/nginx there is www and lah. When I load www.domain.com in my browser, it points to lah.domain.com

When I load domain.com it changes to https://domain.com and gives me a "Web Page Not Available - Error code: ERR_CONNECTION_REFUSED"

What did I enter wrongly on my Nginx configuration file?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

OK, I found out what the issue was, firstly, I removed the 443 and ssl_certificate* from the top block:

server {
    listen 80;
    server_name domain.com www.domain.com;
    return 301 https://domain.com$request_uri;
}

Then I just left hte bottom block in place.

I found that in /etc/nginx/sites-enabled/ I had a second config file for the lah subdomain and there was a symlink for default to /etc/nginx/sites-available/default that didn't exist and was using the URL for lah.domain.com

So, therefore it was like this:

[http|https]://*.domain.com -> https://lah.domain.com

That was what this original config file was used to do when I was testing.

Secondly, I repaired the symlink and put the below config file default to the location as required and removed everything else I didn't need:

# sudo ls /etc/nginx/sites-enabled
-rw-r--r--  1 root root 1320 Jan 30 13:25 default -> /etc/nginx/sites-available/default

I put the file below, as it is, that directory as default:

server {
    listen 80 443;
    server_name domain.com www.domain.com;
    ssl_certificate /usr/share/ca-certificate/pvt/ssl-unified.crt;
    ssl_certificate_key /usr/share/ca-certificate/pvt/ssl.key;
    return 301 https://domain.com$request_uri;
}
server {
    server_name domain.com;
    ssl_certificate /usr/share/ca-certificate/pvt/ssl-unified.crt;
    ssl_certificate_key /usr/share/ca-certificate/pvt/ssl.key;
    listen *:443 ssl;
    index index.php index.html index.htm;
    root /usr/share/nginx/www;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/www;
    }
}

Now it works. Everything [http|https] for [www|non-www] will redirect to: https://domain.com

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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