1

I want to redirect from http to https in an NGINX server block.

Following an answer in a related post I tried adding another server block in the following NGINX config file, but got a 'conflicting server name' warning, as (presumably) the value of server_name doesn't include the protocol prefix:

server {
    listen 80;
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    server_name sub.domain.co.uk;
    return 302 https://sub.domain.co.uk$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    server_name sub.domain.co.uk;
    location / {
        root /home/saves/webapps/html/;
        index index.html;
    }
    location /api/ {
        [...]
   }
}

2 Answers 2

3

The problem is that you have configured two server blocks listening for SSL on the same port with the same server name and there's no way for nginx to decide which one to choose in these conditions. Change it to :

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    server_name _;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    return 302 https://sub.domain.co.uk$request_uri;
}

server {
    listen 443;
    server_name sub.domain.co.uk;

    location / {
        root /home/saves/webapps/html/;
        index index.html;
    }
    location /api/ {
        [...]
   }
}
1
  • thanks Xavier - see my solution below (which works) and let me know if there's anything better. Oct 20, 2014 at 14:34
0

I thought about this, did a little research, and solved it with a much simpler server block before the main one, from this answer:

server {
    listen 80;                            
    location / {
        return 301 https://sub.domain.co.uk;
    }
}

But finally amended it after the comments to this:

server {
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    server_name sub.domain.co.uk;
    location / {
        return 301 https://sub.domain.co.uk;
        root /home/saves/webapps/html/;
        index index.html;
    }
    [...]
}

So thanks to you both for your swift and useful replies.

6
  • 2
    It won't work for SSL connections not matching your domain name : you need a default vhost to redirect these to your actual domain. Oct 20, 2014 at 14:34
  • Note that the location block isn't strictly required here.
    – gparent
    Oct 20, 2014 at 14:40
  • Yes, I just found that out. I need to regenerate the SSL certificate for the new domain name and change my config file. Oct 20, 2014 at 14:42
  • @gparent - then where do I place the return statement - can it go in the main (second) block if I add listen 80 at the top? Oct 20, 2014 at 14:45
  • 1
    Yes, the return statement can be part of the main block for this kind of global redirect.
    – gparent
    Oct 20, 2014 at 14:49

You must log in to answer this question.

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