1
當給定的路徑不存在時,是否可以告訴nginx一個替代位置?我想從我的Rails應用程序提供靜態資產,但是有時編譯的資產可能不可用,我希望有一個回退。nginx緩存未命中行爲
production.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;
}
UPDATE: 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;
}
是啊,感謝反向代理請求到Rails應用程序。我已經提出了這個問題,請看看它是否有意義。它應該是服務器作爲靜態內容,從緩存或最後的手段是真正的Rails應用程序。 – Tombart
更新了我的答案,以反映發佈的配置 – cobaco
謝謝!現在它終於似乎工作 – Tombart