Search code examples
regexnginxproxypass

Nginx location doesn't match


I have an url like this: https://www.vagrant.something.com/cdn/util/thumb/100/100/dev-profile-images/444ba5eacba004cb31dabbb5d5cda3ce9b2c84f4.jpg and I have a nginx configuration with location like this:

location /cdn/ {
  proxy_pass http://cdn-vagrant.something.com/;
}

My problem is that it should execute the proxy_pass to get back the image, but location doesn't match with the URL, what is the problem? If I am using this:

location / {
  proxy_pass http://cdn-vagrant.something.com/;
}

And open the link it is working correctly, but in this case it proxy_pass the all request.


Solution

  • Remove the trailing slash:

    location /cdn/ {
      proxy_pass http://cdn-vagrant.something.com;
    }
    

    That way request uri will be passed to backend as is.

    When proxy_pass contains path (even a single /), that path replaces the location path (/cdn/ in your case) in proxied request.

    UPD:
    Your backend (cdn-vagrant.something.com) is configured to return permanent redirects for /cdn/ -> /. That means your proxy_pass directive was correct, you should pass /cdn/ requests to / on backend. This should work:

    location /cdn/ {
      proxy_pass http://cdn-vagrant.something.com/;
    }
    

    The only thing that I'm not sure about is those if blocks in your www.vagrant.something.com config. Try testing without them, because if is evil. And you should avoid blocks like if (-f $request_filename) and if (!-e $request_filename), maybe use try_files instead.