【发布时间】:2013-12-19 05:01:18
【问题描述】:
我在 Rails 4 上使用页面缓存 gem。我有一个运行 nginx 的 Web 服务器,运行 unicorn 和 rails 的应用服务器,以及运行 postgre 的 db 服务器。
在应用服务器上生成页面缓存时,nginx 不会提供静态文件。只有在我设置之后
config.serve_static_assets = true
页面缓存在生产环境中的作用。我认为这并不理想,尽管现在 rails 正在为静态文件提供服务。
如何让 nginx 为位于应用服务器上的页面缓存提供服务?
这是我的 nginx 配置:
upstream unicorn {
server <%= app_private_ip %>:8080 fail_timeout=0;
}
server {
# listen [::]:80 ipv6only=on default_server;
listen 80 default deferred;
server_name <%= domain %>;
# rewrite ^(.*) https://<%= domain %>$1 permanent;
root <%= current_path %>/public;
sendfile on;
if (-f $document_root/system/maintenance.html) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
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-For $proxy_add_x_forwarded_for;
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 4G;
keepalive_timeout 10;
server_tokens off;
}
server {
listen 443;
server_name <%= domain %>;
ssl on;
ssl_certificate /home/<%= user %>/ssl/<%= domain %>.pem;
ssl_certificate_key /home/<%= user %>/ssl/<%= domain %>.key;
root <%= current_path %>/public;
sendfile on;
if (-f $document_root/system/maintenance.html) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
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-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 4G;
keepalive_timeout 10;
server_tokens off;
}
【问题讨论】:
标签: ruby-on-rails caching nginx unicorn