Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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


57.6k
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.1k
edited 6d ago

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 ?