0

I have my nginx config like::

# HTTP - redirect all requests to HTTPS:
server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        return 301 https://$host$request_uri;
}

# HTTPS - proxy requests on to local Node.js app:
server {
        listen 443;
        server_name example.com;

        ssl on;
        # Use certificate and key provided by Let's Encrypt:
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        # Pass requests for / to localhost:3000:
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:3000/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
        }
}

Currently with the config above:

http://example.com get redirected to https://example.com.

And when I type http://www.example.com that gets redirected to https://www.example.com.

I would like the www requests to redirected to non-www So if someone types http://www.example.com or https://www.example.com I would like it to be redirected to https://example.com. How would would I accomplish this?

4
  • 4
    Possible duplicate of nginx rewrite from www to non-www
    – Tim
    Mar 31, 2017 at 19:40
  • You need another server block listening explicitly for the domains you want to forward. Please read a tutorial, or any of the answer on this site that would answer your question. I have a tutorial here photographerstechsupport.com/tutorials/…
    – Tim
    Mar 31, 2017 at 19:41
  • @Tim, there is no need for a separate server block. Apr 1, 2017 at 15:22
  • @TeroKilkanen, I guess using default server works if it's the only site Nginx is running, but it's not how I do it. Also, if you want to forward www to https:// I think the only way is another server block. I'll provide an answer that illustrates, but I really think this is too trivial, it's been asked and answered 100 times.
    – Tim
    Apr 1, 2017 at 19:06

2 Answers 2

0

You need to replace

# HTTP - redirect all requests to HTTPS:
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
}

with:

# HTTP - redirect all requests to HTTPS:
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://example.com$request_uri;
}

This way nginx does not use the Host header in the client request for determining the host for redirection.

0

Here's the standard Nginx site setup to forward to non-www https website

# Main website, https non-www
server {
  server_name example.com;
  listen 443 ssl http2; # http2 is optional
  # locations, ssl configuration, etc
}

# forward https www to non-www
server {
  server_name www.example.com;
  listen 443 ssl;
  return 301 https://example.com$request_uri;
}    

# Forward http to https
server {
    listen       80;
    server_name  example.com www.example.com;
    access_log  /var/log/nginx/access.log main buffer=128k flush=1m if=$log_ua;
    return       301 https://example.com$request_uri;
}

You must log in to answer this question.

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