【问题标题】:nginx and php-fpm 502 errornginx 和 php-fpm 502 错误
【发布时间】:2021-02-19 18:20:11
【问题描述】:

我为这个问题寻找了许多解决方案。但是,他们都没有帮助我解决它。 /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 配置文件。

谢谢,

可以

【问题讨论】:

  • 请包含您的 nginx 配置
  • @NullDev 包含 nginx 配置。

标签: php nginx


【解决方案1】:

尝试替换您的 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;
}

等等等等。您可以随时查看sock/run/php/ 中可用的内容:

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”,主机:“...”
  • 您确定您正确应用了更改吗?您的日志仍然显示fastcgi://127.0.0.1:9000,这在我提供的版本中不存在。确保编辑正确的配置,删除旧的 php 块,插入 PHP5 块并重新启动 nginx (service nginx restart)
  • 我按照你在 nginx conf 中说的做了。你提到的那一行来自 php conf。从我之前的尝试中我可以确定这一点。
  • 对于 PHP 7.4:fastcgi_pass unix:/run/php/php7.4-fpm.sock;
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 2011-04-06
    • 1970-01-01
    • 2011-03-12
    • 2012-05-19
    • 2012-05-15
    • 2012-02-05
    • 2019-06-07
    相关资源
    最近更新 更多