繁体   English   中英

如何使SSL在Nginx上的Rails中工作?

[英]How to get SSL to work in Rails on Nginx?

我刚刚在Ubuntu,Unicorn和NginX的VPS上部署了Rails 4应用程序。

对于我的应用程序,我需要使用SSL,所以我有以下ApplicationController

class ApplicationController < ActionController::Base

  force_ssl

  ...

end

这是我的nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        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/xml text/css text/comma-separated-values;
        upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        server {
                listen 80;
                server_name myapp.com;
                rewrite ^ https://$server_name$request_uri? permanent;
        }

        server {
                listen 443;
                server_name myapp.com;
                root /home/rails/public;
                index index.htm index.html;

                ssl on;
                ssl_certificate /etc/ssl/myapp.com.crt;
                ssl_certificate_key /etc/ssl/myapp.com.key;

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

                location @app {
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Forwarded-Proto $scheme;
                        proxy_redirect off;
                        proxy_pass http://app_server;
                 }
        }
}

一切似乎都正常。 当请求http页面时,它将转发到https ,这很好。

但是,当尝试在浏览器中更改应用程序的locale时(通过语言菜单),我在浏览器中收到以下错误消息:

Safari无法打开页面https://app_server/en/sessions/new因为Safari无法找到服务器“应用服务器”

在网址中还显示: https://app_server/en/sessions/new

我在这里想念什么?

我对NginX相当陌生,所以也许有人可以在这里帮助我?

非常感谢您提供有关如何增强我的代码的一般建议。

您正在使用proxy_pass http://app_server; 默认情况下将Host标头设置为app_server 添加proxy_set_header Host $host; 因此,您的应用程序将获得正确的Host

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM