NGINX proxy_pass删除路径前缀&解析DNS [英] NGINX proxy_pass remove path prefix & resolve DNS
问题描述
我想使用proxy_pass将请求代理到另一台服务器,同时删除匹配的路径前缀.我相信这样做的一种方法如下;
I'd like to proxy a request to another server using proxy_pass while removing the matched path prefix. I believe that one way of doing this is as follows;
location /a/ {
proxy_pass https://website.com/
}
例如对http://localhost/a/b.html
的请求将被代理到https://website.com/b.html
.
E.g. a request to http://localhost/a/b.html
would be proxied to https://website.com/b.html
.
据我所知,NGINX上非商业版本中的问题是website.com
的DNS A记录将在启动时永久加载和缓存.我已经看到一种通过在proxy_pass指令中使用诸如$request_uri
之类的变量来解决此问题的技术,从而迫使NIGNX根据记录的TTL重新解析DNS.
As far as I am aware the issue with this in non-commercial versions on NGINX is that the DNS A record for website.com
would be loaded and cached forever on startup. I've seen a technique to workaround this by using a variable such as $request_uri
in the proxy_pass directive, thus forcing NGINX to re-resolve the DNS according to the TTL of the record.
例如
location /a/ {
rewrite ^/a/(.*) /$1 break;
proxy_pass https://website.com/$request_uri
}
不幸的是,上述方法似乎无效,因为它似乎仍将/a/前缀传递给上游.
Unfortunately, it seems that the above doesn't work as it seems to still pass the /a/ prefix to the upstream.
基本上,我想在这里实现的是代理请求,同时删除路径前缀,这样就不会永远缓存DNS记录.
Essentially all I want to achieve here is to proxy a request while removing the path prefix in such a way that DNS records are not cached forever.
谢谢.
推荐答案
我不确定您在哪里看到过它,但是仅专门使用$request_uri
肯定不会神奇地使nginx为您解析域名动态地
I'm not sure where you've seen it, but just using specifically $request_uri
is certainly not going to magically make nginx resolve the domain names for you dynamically.
假设所建议的是明确使用变量,例如$uri
(这是一个不同的变量),前提是假设使用变量时,则每次都会单独解析域名,而无需任何缓存?我不确认或否认这种假设是否正确,但是以下内容至少可以为您消除/a
.
Perhaps what was suggested was explicitly using the variables, such as $uri
(which is a different variable), on the assumption that when the variables are in use, then the domain name is resolved individually each time, without any caching? I don't confirm or deny whether such assumption is correct, but the following will at least get rid of /a
for you.
location /a/ {
rewrite ^/a/(.*) /$1 break;
proxy_pass https://website.com/$uri$is_args$args;
}
(请注意,如果确实实现了不缓存域名的功能,那么您可能还希望运行本地解析器,否则,托管服务提供商的DNS的额外延迟和停机时间将立即影响您的网站,更不用说了其服务器可能的DNS查询限制.)
(Note that if it's indeed implemented not to cache the domain name, then you might as well want to run a local resolver, otherwise, the extra latency and downtime of your hosting provider's DNS will immediately affect your site, not to mention the possible DNS query limits of their servers.)
也许更好的解决方案是定期重新启动nginx以自动获取DNS中的更改?例如nginx -s reload
或 kill -HUP
?如 http://nginx.org/en/docs/beginners_guide.html#control 和 http://nginx.org/en/docs/control.html#reconfiguration ,nginx永远不会停止处理任何在重新加载期间提出要求,因此应该是安全的操作;并且很可能也会导致DNS被刷新.
Perhaps a better solution would be to periodically restart nginx to automatically pick up the changes in DNS? E.g., nginx -s reload
or kill -HUP
? As explained in http://nginx.org/en/docs/beginners_guide.html#control and http://nginx.org/en/docs/control.html#reconfiguration, nginx never stops processing any requests during reload, so it should be a safe operation; and it'll most likely result in DNS being flushed, too.
这篇关于NGINX proxy_pass删除路径前缀&解析DNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!