0

I'm trying to setup a web server that will force https. Currently the root domain (I'll call it name.com) redirects to https no problem, but the sub-domain (my.name.com) doesn't. https://my.name.com does work.

This is my config file:

server {
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}    
server {
    # SSL configuration
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name my.name.com www.my.name.com;
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    ssl_certificate <path to cert>
    ssl_certificate_key <path to key>
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot     
}

What am I missing here?

1

Based on your configuration it looks like this is the default site on the server, if so, try editing your first serve block to be:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

You did not have the first block to be a default_server for port 80 and did not have any server_names listed (which would be required if it's not the default server) so no traffic was likely hitting that first server block.

When a block is set as default_server it tells Nginx to use that block to process all requests on the port/IP address which the block is bound to (in this case port 80 on all IPs) which do not match a more specific server block by name. Another way to think of it is as a "catch all" block.

When a block has one or more specific server names set that block will only apply to those hostnames. So in your case the second server block will only respond to my.name.com and www.my.name.com

See this site for additional information and details on SSL redirection.

7
  • Many thanks! Unfortunately that did not solve the issue. I wonder if you can clarify what adding default_server does and how nginx decides which is default? I do have server_name in my second server block, does that mean it won't be the default server?
    – DAnsermino
    Sep 26 '17 at 14:50
  • I've added some information about default_server for you. One question to assist in troubleshooting your issue would be to know exactly how the redirection for my.name.com is failing. Is your browser actually being redirected to my.name.com and then you are getting a certificate error or the page is not loading OR does the redirection never take place and your browser is just stuck at my.name.com? Sep 26 '17 at 15:18
  • Thank you. It times-out at my.name.com, so I would assume the redirect never takes place (https://my.name.com does work).
    – DAnsermino
    Sep 26 '17 at 15:33
  • That's what I was assuming originally. So the problem is that the first block is not "catching" the HTTP request to my.name.com It is possible that you have another Nginix configuration file on the system with a port 80 server block marked as the default_server which is catching the request instead. You could test that by changing your first server block so that it is pointed to a location on your system where it can serve some files from and see if that causes my.name.com to work. Sep 26 '17 at 15:41
  • I tried return 404; but it still just times out (after a minute or so). Made sure to clear my cache. Inside of /etc/nginx there is just default in sites-available and the symlink in sites-enabled
    – DAnsermino
    Sep 26 '17 at 17:37

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.