2013-05-09 81 views
0

我試圖讓我的Rails網站爲http或https提供所有網頁和資源,但是發生的情況是當我以https模式進入時正在被重定向到http,並且資產從未作爲https協議提供。Haproxy + Nginx + Unicorn - 未提供使用SSL的靜態資產

我的nginx的配置是以下之一:

server { 
    listen <%= rubber_env.unicorn_listen_port %>; 
    listen 443 ssl; 

    ssl_certificate  /etc/ssl/certs/server.crt; 
    ssl_certificate_key /etc/ssl/private/server.key; 
    ssl_session_cache  shared:SSL:10m; 

    client_max_body_size 4G; 
    server_name <%= [ rubber_env.domain, rubber_env.web_aliases ].flatten.compact.join(" ") %>; 

    keepalive_timeout 5; 

    # Location of our static files 
    root <%= Rubber.root + "/public" %>; 

    location/{ 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 

    # If you don't find the filename in the static files 
    # Then request it from the unicorn server 
    if (!-f $request_filename) { 
     proxy_pass http://unicorn_server; 
     break; 
    } 
    } 

    location ~ ^/(assets)/ { 
    expires 1y; 
    add_header Cache-Control public; 

    add_header ETag ""; 
    break; 
    gzip_static on; # to serve pre-gzipped version 
    } 

    # this rewrites all the requests to the maintenance.html 
    # page if it exists in the doc root. This is for capistrano's 
    # disable web task 
    if (-f $document_root/system/maintenance.html) 
    { 
     rewrite ^(.*)$ /system/maintenance.html last; 
     break; 
    } 

    error_page 500 502 503 504 /500.html; 
    location = /500.html 
    { 
     root <%= Rubber.root + "/public" %>; 
    } 
    error_page 404 /404.html; 
    location = /404.html 
    { 
     root <%= Rubber.root + "/public" %>; 
    } 
} 

它會更好,如果我可以成爲nginx的靜態資產HTTPS或HTTP,但如果它是不可能的,我可以使用Rails爲他們服務並支付性能損失,因爲這隻會用在我們正在創建的小書籤中。

你知道如何讓這個nginx配置與ssl服務於資產一起工作嗎?

如果您需要我也可以添加獨角獸haproxy配置。

謝謝!

回答

0

我的解決方案是在任何情況下都接受ssl,並將資產作爲ssl資產提供。

<% 
    @path = "/etc/nginx/rubber/unicorn_nginx.conf" 
%> 


upstream unicorn_server { 
# This is the socket we configured in unicorn.rb 
server unix:/var/run/unicorn.sock 
fail_timeout=0; 
} 

server { 
    listen <%= rubber_env.unicorn_listen_port %>; 

    client_max_body_size 4G; 
    server_name <%= [ rubber_env.domain, rubber_env.web_aliases ].flatten.compact.join(" ") %>; 

    keepalive_timeout 5; 

    # Location of our static files 
    root <%= Rubber.root + "/public" %>; 

    location/{ 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 

    # If you don't find the filename in the static files 
    # Then request it from the unicorn server 
    if (!-f $request_filename) { 
     proxy_pass http://unicorn_server; 
     break; 
    } 
    } 

    location ~ ^/(assets)/ { 
    expires 1y; 
    add_header Cache-Control public; 

    add_header ETag ""; 
    break; 
    gzip_static on; # to serve pre-gzipped version 
    } 

    # this rewrites all the requests to the maintenance.html 
    # page if it exists in the doc root. This is for capistrano's 
    # disable web task 
    if (-f $document_root/system/maintenance.html) 
    { 
     rewrite ^(.*)$ /system/maintenance.html last; 
     break; 
    } 

    error_page 500 502 503 504 /500.html; 
    location = /500.html 
    { 
     root <%= Rubber.root + "/public" %>; 
    } 
    error_page 404 /404.html; 
    location = /404.html 
    { 
     root <%= Rubber.root + "/public" %>; 
    } 
} 

server { 
    listen 443 ssl; 

    ssl_certificate  /etc/ssl/certs/server.crt; 
    ssl_certificate_key /etc/ssl/private/server.pem; 
    ssl_session_cache  shared:SSL:10m; 

    client_max_body_size 4G; 
    server_name <%= [ rubber_env.domain, rubber_env.web_aliases ].flatten.compact.join(" ") %>; 

    keepalive_timeout 5; 

    # Location of our static files 
    root <%= Rubber.root + "/public" %>; 

    location/{ 
    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_server; 
    } 

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

    # this rewrites all the requests to the maintenance.html 
    # page if it exists in the doc root. This is for capistrano's 
    # disable web task 
    if (-f $document_root/system/maintenance.html) 
    { 
    rewrite ^(.*)$ /system/maintenance.html last; 
    break; 
    } 

    error_page 500 502 503 504 /500.html; 
    location = /500.html 
    { 
    root <%= Rubber.root + "/public" %>; 
    } 
    error_page 404 /404.html; 
    location = /404.html 
    { 
    root <%= Rubber.root + "/public" %>; 
    } 
}