当给定路径不存在时,是否可以告诉 nginx 另一个位置?我想从我的 Rails 应用程序中提供静态资产,但是有时编译的资产可能不可用,我想有一个后备。
生产.rb
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
nginx.conf:
location ~ ^/assets/ {
expires max;
add_header Cache-Control public;
add_header ETag "";
break;
}
更新: nginx.conf
#cache server
server {
listen 80;
# serving compressed assets
location ~ ^/(assets)/ {
root /var/app/current/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
add_header ETag "";
}
try_files $uri /maintenance.html @cache;
location @cache {
proxy_redirect off;
proxy_pass_header Cookie;
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache one;
proxy_cache_key app$request_uri;
proxy_cache_valid 200 302 5s;
proxy_cache_valid 404 1m;
proxy_pass http://127.0.0.1:81;
}
}
#real rails backend
server {
listen 81;
root /var/app/current/public;
error_log /var/app/current/log/error.log;
rails_env production;
passenger_enabled on;
passenger_use_global_queue on;
}