Nginx – How to use nginx reverse_proxy for PUT requests

http-methodnginxreverse-proxy

When I use nginx as reverse proxy in front of some other web application, it seems not to forward PUT requests, but shows an HTTP 405 generated by nginx (and not the upstream server).

I tried the proxy_next_upstream method for http_405, but it did not work. I wonder why nginx itself checks the HTTP method anyway for a location block which has a reverse_proxy configured.

Best Answer

Nginx is not the problem here. It forwards PUT requests in reverse proxy blocks as expected, if the location matches the request.

I had another location directive, which makes sure images are not served via the reverse proxy. It matched everything with .png (and some other file extensions) in the end, which matched the upload-urls as well.

For the wrong location block, the error 405 is correct. The solution is to make sure the upload requests are actually forwarded into the reverse proxy.


An example for a working reverse proxy configuration:

# proxy requests for /upload the a webapp, which implements PUT
location ^~ /upload {
    proxy_pass "https://backendserver:1234/something";
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    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_buffering off;
    tcp_nodelay on;
}

The configuration works fine, if there are no other location blocks, which take precedence.
My problem was another block:

# This block matched requests for /upload/somefile.png before the proxy block
location ~* ^(/.*png|/.*jpg|/.*jpeg|/.*gif)$ {
    # some directives without proxy_pass
}