After adding SSL certificate i got site too many redirects loop (nginx)

Jakxna360 asked:

Hello and thank you for helping me. I recently added an ssl certificate to my site. It worked for a little bit but now I am getting a “too many redirects loop” in the browsers. I don’t know much about servers (I’m following an online guide) so it might just be a rookie mistake.

Here is the file:

upstream app_server {
    server unix:/var/run/unicorn.sock fail_timeout=0;

server {
    listen 80;
    return 301$request_uri;

server {
    listen   443 ssl;
    listen  [::]:80 default ipv6only=on;
    root /home/example/public;
    rewrite     ^(.*)$1 permanent;
    ssl_certificate /;
    ssl_certificate_key /;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
    index index.htm index.html;

    location / {
            try_files $uri/index.html $uri.html $uri @app;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|$
                    try_files $uri @app;

     location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;

My answer:

It’s a rookie mistake.

You have placed the redirect in your https server block:

    rewrite     ^(.*)$1 permanent;

Which just redirects back to the same URL.

When you remove that, you should be fine. The redirect should only be in the block serving http on port 80, not in the block serving https on port 443.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.