0

I have an issue with my nginx configuration , i want to redirect www.yoursay.transport.nsw.gov.au to https://yoursay.transport.nsw.gov.au

Here is my configuration

server {
    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/yoursay.transport.nsw.gov.au/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yoursay.transport.nsw.gov.au/privkey.pem;

    # enable OCSP stapling to speed up first connect
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/letsencrypt/live/yoursay.transport.nsw.gov.au/chain.pem;

    if ($host = www.$server_name) {
    rewrite ^(.*) https://$server_name$request_uri? permanent;
    }
    server_name yoursay.transport.nsw.gov.au;

    root /var/www/ehq;

    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload";

    include snippets/common_blocks.inc;
}

server {
    listen 80;

    server_name yoursay.transport.nsw.gov.au;
    return 301 https://yoursay.transport.nsw.gov.au$request_uri;
}

But still it doesnt works , it says ERR_CONNECTION_CLOSED

Please help me out to fix this issue .

anemyte
  • 17,618
  • 1
  • 24
  • 45
sambit
  • 339
  • 4
  • 12

1 Answers1

1

Replace this:

    if ($host = www.$server_name) {
    rewrite ^(.*) https://$server_name$request_uri? permanent;
    }
    server_name yoursay.transport.nsw.gov.au;

with this:

    if ($host = www.yoursay.transport.nsw.gov.au) {
        return 301 https://yoursay.transport.nsw.gov.au$request_uri;
    }
    server_name yoursay.transport.nsw.gov.au www.yoursay.transport.nsw.gov.au;

Explanation:

When someone visit your website they sent HTTP request headers. One of them is Host which is the address they've used to reach the website. It can be an IP-address, www.example.com, example.com, or something.completely.irrelevant. server_name option defines to which Host headers this configuration belong. That is why I added www.yoursay.transport.nsw.gov.au to server_name, so this configuration is used for visitors accessing the address. You may also wish to repeat this in your HTTP server block (the one listening on port 80).

anemyte
  • 17,618
  • 1
  • 24
  • 45
  • it doesn't work it says ,nginx: [emerg] unknown directive "redirect" in /etc/nginx/sites-enabled/www.yoursay.transport.nsw.gov.au:37 nginx: configuration file /etc/nginx/nginx_resty.conf test failed – sambit Jan 11 '21 at 09:13
  • @sambit Aw, sorry, not `redirect`, `return`. I'll fix the answer. – anemyte Jan 11 '21 at 09:15
  • sorry to say but it doesnt work now i am seeing another error - NET::ERR_CERT_COMMON_NAME_INVALID after adding the above statement – sambit Jan 11 '21 at 09:58
  • @sambit That is because your certificate issued for `yoursay.transport.nsw.gov.au` only. You need a certificate with two names `yoursay.transport.nsw.gov.au` and `www.yoursay.transport.nsw.gov.au` or a wildcard certificate `*.yoursay.transport.nsw.gov.au`. Just ask a new one with two names from letsencrypt. – anemyte Jan 11 '21 at 10:02
  • can you please tell me how do i request a new certificate for both the domain name or wildcard certificate ? – sambit Jan 11 '21 at 10:18
  • @sambit It depends on how you got the first one. Did you use `certbot`? – anemyte Jan 11 '21 at 10:58
  • 1
    it got fixed after obtaining the certificate via certbot force renewal method . sudo certbot certonly --force-renewal --webroot -w /var/www -d www.yoursay.transport.nsw.gov.au – sambit Jan 11 '21 at 11:42
  • 1
    @sambit glad to help. I hope you did specify `-d` several times because otherwise you just replaced one single-name certificate with another. – anemyte Jan 11 '21 at 11:44