POST request not allowed - 405 Not Allowed - nginx, even with headers included

后端 未结 6 1117
孤独总比滥情好
孤独总比滥情好 2020-11-29 20:23

I have a problem in trying to do a POST request in my application and I searched a lot, but I did not find the solution.

So, I have a nodeJS application and a websi

相关标签:
6条回答
  • 2020-11-29 20:49

    I had similiar issue but only with Chrome, Firefox was working. I noticed that Chrome was adding an Origin parameter in the header request.

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

    proxy_set_header Origin "";
    
    0 讨论(0)
  • 2020-11-29 20:56

    In my case it was POST submission of a json to be processed and get a return value. I cross checked logs of my app server with and without nginx. What i got was my location was not getting appended to proxy_pass url and the version of HTTP protocol version is different.

    • Without nginx: "POST /xxQuery HTTP/1.1" 200 -
    • With nginx: "POST / HTTP/1.0" 405 -

    My earlier location block was

    location /xxQuery {
        proxy_method POST;
        proxy_pass http://127.0.0.1:xx00/;
        client_max_body_size 10M;
    }
    

    I changed it to

    location /xxQuery {
        proxy_method POST;
        proxy_http_version 1.1;
        proxy_pass http://127.0.0.1:xx00/xxQuery;
        client_max_body_size 10M;
    }
    

    It worked.

    0 讨论(0)
  • 2020-11-29 20:57

    I noticed this wasn't working with a static-first-then-reverse-proxy setup. Here's what that looks like:

    location @app {
      proxy_pass http://localhost:3000$request_uri;
    }
    
    location / {
      try_files $uri $uri/ @app;
      error_page 405 @app;
    }
    
    0 讨论(0)
  • 2020-11-29 21:08

    I have tried the solution which redirects 405 to 200, and in production environment(in my case, it's Google Load Balancing with Nginx Docker container), this hack causes some 502 errors(Google Load Balancing error code: backend_early_response_with_non_error_status).

    In the end, I have made this work properly by replacing Nginx with OpenResty which is completely compatible with Nginx and have more plugins.

    With ngx_coolkit, Now Nginx(OpenResty) could serve static files with POST request properly, here is the config file in my case:

    server {
      listen 80;
    
      location / {
        override_method GET;
        proxy_pass http://127.0.0.1:8080;
      }
    }
    
    server {
      listen 8080;
      location / {
        root /var/www/web-static;
        index index.html;
        add_header Cache-Control no-cache;
      }
    }
    

    In the above config, I use override_method offered by ngx_coolkit to override the HTTP Method to GET.

    0 讨论(0)
  • 2020-11-29 21:10

    This configuration to your nginx.conf should help you.

    https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb

    server {
        listen       80;
        server_name  localhost;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        error_page  404     /404.html;
        error_page  403     /403.html;
    
        # To allow POST on static pages
        error_page  405     =200 $uri;
    
        # ...
    }
    
    0 讨论(0)
  • 2020-11-29 21:10

    This is the real proxy redirection to the intended server.

    server {
      listen          80;
      server_name     localhost;
    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
        proxy_pass http://xx.xxx.xxx.xxx/;
        proxy_redirect off;
        proxy_set_header Host $host;
    
      }
    }
    
    0 讨论(0)
提交回复
热议问题