Nginx proxy_pass 然后 try_file [英] Nginx proxy_pass then try_file
问题描述
我正在使用前面的 nginx 设置 Rails 应用.
I am setting up a Rails app with nginx in front.
我想要的是首先检查 URL 是否对 Rails 有意义然后提供公共文件夹的内容.
What I want is first to check if the URL makes sense for Rails then serve content of the public folder.
我无法做到这一点:
upstream extranet {
server localhost:3000;
}
server {
location / {
try_files @extranet $uri;
root /var/www/extranet/public;
}
location @extranet {
proxy_pass http://extranet;
proxy_read_timeout 90;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;
client_max_body_size 100m;
}
}
我得到:*1 rewrite or internal redirection cycle while internal redirect to "/"
error.
I get: *1 rewrite or internal redirection cycle while internally redirecting to "/"
error.
看起来 try_files $uri @extranet;
有效,但在我的情况下,首先检查 Rails 应用感觉更安全,因为公共文件夹可能会更改.
It seems like try_files $uri @extranet;
works but in my case it feels safer to check the Rails app first because the public folder might change.
推荐答案
try_files
检查本地文件系统上是否存在文件,并且无法响应来自代理的响应代码.
try_files
checks for the presence of a file on the local file system and cannot respond to the response code from a proxy.
推测,如果远程页面不存在,代理响应带有 404 响应,可以通过 error_page
语句拦截.
Presumably, the proxy response with a 404 response if the remote page does not exist, which can be intercepted by an error_page
statement.
例如:
location / {
proxy_pass http://extranet;
proxy_set_header ...
proxy_set_header ...
proxy_set_header ...
proxy_intercept_errors on;
error_page 404 = @fallback;
}
location @fallback {
root /var/www/extranet/public;
try_files $uri =404;
}
有关更多信息,请参阅本文档.
See this document for more.
这篇关于Nginx proxy_pass 然后 try_file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!