R.Han
R.Han

Reputation: 13

www in domain not working - nginx

I'm having problems getting domains to work using nginx when you include www. in the url.

mydomain.com --> Working but www.mydomain.com --> Not Working

Config file:

server {
    listen 80;
    listen [::]:80;
    #server_name git.mydomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 default_server ssl;
    listen [::]:443 default_server ssl;
    server_name git.www.mydomain.com;
    # certs sent to the client in SERVER HELLO are concatenated in ssl_certific$
    ssl_certificate /etc/nginx/ssl/mydomain.com.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 4096 bits
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;

    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDH$
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;

    location / {
            try_files $uri $uri/ =404;
    }
}

Can you help me? Thank you!

Upvotes: 0

Views: 1442

Answers (1)

Dez
Dez

Reputation: 5838

You need to set up the server names you want Nginx to serve from.

server {
    listen 80;
    listen [::]:80;
    server_name mydomain.com www.mydomain.com;
    return 301 https://$host$request_uri;
}

So you list the names you want separated by spaces. I assume that you want to redirect all your www and non-www traffic to https with the server name you have set up in the https configuration, and that your https server name is right.

Upvotes: 1

Related Questions