简体   繁体   English

nginx 和 php-fpm 502 错误

[英]nginx and php-fpm 502 error

I looked for many solutions for that issue.我为这个问题寻找了许多解决方案。 However, none of them helped me to solve it.但是,他们都没有帮助我解决它。 The error that shown in /var/log/nginx/error.log as follows: /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 config as follows: 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;
    }

}

If it'll be required I can post php-fpm config file, also.如果需要,我也可以发布 php-fpm 配置文件。

Thanks,谢谢,

Can

Try to replace your PHP block尝试替换您的 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;
}

By this, depending on your PHP Version,通过这个,取决于你的 PHP 版本,

For PHP 5 :对于PHP 5

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

Or for PHP 7 :或者对于PHP 7

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

For PHP 7.4对于PHP 7.4

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

And so on and so forth.等等等等。 You can always check what sock 's are available in /run/php/ :您可以随时检查/run/php/中可用的sock

ls -la /run/php

then just specify the version you want to use in your Nginx config.然后只需在 Nginx 配置中指定要使用的版本。

Don't forget to restart/reload nginx after those config changes (as root):不要忘记在这些配置更改后重新启动/重新加载 nginx(以 root 身份):

# check for config errors
nginx -t

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

Increasing fastcgi_read_timeout parameter to 600 helped me to solve my problem.fastcgi_read_timeout参数增加到600帮助我解决了我的问题。 Now, it's a little bit slow on loading the site.现在,加载网站有点慢。 However I can preview and manage the site, at least.但是,我至少可以预览和管理该站点。 Thanks for your replies @NullDev.感谢您的回复@NullDev。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM