【问题标题】:Serving Assets VIA SSL in Rails 3 App Using NGINX and Unicorn使用 NGINX 和 Unicorn 在 Rails 3 应用程序中通过 SSL 服务资产
【发布时间】:2014-02-02 20:43:12
【问题描述】:

我已经设置了我的 rails 应用程序,效果很好。不幸的是,在该网站的 https:// 版本上,我的任何资产都没有得到服务……知道为什么会发生这种情况吗?所有资产都通过 http:// 提供,但没有通过 https://

帮助?

============= 代码==============

upstream unicorn {


server unix:/tmp/unicorn.XXX.sock fail_timeout=0;
}

server {
    listen 80 default;
    server_name example.com;
    root /home/deployer/apps/XXX/current/public;

location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
}

try_files $uri/index.html $uri @unicorn;

location @unicorn {
    proxy_set_header X-Forwarded-Proto http;
    proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 5G;
keepalive_timeout 10;
send_timeout 240;
sendfile_max_chunk 5m;
}

server {
    listen 443;
    server_name example.com;
    root /home/webuser/apps/XXX/current/public;

location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
}

try_files $uri @non-ssl-redirect @unicorn;

location @unicorn {
    proxy_set_header  X-Real-IP       $remote_addr;
    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 /500.html;
client_max_body_size 5G;
keepalive_timeout 10;

ssl on;
ssl_certificate         /etc/nginx/ssl/server.crt;
ssl_certificate_key     /etc/nginx/ssl/server.key;
ssl_protocols           SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers             ALL:-ADH:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP;
ssl_session_cache       shared:SSL:10m;

send_timeout 240;
sendfile_max_chunk 5m;
}

【问题讨论】:

    标签: ruby-on-rails ssl nginx https asset-pipeline


    【解决方案1】:

    听起来您的资产主机配置硬连线到http。当您通过https 查看页面,但通过http 加载资产时,许多浏览器会阻止该资产或显示警告。

    解决这个问题的最简单方法是设置一个不包含协议的 Rails asset_host,它应该继承加载它的页面的协议。

    例如:

    # Use just the asset host domain name for Rails pages
    config.action_controller.asset_host = "assets.mycompany.com"
    # Specify HTTP for ActionMailer messages, since they don't have a protocol to inherit
    config.action_mailer.asset_host = "http://assets.mycompany.com"
    

    如果您使用https 协议正确地包含您的资产,但它们无法加载 - 您的资产的主机名和 SSL 证书之间可能存在 SSL 证书名称不匹配。例如,如果您使用自定义域名直接从 S3 提供资产,则 S3 SSL 证书 (*.s3.amazonaws.com) 将无法匹配 assets.yourcompany.com 并导致 SSL 错误,从而阻止加载资产。

    在这种情况下,唯一的解决方法是使用允许自定义 SSL 证书与您的主机名匹配的资产主机或 CDN,或恢复为与您的提供商 SSL 证书匹配的公共主机名。

    【讨论】:

    • 这也适用于我的 application.css 文件?
    • 我在上面添加了我的 nginx.conf 文件(有一些遗漏)。
    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2015-08-15
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多