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

Issue with nginx host file, php-fpm and phalcon 4.x

The situation

I have the following directory structure

  • config
  • controllers
  • models
  • views
  • public
    • index.php
  • backend
    • modules
    • public
      • index.php
  • ......

The hosting account is a vagrant environment with debian 10, mariadb, php-fpm and nginx.

I have a frontend that is located in the root of the hosting account and the index.php thus located at "/var/www/phalcon.test/web/public".

I also have a backend that is located in the sub folder backend in the root of the hosting acocunt and the index.php is located at "/var/www/phalcon.test/web/backend/public".

Basically I have two separate phalcon projects in the same hosting account.

I've tried various things, read nginx documentation and I have been searching in google, but I have not been able to find a working nginx host set-up for the above folder structure.

The frontend is working without a problem, but I can't seems to get the backend working. I can access statis files like index.txt, but when I try to access the index.php directly or implicit I get a nginx 404 error message. I have tried various "solutions" for the "locarion /backend/" directive but none is working. I'm looking for a solution using the above folder structure. Not looking into subdomains.

Any help is appricited.

I use the following host file

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;

  # Set environment
  fastcgi_param APPLICATION_ENV development;

  # Set location for the frontend
  location / {
    try_files $uri $uri/ /index.php?_url=$uri&$args;
  }

  # Set location for the backend
  location /backend {
    root /var/www/cms-base.test/web/backend/public;

    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;
  }

  # make sure that static files are cached for max duration
  location ~* \.(js|css|less|png|jpg|jpeg|gif|ico|woff|ttf|svg|tpl)$ {
    expires       max;
    access_log    off;
    log_not_found off;
  }
}


58.3k
Accepted
answer

Hi

In location backend you should doing following


# Set location for the backend
  location /backend {
       alias /var/www/cms-base.test/web/backend/public;

        if (!-e $request_filename) { rewrite ^ /backend/index.php last; }
                             location ~* \.php(/|$) {
                                 fastcgi_pass  unix:/var/run/php/vagrant.sock;
                                 fastcgi_split_path_info ^(.+\.php)(/.*)$;
                                 fastcgi_param SCRIPT_FILENAME $request_filename;
                                 include fastcgi_params;
                             }
                             location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
                                         expires 5d;
                              }
         }
     }


4.5k
edited Nov '20

This is almost what I was looking for. Phalcon is running on both the front and back. So that is working perfect

The only issue I run in to now is that the _url supplied to the backend/public/index.php contains "/backend" instead of "/" or for users "/backend/users" instead of "/users". I can strip the /backend in my bootstrap before I send it to the application handler. But I rather have that part outside and in the host file instead.

Any idea on how to do that ?

edited Dec '20

The PHP-FPM (FastCGI Process Manager) is usually used to allow Nginx to provides a powerful set of tools that offer maximum performance for your PHP index index.php index.html index.htm; # if file exists return it right away if And this second configuration allow you to have different configurations by host

TellPopeyes