NGINX在http到https重定向显示空白页面

时间:2015-06-11 09:44:47

标签: ruby-on-rails redirect nginx amazon-ec2 load-balancing

我已经在我的amazon ec2服务器上使用unicorn,Rails 4.2.0和Ruby 2.1.3安装了nginx(版本1.6.3)。在我的系统上启用了Amazon负载平衡。域名看起来像abc.example.com。如果没有在nginx conf文件上写入重定向代码,那么https://abc.example.comhttp://abc.example.com似乎都有效。但是当我尝试将所有http请求重定向到https时,有时它会工作几秒钟然后出现空白页面,有时它从一开始就显示为空白页面。有时它也显示503错误。重定向代码是:

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

我的nginx conf文件如下所示:

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;
            }
    }
  }

那么,如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

请将重定向移动到自己的块;

您可以将此作为重定向使用:

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

我不想在Nginx中使用IF Condition,除非我必须这样做。如果不能,我们可以查看它是否有效。

如果所有内容都要转到listen 80;,请删除ssl,然后您可以忘记端口80.

listen 443 default deferred;

尝试一下,如果您需要更多帮助,请告诉我。

您可以调整此设置以适合您的设置,然后重新启动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;
}

你的unicorn.rb有什么?

相关问题