Nginx Redirect Loop with Multiple Server Blocks – Troubleshooting Guide

301-redirectnginxsslvirtualhost

I'm using Nginx to host a number of virtual hosts (or server blocks in Nginx nomenclature). These hosts all share the same domain with each assigned its own subdomain. One subdomain enters an endless redirect loop, obviously undesirable behavior.

NB: I've redacted my domain name and replaced it with <mysite>.

The root domain – www.<mysite>.com receives requests as expected. It also redirects HTTP requests to HTTPS. It is configured in <mysite>-www.

The subdomain assets.<mysite>.com also receives requests as expected. It isn't configured to redirect HTTP to HTTPS, either protocol is served. There is no trouble. It is configured in <mysite>-holding.

The final subdomain soft.<mysite>.com is configured identically to assets.<mysite>.com but it falters. It instead sends the browser on a redirect loop, each time pointing to https://soft.<mysite>.com This is despite there being no such setting in the config file for this vhost. It is configured in <mysite>-soft.

Config for <mysite>-www:

server {
    server_name www.<mysite>.com;

    listen 80 default_server ;
    listen [::]:80 default_server;

    location / {
        return 301 https://$server_name$request_uri;
    }

    root /var/www/html;
}

server {
    listen              443 ssl default_server;
    ssl_certificate /etc/letsencrypt/live/www.<mysite>.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/www.<mysite>.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;

    root /var/www/html;
    server_name www.<mysite>.com;
    error_page 404 /404.html;

    location / {
        limit_req zone=mylimit burst=20 nodelay;
        try_files $uri $uri/ =404;
        index index.html index.htm;
    }
}

Config for <mysite>-holding:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;

    server_name assets.<mysite>.com;

    ssl_certificate /etc/letsencrypt/live/assets.<mysite>.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/assets.<mysite>.com/privkey.pem; 
    include /etc/letsencrypt/options-ssl-nginx.conf;

    root /var/www/holding/html;

    location / {
        limit_req zone=mylimit burst=20 nodelay;
        try_files $uri $uri/ =404;
        index index.html;
    }

The config file for soft.<mysite>.com is identical to the one above, except for where it says 'assets' it says 'soft'. And yet it enters this endless redirect loop.

I've tried varying the location directive. I've consulted the docs which were better than expected. Still, it loops. Help will be appreciated!

Best Answer

As it happened, the problem was as follows:

I hadn't symlinked properly from /sites-enabled to /sites-available and it was using an old duplicate of <mysite>-www which caused the redirect loop.

Having the file configured as above in my question was the solution. Either copying it to sites-enabled, or symlinking it properly will solve the problem for anyone in a similar predicament.

Mea culpa.