我有一个 NGINX 服务器,用于为两个 Ghost 博客站点提供反向代理服务。对于静态内容,我希望它跳过node
服务并直接由 NGINX 提供。应该很直截了当,但有一个转折。
这是一个配置块,它设置反向代理,然后为静态内容提供例外。这很好用(注意:双括号引用替换为我正在使用的两个不同 Ghost 服务的名称):
location / {
proxy_redirect off;
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 $scheme;
proxy_set_header Host $http_host:$server_port;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache ghost_{{slug}}_cache;
proxy_cache_key ghost$request_uri$scheme;
proxy_pass http://ghost_{{slug}}_upstream;
}
location /assets {
root {{assets_root}};
}
因此,在/assets
类似请求的情况下,http://example.com/assets/js/doit.js
上述配置将匹配/assets
,然后将“assets/js/doit.js”附加到上面定义的根目录。正如我所说,这部分工作正常,因为我指向的位置具有文件系统中表示的完整 URL 路径(例如,有一个assets/js
包含文件的目录doit.js
)。
我正在努力解决的问题——我对 NGINX 很陌生,所以怀疑这很容易——类似的情况是你有这样的配置块:
location /content {
root {{content_root}};
}
此块 - 从配置和执行的角度来看 - 与assets
块相同,但在这种情况下,我希望它只传递 URL 中跟在 内容后面的部分。因此,如果请求是http://example.com/content/images/july/foo.jpg
那么返回的是驻留在文件系统上的任何内容:
- {{content_root}} +
/images/july/foo.jpg
而不是我现在得到的是:
- {{content_root}} +内容+
/images/july/foo.jpg