Nginx Reverse Proxy 405 PUT Chrome

Viewed 5975

I'm getting a 405 response when I try to request to my Tomcat server through an Nginx reverse proxy.

If I request directly to the tomcat server it works successfully from Chrome or Firefox. But if I request to Nginx Proxy I get a 405 from Chrome, but works from Firefox.

I've been testing with different configuration in my nginx.conf under location/foo/ { ... }

I've tried:

error_page 405 =200 $uri;

Also

 add_header "Allow" "GET, POST, HEAD, PUT, DELETE" always;
    add_header "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS" always;`

location / {
    dav_methods PUT DELETE;
    proxy_pass http://csprocure;
}

My nginx.conf for redirections is as follows:

upstream serverS {
   server xxx.xxx.xxx.xxx:yyyy;
}

server {

    listen 80;
    server_name $hostname;
    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;
    error_log   /dev/stdout info;
    access_log  /dev/stdout;

    location /foo/ {
        proxy_pass         http://serverS;
        proxy_redirect     http://xxx.xxx.xxx.xxx/ http://xxx.xxx.xxx.xxx:yyyy/;
        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-Host $server_name;
        proxy_set_header   X-Forwarded-Proto: https;
    }

What can I add to my configuration to avoid the 405 in Chrome?

2 Answers

I've been comparing request between Firefox and Chrome and there is a difference in the Chrome side, in request Chrome add an "Origin" parameter in the header request that is making the error.

So in my nginx.conf I've added the parameter to avoid it under location/

proxy_set_header Origin "";

So my location configuration block now is:

location /foo/ {
    proxy_set_header Origin "";
    proxy_pass         http://serverS;
    proxy_redirect     http://xxx.xxx.xxx.xxx/ http://xxx.xxx.xxx.xxx:yyyy/;
    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-Host $server_name;
    proxy_set_header   X-Forwarded-Proto: https;
}

A good explanation online for whitelisting HTTP methods can be found here

This works for me:

add_header Allow "GET, POST, HEAD, PUT, DELETE" always;

if ( $request_method !~ ^(GET|POST|HEAD|PUT|DELETE)$ ) {
   return 405;
}

..and obviously, make sure you check the config nginx -t and reload config nginx -s reload before testing changes

Related