Nginx- 405不允许- fastcgi超时

5
我正在尝试使用php 5.6和nginx在Synology上设置网站。该网站是WordPress和一个主题。在处理演示导入时,我们遇到了NGINX错误405(不允许)。这有点令人沮丧,因为我喜欢事情被正确地完成。我已经查看了php.ini文件和nginx.conf文件。
# Copyright (c) 2000-2016 Synology Inc. All rights reserved.

worker_processes        auto;
#worker_cpu_affinity    auto;
worker_rlimit_nofile    65535;

include conf.d/main.conf;

events {
    use             epoll;
    multi_accept    on;
    worker_connections 1024;

    include conf.d/events.conf;
}

http {
    include         mime.types;
    default_type    application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  off;
    #access_log syslog:server=unix:/dev/log,facility=local7,tag=nginx_access,nohostname main;
    error_log   syslog:server=unix:/dev/log,facility=local7,tag=nginx_error,nohostname error;

    sendfile        on;
    server_tokens   off;

    proxy_request_buffering     off;
    fastcgi_request_buffering   off;
    scgi_request_buffering      off;

    proxy_buffering     off;
    fastcgi_buffering   off;
    scgi_buffering      off;

    resolver_timeout              5s;
    client_header_timeout         10s;
    client_body_timeout           60s;
    send_timeout                  60s;
    keepalive_timeout             65s 20s;
    client_max_body_size          0;
    server_names_hash_max_size    8192;

    ssl_certificate           /usr/syno/etc/certificate/system/default/fullchain.pem;
    ssl_certificate_key       /usr/syno/etc/certificate/system/default/privkey.pem;
    ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers               ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA;
    ssl_dhparam               /usr/syno/etc/ssl/dh2048.pem;
    ssl_prefer_server_ciphers on;

    gzip_disable    "msie6";
    gzip_min_length 1000;
    gzip_types      text/plain text/css application/javascript application/json;
    gzip_vary       on;
    gzip_static     on;

    upstream synoscgi {
        server unix:/run/synoscgi.sock;
    }

    index index.html index.htm index.php;

    set_real_ip_from 127.0.0.1;
    real_ip_header X-Real-IP;

    server {
        listen 5000 default_server;
        listen [::]:5000 default_server;

        server_name _;

        gzip on;

        include app.d/alias.*.conf;
        root /usr/syno/synoman;
        index index.cgi;

        ignore_invalid_headers off;

        include app.d/dsm.*.conf;
        include /usr/syno/share/nginx/conf.d/dsm.*.conf;
        include conf.d/dsm.*.conf;

        location = / {
            try_files $uri /index.cgi$is_args$query_string;
        }

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        location ~ /webman/modules/(PersonalSettings|ExternalDevices|FileBrowser)/index_ds.php$ {
            alias /usr/syno/share/OAuth/index_ds.php;
            default_type text/html;
        }

        location ~ \.cgi {
            include             scgi_params;
            scgi_read_timeout   3600s;
            scgi_pass           synoscgi;
        }

        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ~ ^/webman/modules/Indexer/ {
            deny all;
        }

        location ~ ^/webapi/lib/ {
            deny all;
        }

        location ~ ^/webapi/(:?(:?.*)\.lib|(:?.*)\.api|(:?.*)\.auth|lib.def)$ {
            deny all;
        }

        location ~ /\. { access_log off; log_not_found off; deny all; }

        location ~* \.(?:js|css|png|jpg|gif|ico)$ {
            access_log off;
            log_not_found off;
        }

        location = /favicon.ico {
            access_log off;
            log_not_found off;
        }

        location = /robots.txt {
            allow all;
            access_log off;
            log_not_found off;
        }

    }

    server {
        listen 5001 default_server ssl;
        listen [::]:5001 default_server ssl;

        server_name _;

        include app.d/alias.*.conf;
        root /usr/syno/synoman;
        index index.cgi;

        ignore_invalid_headers off;

        include app.d/dsm.*.conf;
        include /usr/syno/share/nginx/conf.d/dsm.*.conf;
        include conf.d/dsm.*.conf;

        location = / {
            try_files $uri /index.cgi$is_args$query_string;
        }

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        location ~ /webman/modules/(PersonalSettings|ExternalDevices|FileBrowser)/index_ds.php$ {
            alias /usr/syno/share/OAuth/index_ds.php;
            default_type text/html;
        }

        location ~ \.cgi {
            include             scgi_params;
            scgi_read_timeout   3600s;
            scgi_pass           synoscgi;
        }

        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ~ ^/webman/modules/Indexer/ {
            deny all;
        }

        location ~ ^/webapi/lib/ {
            deny all;
        }

        location ~ ^/webapi/(:?(:?.*)\.lib|(:?.*)\.api|(:?.*)\.auth|lib.def)$ {
            deny all;
        }

        location ~ /\. { access_log off; log_not_found off; deny all; }

        location ~* \.(?:js|css|png|jpg|gif|ico)$ {
            access_log off;
            log_not_found off;
        }

        location = /favicon.ico {
            access_log off;
            log_not_found off;
        }

        location = /robots.txt {
            allow all;
            access_log off;
            log_not_found off;
        }

    }

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        listen 443 default_server ssl;
        listen [::]:443 default_server ssl;

        server_name _;

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        include app.d/www.*.conf;
        include app.d/alias.*.conf;
        include /usr/syno/share/nginx/conf.d/www.*.conf;
        include conf.d/www.*.conf;

        location = /webman/pingpong.php {
            rewrite /webman/pingpong.php /webman/pingpong.cgi break;

            root /usr/syno/synoman;
            include scgi_params;
            scgi_pass synoscgi;
        }

        location = /webdefault/images/logo.jpg {
            alias /usr/syno/share/nginx/logo.jpg;
        }

        error_page 405 =200 $uri;

        location ~* \.php$ {
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_read_timeout 240;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }


        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ^~ /.well-known/acme-challenge {
            root /var/lib/letsencrypt;
            default_type text/plain;
        }

        include app.d/.location.webstation.conf*;

        location ~ ^/$ {

            if ($scheme = https) {
                rewrite / https://$host:5001/ redirect;
            }

            rewrite / http://$host:5000/ redirect;

        }
    }

    include conf.d/http.*.conf;
    include app.d/server.*.conf;
    include sites-enabled/*;
}

我在互联网上搜索了很多,目前只找到一些有趣的线索但都没有起作用。 我尝试将以下内容添加到nginx.conf文件中(这就是为什么我的实际nginx.conf文件中有这些行),但它并没有解决我的问题。
location ~* \.php$ {
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_read_timeout 240;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

以下是错误日志信息:

2017/02/10 18:14:07 [error] 18555#18555: *2563 在从上游服务器接收响应头时,上游服务器超时 (110: Connection timed out),客户端: xxx.xxx.xxx.xxx, 服务器: example.com, 请求: "POST /wp-admin/admin-ajax.php HTTP/1.1", 上游服务器: "fastcgi://unix:/run/php-fpm/php56-fpm.sock", 主机: "www.example.com", 引用地址: http://example.com/wp-admin/admin.php?page=laborator-demo-content-installer&install-pack=agency&

如果您有解决问题的想法...因为我已经挣扎了几个星期了... 非常感谢。


你解决了这个问题吗?我也遇到了同样的问题,在搜索了一周左右后,最终来到了这里... - LukeP
@LukeP请看我的回答。如果还是不行,请发布您的error_page指令的要点,因为它们对此至关重要。 - mknecht
@mknecht 谢谢。最终我解决了问题。我不记得具体是什么问题,但它与我的环境有关。我在 Synology NAS 上运行 ResourceSpace DAM,并且配置由 Synology DSM 管理,每次重新启动 Web 服务器以应用更改时都会替换我所做的配置更改。 - LukeP
1个回答

4

tl;dr- 你的WordPress太慢了。跳到底部看看如何使NGINX返回正确的504 GATEWAY TIMEOUT状态码。

更详细的解释:你有一堆不相关的问题同时作用,导致效率低下。

1. WordPress,你的上游服务器,响应速度不够快

它需要超过4分钟,这就是为什么你在日志中看到110: Connection timed out解决慢速的方法是加快WordPress的速度。作为一种解决方法,你可以给它更多时间来处理请求。要做到这一点,增加fastcgi_read_timeout 240;规则中的数字。注意,超时时间以秒为单位,因此如果你愿意等待10分钟,将其设置为600

我建议不要增加超时时间。你真的应该解决性能问题本身。这样长时间的请求会阻塞NGINX和WordPress中的资源,因此即使是你自己,也很容易被DDoS攻击。

由于你的上游花费时间太长,NGINX将响应504 GATEWAY TIMEOUT。但它无法响应,因为……

2. POST请求无法用静态文件响应

在你的error_page位置,你告诉NGINX使用静态文件来处理请求。对于GETHEAD是没问题的,但对于POST不起作用,因为它会要求NGINX覆盖/创建文件。这既不是NGINX预期的,也不受其支持。(使用其他修改动词如PUTDELETE也会因同样的原因失败。)

请注意,你正在使用一个命名位置@error_page,其方法仍然是POST,正如手册所说

If there is no need to change URI and method during internal redirection it is possible to pass error processing into a named location.

您已经知道这个部分,这就是为什么您添加了error_page 405 =200 $uri;规则。不幸的是,这并没有救你,因为...
3. 默认情况下,内部 error_page 重定向不是递归的
据我所见,这在error_page手册的文档中没有提到,但它在以下指令的文档中提到:
解决方法:启用递归的 error_page 重定向
指令 recursive_error_pages 允许您处理在处理先前错误时发生的错误。来自文档的内容:
 Enables or disables doing several redirects using the error_page directive. The number of such redirects is limited.

如果您启用此功能,将 recursive_error_pages on; 放入您的服务器块中,您将允许 error_page 405 指令生效。
不幸的是,由于您仍在使用 $uri 部分请求相同的资源,WordPress 将再次查询相同的 URL,并使用 GET 请求。我不知道您的 WordPress 如何处理它,但很有可能此时发生的错误无法帮助您调试此问题。
这实际上只是绕过问题;您应该得到原始的 504。因此,我建议您不要启用递归错误页面重定向,而应采取以下措施: 解决方案:使用 GET 获取您的错误页面 我假设您仍然希望在POST请求时返回error.html。为此,您需要强制NGINX放弃POST并使用GET处理错误,以便可以使用静态文件。我发现实现这一点的唯一方法是不使用命名位置(以@开头的位置),而是改用internal位置。
要使用此选项,请更改您的@error_page位置以包括伪路径和internal指令,例如:
    location /error_page {
        internal;
        root /usr/syno/share/nginx;
        rewrite (.*) /error.html break;
    }

之后,修改error_page指令,使用新的位置:
    error_page 403 404 500 502 503 504 /error_page;

资源:NGINX邮件列表上的有用讨论。


我在这篇博客文章中总结了关于error_page指令的注意事项,以及我遇到的其他问题,希望对某些人(也许是我未来的自己)有所帮助:http://muratknecht.de/tech/why-nginx-returns-405-post-504-gateway-timeout-gotchas-error-page/ - mknecht

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接