We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

php7.4-fpm + Phalcon4.1 with 502 Bad Gateway Error!

Running the basic tutorial: https://github.com/phalcon/tutorial/tree/4.0.x

It got errors:

2020/11/13 22:18:02 [error] 15276#15276: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.10.123, server: svr, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "10.0.10.1"
2020/11/13 22:18:04 [error] 15276#15276: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.10.123, server: svr, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "10.0.10.1"
2020/11/13 22:29:25 [error] 15372#15372: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.10.123, server: svr, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "10.0.10.1"
2020/11/13 22:29:26 [error] 15372#15372: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.10.123, server: svr, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "10.0.10.1"
2020/11/13 22:29:27 [error] 15372#15372: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.10.123, server: svr, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "10.0.10.1"
2020/11/13 22:29:56 [alert] 15372#15372: *2 open socket #14 left in connection 4
2020/11/13 22:29:56 [alert] 15372#15372: aborting
2020/11/13 22:31:17 [error] 672#672: *2 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.10.123, server: svr, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "10.0.10.1"

In <?php phpinfo()?> page I can see:

phalcon enabled
Author  Phalcon Team and contributors
Version 4.1.0
Build Date  Nov 12 2020 15:34:57
Powered by Zephir   Version 0.12.19-4345593

Server: Ubuntu 20.04 LTS 64bit , install the extension in the way apt-get install php7.4-phalcon

php7.4-fpm(ngnix)

Phalcon4.1

PHP + ngnix works well, because I can CURD with phpmysqladmin

ngnix.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 120;
    types_hash_max_size 2048;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;

    client_max_body_size    32m;

    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # https://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

default.conf

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    server_name svr;

    root /var/www/html/public;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html ;

    charset utf-8;
    client_max_body_size 32M;
    fastcgi_read_timeout 300;

    location / {        
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        # Matches URLS `$_GET['_url']`
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php {
    #   include snippets/fastcgi-php.conf;

    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # When the HTTP request does not match the above
    # and the file ends in .php
    location ~ [^/]\.php(/|$) {
        # try_files $uri =404;

        # Ubuntu and PHP7.0-fpm in socket mode
        # This path is dependent on the version of PHP install
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

        # Alternatively you use PHP-FPM in TCP mode (Required on Windows)
        # You will need to configure FPM to listen on a standard port
        # https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/
        # fastcgi_pass  127.0.0.1:9000;

        fastcgi_index /index.php;

        include fastcgi_params;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_param PATH_INFO       $fastcgi_path_info;
        # fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        # and set php.ini cgi.fix_pathinfo=0

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

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

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}


4.5k

Hi,

Below is a nginx host file that works for me

Make sure that the following directives are correct:

  • root /var/www/cms-base.test/web/public; --> this needs to point to your public folder in your project

  • server_name cms-base.test www.cms-base.test; --> Make sure the right domain names are placed here. In my case it is cms-base.test and www.cms-base.test

  • fastcgi_pass unix:/var/run/php/vagrant.sock; -> Make sure that this points to the right sock file. I use vagrant environment with multiple test domains on it. Just have a look at the /var/run/php folder and pick the right one for you. Proberbly "/var/run/php/php7.4-fpm.sock" when using php 7.4
server {
  listen  80;
  listen  [::]:80;

  index index.php index.html index.htm;
  root /var/www/cms-base.test/web/public;

  error_log /var/www/cms-base.test/logs/nginx-error.log;
  access_log /var/www/cms-base.test/logs/nginx-access.log;
  rewrite_log on;

  charset     utf-8;
  server_name cms-base.test www.cms-base.test;

  client_max_body_size 100M;
  fastcgi_read_timeout 1800;

  fastcgi_param APPLICATION_ENV development;

  location / {
    try_files $uri $uri/ /index.php?_url=$uri&$args;
  }

  location ~ [^/]\.php(/|$) {
    fastcgi_pass  unix:/var/run/php/vagrant.sock;

    fastcgi_index /index.php;

    include fastcgi_params;

    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
      return 404;
    }

    fastcgi_param PATH_INFO       $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ /\.ht {
    deny all;
  }

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

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

  location ~* \.(js|css|less|png|jpg|jpeg|gif|ico|woff|ttf|svg|tpl)$ {
    expires       max;
    access_log    off;
    log_not_found off;
  }
}


31.3k

Well, It worked after I manually reinstall the Phalcon. But it seems the potential problems have not solved.

https://github.com/phalcon/cphalcon/issues/15210



4.5k

You can always enable debugging mode in nginx by adding debug behind the error log definition in your host file

error_log /var/www/cms-base.test/logs/nginx-error.log debug;

This will generate a lot of information, but might give you a clue as to what nginx is trying to do and why it might fail. I usually clean my error log and refresh my page after which I annalyze the logs generated for that one request.

My environment is debian 10 (latest), running on a vagrant environment (local vps) with nginx, php-fpm, mariadb and php-phalcon4 (version 4.0.6).

Thank you so much for solution.

edited Nov '20

Thanks for the step by step tutorial MyFordBenefits Login