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?
Best Answer
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.
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.