Nginx static resource POST request returns status 405, nginx405

Source: Internet
Author: User

Nginx static resource POST request returns status 405, nginx405

The colleague gave a json request. In the HTTP interface testing tool, the post request result returns the 405 status, and the get request returns the data. I found that 405 is returned because most web servers, such as Apache, IIS, and Nginx, do not allow static files to respond to POST requests.
I tried several methods on the Internet. The following method is used to convert the POST request received by static files to the GET method through upstream.

    upstream static_resource {               server localhost:80;         }    server {        listen       80;        server_name app.test.com;        root   /home/app.test.com/;         location / {            root   /home/app.test.com/;            index  index.shtml index.html;        }        error_page 405 =200 @405;         location @405         {             root /home/app.test.com/;            proxy_method GET;             proxy_pass http://static_resource;          }   

Again, I tested the HTTP interface test tool and found that 405 was changed to 404. I thought for a moment because multiple virtual hosts were configured on the web server to listen on port 80, upstream accesses the first virtual host in the configuration file by default, instead of the app.test.com of the post request. Therefore, it tries to define a virtual host to listen to port 81 and point to app.test.com.

server {        listen       81;        server_name app.test.com;        root   /home/app.test.com/;        location / {            root   /home/app.test.com/;                 }    }

Modify the server in upstream at the same time, and change the returned status of the second request from 405 to 200.

 upstream static_resource {               server localhost:81;         }

The following is an example of the network transfer method. This method is invalid after I configure it.

   error_page   405 =200 @405;   location @405   {       root  /home/app.test.com;   }    

After this method is configured, the returned status changes from 405 to 302.

  location / {            root   /home/phone_app/;            error_page 405 =200 http://$host$request_uri;            index  index.shtml index.html;        }

This method has not been tried
Modify src/http/modules/ngx_http_static_module.c in the nginx source code directory, comment out the following code, recompile and install nginx according to the original compilation Parameters

/*    if (r->method & NGX_HTTP_POST) {        return NGX_HTTP_NOT_ALLOWED;       }*/

Or do not execute make install according to the original compilation parameter./configuer make

cp  $nginx_dir/sbin/nginx  $nginx_dir/sbin/nginx.bakcp ./objs/nginx   $nginx_dir/sbin/nginx$nginx_dir/sbin/nginx -s reload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.