Nginx和PHP-FPM 502错误

4

我寻找了许多解决方案,但都没有帮助我解决问题。在/var/log/nginx/error.log中显示的错误如下:

2017/04/21 16:08:16 [error] 29233#0: *319 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: ..., server: ..., request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "..."

Nginx配置如下:
server {

    listen 443;
    server_name ... ...;

    ssl on;
    ssl_certificate /etc/nginx/ssl/....cer;
    #ssl_client_certificate /etc/nginx/ssl/....cer;
    ssl_certificate_key /etc/nginx/ssl/....key;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2; 

    root /var/www/drupal7; ## <-- Your only path reference.

    # Enable compression, this will help if you have for instance advagg module
    # by serving Gzip versions of the files.
    gzip_static on;
    sendfile on;
    client_max_body_size 2048M;

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

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

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
            allow 127.0.0.1;
            deny all;
    }

    location ~ \..*/.*\.php$ {
            return 403;
    }

    # No no for private
    location ~ ^/sites/.*/private/ {
            return 403;
    }

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
            return 403;
    }
    location / {
            # This is cool because no php is touched for static content
            try_files $uri @rewrite;
            proxy_read_timeout 300;
    }

    location /adore-djatoka {
#            if($args ~* "/adore-djatoka/resolver?url_ver=.+&rft_id=.+&svc_id=.+") {
#                rewrite ^ http://...:8080/adore-djatoka/resolver?url_ver=$0&rft_id=$2&svc_id=$1 last;
#            }
#            rewrite    ^(.*)https(.*)$    $1http$2;
             proxy_pass http://...:8080/adore-djatoka;
#            proxy_redirect http://...:8080/adore-djatoka /adore-djatoka;
            #proxy_redirect off;
    }

    location @rewrite {
            # You have 2 options here
            # For D7 and above:
            # Clean URLs are handled in drupal_environment_initialize().
            rewrite ^ /index.php;
            # For Drupal 6 and bwlow:
            # Some modules enforce no slash (/) at the end of the URL
            # Else this rewrite block wouldn't be needed (GlobalRedirect)
            #rewrite ^/(.*)$ /index.php?q=$1;
    }

    # For Munin
    location /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_intercept_errors on;
            fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass php-fpm;
    }

    # Fighting with Styles? This little gem is amazing.
    # This is for D7 and D8
    location ~ ^/sites/.*/files/styles/ {
            try_files $uri @rewrite;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

}

如果需要,我可以发布php-fpm配置文件。
谢谢,
Can

请提供您的 nginx 配置。 - NullDev
@NullDev 包含了 nginx 配置。 - pathintegral
2个回答

4

尝试替换您的PHP块

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_intercept_errors on;
    fastcgi_pass 127.0.0.1:9000;
    #fastcgi_pass php-fpm;
}

根据您所使用的PHP版本不同,操作也会有所不同。

对于PHP 5

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php5-fpm.sock;
}

或者对于PHP 7

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

针对PHP 7.4

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

等等,您可以随时检查 /run/php/ 中可用的 sock

ls -la /run/php

接着在你的Nginx配置文件中指定所需的版本。

不要忘记在修改配置后重新启动/重载nginx(以root身份):

# check for config errors
nginx -t

# make sure the command above doesn't yield any errors.
# Then reload nginx
service nginx reload

我正在项目中使用PHP 5。我按照你提到的做了,但是没有任何变化@NullDev。2017/04/22 13:16:46 [错误] 29233#0: *29976从上游读取响应头时recv()失败(104:连接被对等方重置),客户端:...,服务器:...,请求:"GET / HTTP/1.1",上游:"fastcgi://127.0.0.1:9000",主机:"..." - pathintegral
你确定你正确地应用了更改吗?你的日志仍然显示 fastcgi://127.0.0.1:9000,但这在我提供的版本中不存在。请确保你编辑了正确的配置文件,按照说明删除旧的 PHP 块,插入 PHP5 块并重新启动 nginx(service nginx restart)。 - NullDev
我按照你在nginx配置中说的做了。你提到的那一行来自php配置。我对此非常确定,因为我之前尝试过。 - pathintegral
1
对于 PHP 7.4: fastcgi_pass unix:/run/php/php7.4-fpm.sock; - Avi Kehat

1

fastcgi_read_timeout参数增加到600帮助我解决了问题。现在,网站加载有点慢。但是至少我可以预览和管理网站。感谢@NullDev的回复。


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