0

I've changed my conf file so that when a user types in the domain without www it redirects to the domain with www:

server_name example.com; return 301 $scheme://www.example.com$request_uri; I also wish for my https for anything under /user

I get the error of too may redirects, where am I going wrong?

So I have:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /var/www/example.com/site;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name example.com;
return 301 $scheme://www.example.com$request_uri;

location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
}
location /user {
        rewrite ^ https://$http_host$request_uri? permanent;
}
}

For port 443:

server {
listen 443;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;

root /var/www/example.com/site;
index index.html index.htm;

ssl on;
ssl_certificate //path here
ssl_certificate_key //path here

location / {
        rewrite ^ http://$http_host$request_uri? permanent;
}
location /user {
}
}
2
  • Where are your other two server blocks? Oct 4, 2015 at 17:58
  • Michael Hampton - im not sure what you mean?
    – panthro
    Oct 4, 2015 at 18:02

2 Answers 2

2

The best way to do this is to have separate server blocks for example.com and www.example.com. The entry for example.com should only have:

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

The entry for www.example.com will have everything else (and obviously not have the redirect).

Similarly, you will have two separate server entries for https (port 443).

2
  • Thanks but could I possibly have a complete example? It's not clear how I would restrict https for /user only?
    – panthro
    Oct 4, 2015 at 18:51
  • The config you have looks correct for that. You just need to split example.com and www.example.com into separate sections and it should work fine.
    – seumasmac
    Oct 4, 2015 at 18:56
0

I would do something like this: (untested)

server {
listen       80;
server_name  example.com;
return       301 http://www.example.com$request_uri;
}
server {
       listen       80;
       server_name  www.example.com;
       root /var/www/example.com/site;
       index index.html index.htm;

location / {
       rewrite ^/index.php;
          }
}
server {
   listen 443;
   server_name www.example.com;
   root /var/www/example.com/site;
   index index.html index.htm;

   ssl on;
   ssl_certificate //path here
   ssl_certificate_key //path here


  location /user {
                  rewrite ^ https://$http_host$request_uri? permanent;
                 }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.