ruby-on-rails - nginx 服务器中的 Ruby on Rails,HTTPS 重定向到 HTTP

标签 ruby-on-rails ssl nginx passenger

我有一个客户想要在其网站上使用 SSL,所以我获得了证书并使用它设置了 nginx conf(下面是配置)。如果我不将 HTTPS 部分的根指向真正的服务器根,它会工作,但如果我将根设置为站点文件,HTTPS 将被重定向到 HTTP。没有错误消息。

有什么想法吗?

user  www-data;
worker_processes  4;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    passenger_root /usr/local/rvm/gems/ruby-1.9.3-p448/gems/passenger-4.0.14;
    passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p448/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.nope.se;

    passenger_enabled on; 
    root /var/www/current/public/;

        #charset koi8-r;

        #access_log  logs/host.access.log main;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen       443;
        server_name  www.nope.se;

        ssl                  on;
        ssl_certificate      /opt/nginx/cert/www.nope.se.crt;
        ssl_certificate_key  /opt/nginx/cert/www.nope.se.key;

        ssl_session_timeout  10m;

        #ssl_protocols  SSLv2 SSLv3 TLSv1;
        #ssl_ciphers  HIGH:!aNULL:!MD5;
        #ssl_prefer_server_ciphers   on;

    passenger_enabled on;
        root /var/www/current/public/;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    }

}

最佳答案

老实说,我不明白你的问题。但是这里有一些关于典型的 nginx-https 配置是如何完成的。希望你觉得它有用。

SSL 是一种工作在 HTTP 之下一层的协议(protocol)。将其视为 HTTP 协议(protocol)在其中传播的隧道。因此,无论您在何处指定它们,都会在任何 HTTP 相关配置之前加载您的 SSL 证书。这也是为什么每个 nginx 实例应该只有一个 SSL 设置的原因。

我建议您像这样将 ssl 证书相关逻辑移动到单独的 server block 中。

server {
   listen                    443 ssl default_server;
   ssl_certificate           ssl/website.pem;
   ssl_certificate_key       ssl/website.key;
   ssl_trusted_certificate   ssl/ca.all.pem;
   ssl_session_cache         builtin:1000     shared:SSL:10m;  
   ssl_session_timeout       5m;


   ssl_protocols             SSLv3 TLSv1 TLSv1.1 TLSv1.2; # default on newer versions
   ssl_prefer_server_ciphers on;

   # The following is all one long line. We use an explicit list of ciphers to enable
   # forward secrecy without exposing ciphers vulnerable to the BEAST attack

   ssl_ciphers  ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES128-SHA:RC4-SHA:RC4-MD5:ECDHE-RSA-AES256-SHA:AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:DES-CBC3-SHA:AES128-SHA;

   # The following is for reference. It needs to be specified again
   # in each virtualhost, in both HTTP and non-HTTP versions.
   # All this directive does it to tell the browser to use HTTPS version of the site and remember this for a month
  add_header                Strict-Transport-Security    max-age=2592000;
}

我还建议您在非 https 服务器 block 中设置 301 重定向,如下所示。

改变这个:

 server {
    listen       80;
    server_name  www.nope.se;  
    ... 
 }

像这样:

server {
    listen       80;
    server_name  www.nope.se;
    add_header   Strict-Transport-Security  max-age=7200;
    return       301                        https://$host$request_uri;
 }

有了这个,当用户访问 http://www.nope.se 时,他们将被自动重定向到 https://www.nope.se

关于ruby-on-rails - nginx 服务器中的 Ruby on Rails,HTTPS 重定向到 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212204/

相关文章:

ruby-on-rails - 使用 Active Record 查询 json 数组 (Rails 4/postgresql9.4)

ruby-on-rails - ruby cucumber 测试实践

ruby-on-rails - Rails Friendly_id唯一子弹格式

Qt5 QWebView如何发送客户端证书?

Python (pip) throw [SSL : CERTIFICATE_VERIFY_FAILED] even if certificate chain updated

html - HTML5 模式下的 NGINX、proxy_pass 和 SPA 路由

django - Docker中的Django微服务

ruby-2.2.2升级后mysql无法连接到环境

ssl - 我在 Google Cloud 托管上的 SSL 证书在哪里?

Nginx Ingress - 如果没有匹配的路由,如何设置重定向到外部站点?