2

Running Nginx and trying to redirect:

www to non-www

http to https

I realize there are similar threads, but none with the same scenario.

I've installed Webmin/Virtualmin, and Fast-CGI. I have many accounts/sites on this server. For mysite, this is the server block:

server {
    listen my_server_IP;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
    root /home/example/public_html;
    index index.html index.htm index.php;
    access_log /var/log/virtualmin/example.com_access_log;
    error_log /var/log/virtualmin/example.com_error_log;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param SCRIPT_FILENAME /home/example/public_html$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT /home/example/public_html;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param HTTPS $https;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/php-nginx/148180748420424.sock/socket;
    }
    # htaccess
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    #
    listen my_server_IP:443 ssl;
    ssl_certificate /home/example/ssl.cert;
    ssl_certificate_key /home/example/ssl.key;
}

Now, the config as it looks now, is none of my doing, apart from the #htaccess bit (converted from Apache's .htaccess to enable "pretty links" in Wordpress) and the "return 301" line.

The SSL certificate is from LetsEncrypt, if that makes a difference.

I've saved and restarted Nginx.

When requesting the site in Chrome, it tells me "ERR_TOO_MANY_REDIRECTS"

What am I doing wrong? Do I need to add or remove anything?

2

1 Answer 1

3

This is a simplified version of your current config (minus all the bits that currently won't do anything content wise) with some notes added so you can see what is happening. The last two lines just keeping sending things back to the start. Hence to many redirects error.

server {
    listen my_server_IP; #listen on IP x.x.x.x
    listen my_server_IP:443 ssl; #listen on IP x.x.x.x on 443
    server_name example.com www.example.com; #Of the requests on IP x.x.x.x look for those with with one of these headers. send to line below       
    return 301 https://example.com$request_uri; #Send everything to the line above
    } 

This is what you need, you can use an if statement but lets not this time, just so you can clearly see whats going on. Its more lines but it will work.

server {
    listen my_server_IP;
    server_name example.com www.example.com; #Listen for non-https requests 
    return 301 https://example.com$request_uri; #Send to the correct https address
    }
server {
    listen my_server_IP:443 ssl;
    server_name www.example.com; #Listen for https (www) requests
    return 301 https://example.com$request_uri; #Send to the correct https 
    ssl_certificate /home/example/ssl.cert;
    ssl_certificate_key /home/example/ssl.key;
    }
server {
    listen my_server_IP:443 ssl;
    server_name example.com;
    ssl_certificate /home/example/ssl.cert;
    ssl_certificate_key /home/example/ssl.key;
    <Rest of your config from above, fastcgi etc>
    }
1
  • 1
    Note that the SSL certificate directives need to appear in both ssl server blocks. Dec 20, 2016 at 19:32

You must log in to answer this question.

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