d1ch0t0my
d1ch0t0my

Reputation: 443

Nginx Redirect all to https and non www - redirect loop

I am trying to redirect all non http and www traffic for the server_name to https://example.com. I have the following issues:

  1. Any requests for http://www.example.com don't get redirected;
  2. Any requests for http : //example.com get error 400;
  3. Any requests for https go into a 301 redirect loop.

I've tried multiple variations using separate server blocks and the closest I came was getting it all working except the redirect loop which plagues me no matter what I do. Would appreciate some advice and pointers here. Code is below:

server {
   listen         443;
   listen         80;
   server_name    www.example.com example.com;

root /home/example/public_html;
index index.html index.htm index.php;

#SSL Stuff here

if ($scheme = http) { return 301 https://example.com$request_uri; }
if ($server_name = www.example.com) { return 301 https://example.com$request_uri; }

include conf.d/wp/restrictions.conf;
include conf.d/wp/wordpress.conf;
}
EDIT

So I tried the below and it all works with no 301 loop apart from http : //www.example.com is allowed to pass with no redirect to SSL. I don't understand how that's possible as it should be caught by the port 80 rule no? Updated config below:

server {
listen         80;
   server_name    example.com;
   server_name    www.example.com;
   return 301 https://$server_name$request_uri;
}

################# SECURE #################

server {
listen   443;
server_name example.com;
access_log /var/log/nginx/example-ssl.access.log;
error_log /var/log/nginx/example-ssl.error.log;

root /home/example/public_html;
index index.html index.htm index.php;

# SSL Stuff here

#include conf.d/wp/restrictions.conf;
#include conf.d/wp/wordpress.conf;
}

Upvotes: 1

Views: 451

Answers (1)

Leo Gallego
Leo Gallego

Reputation: 380

There are a few issues with your config.

  1. I would replace $server_name for $host or hardcode the domain you want, like "secondexample.com$request_uri"
  2. You are missing the ssl tag in the server 443 line.

Config:

server {
   listen         80;
   server_name    example.com;
   server_name    www.example.com;
   return 301 https://$host$request_uri;
}

################# SECURE #################

server {
   listen   443 ssl;
   server_name example.com;
   access_log /var/log/nginx/example-ssl.access.log;
   error_log /var/log/nginx/example-ssl.error.log;

   root /home/example/public_html;
   index index.html index.htm index.php;

   # SSL Stuff here

   #include conf.d/wp/restrictions.conf;
   #include conf.d/wp/wordpress.conf;
}

Upvotes: 0

Related Questions