Nginx – How to PUT to Static File

nginx

I am trying to figure out how to allow PUT to a static file in nginx, the put request will be proxied to a php script. An example of the request I am trying to get working using curl is:

curl -X PUT  -d "tags=abc,def" cp.local/api/Image/myimage.jpg

This throws a 405 Not Allowed in nginx. Other urls work, for example:

curl -X PUT  -d "tags=abc,def" cp.local/api/Image/myimage.pdf

I read a forum thread where someone had a similar problem but with POST but it is outdated and there didn't seem to be a real fix in the thread.

The relevant nginx config directive is:

location ~* ^/api/.*$ {
    root           /my/path/public;
    fastcgi_param  SCRIPT_FILENAME  /my/path/public/Api.php;
    fastcgi_pass   127.0.0.1:9000;
    include        fastcgi_params;
}

Best Answer

You cannot do anything but GET a static file. If you want to POST, PUT, DELETE etc you need to make sure your have a location block handling the URI and that this location block handles the request in a way that allows such requests.

For instance by proxy_passing to Apache or fastcgi_passing to PHP.

With the config you have provided it should theoretically be allowed. But you've removed a lot of information and most likely you have a location block to catch static files. When you're dealing with regex locations the first matching one will be used so you need to make sure your api location is defined before any other matching locations.