Resolution of Nginx 405 errors

Source: Internet
Author: User
Tags error status code

The URL of the Access Server can be downloaded in the IE browser, but it cannot be tested on the client. A network error is prompted. on the client side, error 405 is returned, Which is returned by nginx. So, take the browser on my computer and enter the specified url. Then the client simulator can be opened, so I checked the logs and found the differences. The browser sends a GET request, while the client sends a POST request.

The log is as follows:

Xxx. xxx. xxx. xxx--[xx/xxx/2xxx: 14: 00: 00 + 0800] "POST/xxxxxxx. xxxx HTTP/1.1 "" 405 "173"-"" curl/7.12.1 (i686-redhat-linux-gnu) libcurl/7.12.1 OpenSSL/0.9.7a zlib/1.2.1.2 libidn/0.5.6 ""-"

The preceding xxx is the ip address of the request source, and the POST is the accessed file.

So I searched the internet and found the cause of a similar problem. The following describes how to solve this problem in Zhang Yan's blog. Below are some examples of this problem:

URL: http://blog.s135.com/read.php? 337

This is explained because only the GET method can be used to access static files, rather than POST. Therefore, in the nginx return information, after version 0.6.33, a 405 error status code is added, which means that you cannot use the POST method to request static files. If yes, you need to change the method.

The specific change log is displayed on the official site of nginx. The content is as follows:

URL: http://www.nginx.net/CHANGES-0.6

Changes with nginx 0.6.33 20 Nov 2008

*) Feature: now nginx returns the 405 status code for POST method
Requesting a static file only if the file exists.

However, we hope that the POST method can be supported, so in the configuration of instructor Zhang, 405 is redirected and 200 is returned, that is, if 405 is encountered, it will be treated as 200.

The specific configuration is as follows:

Error_page 405 = 200 @ 405;
Location @ 405
{
Root/opt/htdocs;
}

405-> 200 is redirected, and doc_root is specified for the error 405, which is the normal doc_root configuration.

As a result, after the above configuration, it was found that it did not take effect, or 405 was returned. At that time, it was a point in the world, and I thought it was a problem with nginx, so I changed to the earlier version of 0.6.35, no, two methods are available for random internet access.

1. Use the post_to_static patch, but I really don't know how to apply this patch, and finally I crashed .. Otherwise, recompile an nginx file to support the post_to_static configuration item. You can select on/off to enable POST requests to access static pages.

You can refer to the following urls:

Http://www.ruby-forum.com/topic/142663there is a patch written by C.

Http://bbs3.chinaunix.net/viewthread.php? Tid = 1351579 is the same as that of instructor Zhang, But I explained why rewrite is necessary.

2. Second, use proxy_pass for proxy access. However, after I redirected to 127.0.0.1: port/, the error 502 still occurred.

The url is as follows.

3. The third type is a good way of thinking, but it does not work. You may need to modify it.

# Location ~ (. Xxx1 $ |. xxx2 $ ){
# Root/home/xxxxx/htdocs;
# Index index.html index.htm;

# If ($ request_method! = GET ){
# Set $ request_method GET;
#}
# Fastcgi_param REQUEST_METHOD GET;
#}

Here, we want to assign the $ requset_method variable to GET if the access request is not GET when accessing a file that matches our rules, however, fcgi is not used here, and the following sentence may not take effect. If two $ requset_method statements appear at the same time, the configuration file will prompt a syntax error and the variable is repeated, here I think if the url request comes over, the variable here may not be changed, and the value can no longer be re-assigned. But why can rul rewrite it? This is a question.

The above methods did not solve the problem, and finally let Han brother give a random test, Here despise him .. It seems that we sometimes do not have to be restricted by rules or be bound by existing success stories.

The specific configuration is as follows:

Location ^ ~ /Xxxx. xxx1 {
Root/home/xxxxxx/htdocs;
Error_page 405 = 200/xxxx. xxx1;
}

Location ^ ~ /Xxxx. xxx2 {
Root/home/xxxxxx2/htdocs;
Error_page 405 = 200/xxxx. xxx2;
}

Why does error_page take effect internally rather than on the server.

In addition, the configuration here is not flexible, but it is indeed because there are two files that are configured separately, directly specifying where to download, and even the redirected root is directly a file.

======================
My Successful Configuration case is as follows: location/
{
Root/www/client/domain;
Index index.shtml index.html;
Error_page 405 = 200/index.shtml;
}
Or
Location ~ */* \. Shtml $
{
Root/www/client/$ domain;
Index index.shtml index.html;
Error_page 405 = 200 http: // $ domain $ request_uri;

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.