簡體   English   中英

NGINX在http上顯示空白頁面到https重定向

[英]NGINX showing blank page on http to https redirect

我已經在具有獨角獸,Rails 4.2.0和Ruby 2.2.3的亞馬遜ec2服務器上安裝了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;
            }
    }
  }

那么,如何解決這個問題呢?

請把重定向移到它自己的塊;

您能否將其用作重定向:

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

除非有必要,否則我不希望在Nginx中使用IF條件。 看看這是否可行,我們可以進行處理。

還請刪除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什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM