我假设您的意思是我如何从 nginx 而不是 Unicorn 提供我的静态资产
我刚刚解决了这个问题,这是我的一个片段nginx.conf
# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
try_files $uri/index.html $uri.html $uri @app;
# Set Far Future Cache on Static Assets
# All requests starting with /xyz/ where xyz is
# one of the options below (~* == case insensitive)
location ~* ^/(images|javascripts|stylesheets)/ {
# Per RFC2616 - 1 year maximum expiry
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
location @app { ... }
我正在使用 Rails 3.0.10,所以你需要类似的东西^/assets/
。该~*
指令告诉 nginx 进行不区分大小写的正则表达式比较。此外,您不需要像在其他语言中那样转义反斜杠。
这里是 Nginx 文档:http ://wiki.nginx.org/HttpCoreModule#location