NGINX: 405 Not Allowed

时间:2023-03-09 02:19:10
NGINX: 405 Not Allowed

近期做一个手机端静态网站,在wcm上网站预览的时候显示正常,网站数据发布到nginx网站服务上后,发现页面有部分不显示:

正常页面:

NGINX: 405 Not Allowed

错误页面:

NGINX: 405 Not Allowed

进入谷歌浏览器的Developer Tools(F12键),进入Network探测

NGINX: 405 Not Allowed

NGINX: 405 Not Allowed

NGINX: 405 Not Allowed

查到错误信息:Request Method:POST    Status Code:405 Not Allowed

网上传抄的添加以下配置的解决办法不可用:

error_page 405 =200 @405;
location @405
{
root /data/jcyweb/hnjcy;
}

成功解决问题的方法:

upstream static_backend {
server localhost:80;
} server {
listen 80; # ... error_page 405 =200 @405;
location @405 {
root /data/jcyweb/hnjcy;
proxy_method GET;
proxy_pass http://static_backend;
}
}

即转换静态文件接收的POST请求到GET方式

最后的配置文件:

#user  nobody;
worker_processes auto; ##允许Nginx进程生成的worker process数 根据cpu内核数自动生成
#error_log logs/error.log
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll; ##配置事件驱动模型
worker_connections ; ##配置最大连接数
} http {
include mime.types; ##定义MIME-Type
default_type application/octet-stream;
log_format main '$remote_addr - [$time_local] - "$request"' ##定义日志类型,远程地址、时间、请求内容、请求状态
' - $status';
types_hash_max_size ; ##多servername需设置
server_names_hash_bucket_size ;
sendfile on;
server_tokens off; ##关闭默认版本号
keepalive_timeout ; ##定义链接超时时间,防止慢速dos攻击
client_header_timeout ; ##请求头超时时间
client_body_timeout ; ##请求体超时时间
reset_timedout_connection on; ##关闭不响应的客户端连接,释放客户端占有内存
send_timeout ; ##客户端两次读取时间,如果这段时间里没有读取任何数据,nginx就会关闭连接 upstream static_backend {
server localhost:;
} server { ##直接访问IP 反馈500错误
listen ;
location / {
root /data/jcyweb/hnjcy;
index index.html;
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
} error_page /.html;
location = /.html {
root html;
} error_page /50x.html;
location = /50x.html {
root html;
} error_page = @;
location @{
root /data/jcyweb/hnjcy;
proxy_method GET;
proxy_pass http://static_backend;
}
} }

nginx.conf

还有一更好的方法:修改源代码,重新编译安装nginx 

编辑nginx源代码

[root@localhost ~]# vim src/http/modules/ngx_http_static_module.c

修改: 找到下面一段注释掉 

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

然后按照原来的编译参数,重新编译安装nginx,即可