ruby-on-rails - 使用 NGINX 和 Unicorn 在 Rails 3 应用程序中通过 SSL 提供 Assets 服务

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

我已经设置了我的 Rails 应用程序,效果很好。不幸的是,在该网站的 https://版本上,我的任何 Assets 均未得到服务...知道为什么会发生这种情况吗?所有 Assets 均通过 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;
}

最佳答案

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

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

例如:

# 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 协议(protocol)正确包含 Assets ,但它们无法加载 - Assets 的主机名和 SSL 证书之间可能存在 SSL 证书名称不匹配。例如,如果您使用自定义域名直接从 S3 提供 Assets ,则 S3 SSL 证书 (*.s3.amazonaws.com) 将无法匹配 assets.yourcompany.com 并导致SSL 错误,导致资源无法加载。

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

关于ruby-on-rails - 使用 NGINX 和 Unicorn 在 Rails 3 应用程序中通过 SSL 提供 Assets 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21119487/

相关文章:

ruby-on-rails - 如何将 Capistrano 用于非 Rails 应用程序?只是一个静态 html 站点

ssl - Letsencrypt + Docker + Nginx

java - 从java中的https-URL读取

nginx - Nginx 重写规则 - 在 url 上包含路径

javascript - SyntaxError : expected expression, 在 Heroku 上得到 '<'

nginx - 覆盖 Nginx Recipe default_enabled_site

ruby-on-rails - 如何创建指向其他页面的 will_paginate 链接?

ruby-on-rails - 由于关联模型的验证,Rspec 应该对 ActiveRecord 模型进行匹配器测试失败

mysql - 我的连接表似乎不起作用

ssl - 开发人员沙盒中的任何 Sonos 服务是否都需要 HTTPS?