NGINX showing blank page on http to https redirect

2.7k Views Asked by At

I have installed nginx (version 1.6.3) on my amazon ec2 server with unicorn, Rails 4.2.0 and Ruby 2.1.3. Amazon load balancing is enabled on my system. The domain looks like abc.example.com. If no redirection code is written on nginx conf file, then both https://abc.example.com and http://abc.example.com seem working. But when I try to redirect all http requests to https, then sometimes it works for few seconds and then appear blank page, and sometimes it appears blank page from the beginning. Sometimes it shows 503 error too. The redirection code is:

        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host$request_uri? permanent;
        }

My nginx conf file looks like this:

user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
    worker_connections 768;
}
http {
sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

include /etc/nginx/mime.types;
    default_type application/octet-stream;
access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
gzip on;
    gzip_disable "msie6";
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

server {
            listen 80;
            listen 443 ssl;
            client_max_body_size 4G;
            server_name abc.example.com;
            root '/var/www/html/example/public';
            try_files $uri/index.html $uri.html $uri @unicorn;
            location @unicorn {
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Forwarded_Proto $scheme;

        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host$request_uri? permanent;
        }

                    proxy_redirect off;
                    proxy_pass http://unicorn;
                    proxy_read_timeout 300s;
                    proxy_send_timeout 300s;
            }
            error_page 500 502 503 504 /500.html;
            location = /500.html {
                    root /var/www/html/example/public;
            }
    }
  }

So, how to resolve this issue?

1

There are 1 best solutions below

7
On

Please move the redirection to its own block;

Can you please use this as your redirection:

server {
  server_name domain.com.au;
  server_tokens off;
  return 301 https://$host$request_uri;
}

I prefer not to use IF Condition in Nginx unless I have to. See if this works if not we can work on it.

Also please remove listen 80; if everything suppose to go to ssl then you can forget port 80.

listen 443 default deferred;

try it and let me know if you need more help.

Can you please adjust this setting to fit yours then restart the Nginx:

upstream unicorn {
    server unix:/tmp/unicorn.production_domain.sock fail_timeout=0;
}

server {
  server_name domain.com;
  server_tokens off;
  return 301 https://$host$request_uri;
}

server {

    listen 443 default deferred;
    ssl on;
    ssl_certificate /etc/ssl/SSL.crt;
    ssl_certificate_key /etc/ssl/domain.com.key;

    server_name domain.com.au;
    root /var/www/public_html/production.domain.com;
    access_log /var/www/public_html/production.domain.com/log/nginx.access.log;
    error_log /var/www/public_html/production.domain.com/log/nginx.error.log ;

    try_files $uri/index.html $uri @unicorn;
    location @unicorn {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unicorn;
    }

    error_page 500 502 503 504 /public/500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

What do you have in your unicorn.rb?