0

I tried to redirect http://www & http:// on https://, but I have a loop of redirection, do you know why ? When I only put server_name www.mywebsite.lol it's ok for https redirect...

I tried

server {
    server_name     mywebsite.lol;
    rewrite ^(.*)   https://mywebsite.lol$1 permanent;
}

and

server {
    server_name     mywebsite.lol;
    return          301 https://$server_name$request_uri;
}

And my full: configuration

server {
# Port
listen 80;

# Hostname
server_name mywebsite.lol;

# Logs (acces et erreurs)
access_log /var/log/nginx/mywebsite.lol.access.log;
error_log /var/log/nginx/mywebsite.lol.error.log;

# Repertoire
root /home/mywebsite/www;

# Fichier a executer par defaut (en ordre)
index index.html index.php;

pagespeed off;

# Needs to exist and be writable by nginx.  Use tmpfs for best performance.
pagespeed FileCachePath /var/lightpics_ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
pagespeed DisableFilters remove_comments;

#Expire Header
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

}

# Refuse accès httaccess
location ~ /\. {
    deny all;
}

}

10
  • What does the HTTPS server block look like?
    – womble
    Aug 12, 2015 at 23:46
  • I only have that in my file.lol. I use https thanks to cloudflare, I don't have certificate Aug 12, 2015 at 23:49
  • Well, there's your problem. You don't have any configuration that would actually serve content.
    – womble
    Aug 12, 2015 at 23:50
  • Are you sure ? When I remove https and add www. I have welcome to nginx blahblahblah Aug 12, 2015 at 23:52
  • Yes, based on the information you've provided, I'm sure.
    – womble
    Aug 12, 2015 at 23:53

2 Answers 2

1

The proper way to do a Nginx redirect is with HTTP 301, not a rewrite. See here.

As far as your server setup goes, see mine below. I define the server name on port 80, then redirect to HTTPS on port 443. Everything (SSL settings, root, index, etc...) is applied under this HTTPS server block.

server {
        listen 80 default_server;                       #Listen on IPv4
        listen [::]:80;                                 #Listen on IPv6
        server_name loganmarchione.com;
        return 301 https://$host$request_uri;           #Redirect HTTP to HTTPS
}

server {
        listen 443 ssl spdy;                            #Listen on IPv4
        listen [::]:443 ssl spdy;                       #Listen on IPv6

        ssl_certificate /etc/nginx/ssl/unified2.crt;
        ssl_certificate_key /etc/nginx/ssl/my-private-decrypted.key;

        ...
1
  • I concur, this is how I have it configured pretty much the same way on a production site. The only difference is I have separate log files for each server.
    – Mick T
    Jun 13, 2016 at 19:22
0

You are getting the redirect loop because the same 'server' directive is answering requests for both http (port 80) and https (port 443)

You want to have separate 'server' blocks for http vs https - the http block should only redirect to https, whereas the https block should include your main config, ie 'root' & etc.

server {
  listen 80;
  server_name mywebsite.lol;
  rewrite ^(.*) https://mywebsite.lol$1 permanent;
}

server {
  listen 0.0.0.0:443 ssl;
  server_name mywebsite.lol;

  ssl_certificate      /etc/pki/tls/certs/mywebsite.lol.crt;
  ssl_certificate_key  /etc/pki/tls/certs/mywebsite.lol.key;

  # Repertoire
  root /home/mywebsite/www;

  ...

}

You must log in to answer this question.

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