NGINX proxy_pass remove path prefix & resolve DNS

后端 未结 1 938
执念已碎
执念已碎 2021-02-02 16:07

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;

locat         


        
相关标签:
1条回答
  • 2021-02-02 16:27

    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.

    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;
    }
    

    (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.)


    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.

    0 讨论(0)
提交回复
热议问题