简体   繁体   English

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

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

I just deployed my Rails 4 app on a VPS with Ubuntu, Unicorn and NginX. 我刚刚在Ubuntu,Unicorn和NginX的VPS上部署了Rails 4应用程序。

For my app I need to use SSL, so I have this ApplicationController : 对于我的应用程序,我需要使用SSL,所以我有以下ApplicationController

class ApplicationController < ActionController::Base

  force_ssl

  ...

end

This is my nginx.conf : 这是我的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;
                 }
        }
}

Everything seems to be working OK. 一切似乎都正常。 When a request for an http page comes, it gets forwarded to https , which is nice. 当请求http页面时,它将转发到https ,这很好。

However, when trying to change the locale of the application in the browser (through the languages menu), I get this error message in the browser: 但是,当尝试在浏览器中更改应用程序的locale时(通过语言菜单),我在浏览器中收到以下错误消息:

Safari can't open the page https://app_server/en/sessions/new because Safari can't find the server "app server" Safari无法打开页面https://app_server/en/sessions/new因为Safari无法找到服务器“应用服务器”

In the URL it also says: https://app_server/en/sessions/new 在网址中还显示: https://app_server/en/sessions/new

What am I missing here? 我在这里想念什么?

I am fairly new to NginX, so maybe someone can help me out here? 我对NginX相当陌生,所以也许有人可以在这里帮助我?

Any general advice on how to enhance my code is highly appreciated. 非常感谢您提供有关如何增强我的代码的一般建议。

You're using proxy_pass http://app_server; 您正在使用proxy_pass http://app_server; which set Host header to app_server by default. 默认情况下将Host标头设置为app_server Add proxy_set_header Host $host; 添加proxy_set_header Host $host; , so your application will get right 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