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

Admin folder does not work with nginx

Hello!

Two phalcons are installed on the website:

  1. /home/srv/http/site.com
  2. /home/srv/http/site.com/admin

Part of nginx.conf:

server {
listen 80;
server_name site.com;
root /home/srv/http/site.com/public;
index index.php;

access_log off;
error_log /var/log/nginx/site.com_error.log;

location / {
    root /home/srv/http/site.com/public;
    try_files $uri $uri/ /index.php?_url=$uri&$args;
}

location /admin/ {
    root /home/srv/http/site.com/admin/public;
    try_files $uri $uri/ /admin/public/index.php;
}

include /etc/nginx/php.conf;

}

Previously, the apache was installed. And website and admin folder worked.

And now I replaced apache by nginx.

Website https://site.com work. But https://site.com/admin does not work, error 404.

Tell me please, what I'm doing wrong?



1.2k

I changed locations in config:

location / {
    root /home/srv/http/site.com/public;
    try_files $uri $uri/ /index.php?_url=$uri&$args;
}

location ~ \.php$ {
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/srv/http/site.com/public$fastcgi_script_name;
    include fastcgi_params;
}

location /admin {
    root /home/srv/http/site.com/admin/public;
}

location ~ /admin/.+\.php$ {
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/srv/http/site.com/admin/public$fastcgi_script_name;
    include fastcgi_params;
}    

But https://site.com/admin/ dont work also, 404 error.

I think you also need the try_files directive inside the location:

location ~ /admin/.+\.php$ {
    try_files $uri $uri/ /admin/index.php?_url=$uri&$args; #this line
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/srv/http/site.com/admin/public$fastcgi_script_name;
    include fastcgi_params;
}


1.2k
edited Jun '17

I changed location:

location /admin {
root /home/srv/http/site.com/admin/public;
try_files $uri $uri/ /admin/index.php?_url=$uri&$args;

}

This dont work.

And i try changed this:

location /admin {
try_files $uri $uri/ /admin/index.php?_url=$uri&$args;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/srv/http/site.com/admin/public$fastcgi_script_name;
include fastcgi_params;

}

Dont work also.

edited Jun '17
        # location pattern is just an public alias, can be whatever virtual (SEO) URI
location /my-admin-dir { try_files $uri $uri/ /physicalDirOnYourFileSystem/index.php$is_args$query_string; }

where: my-admin-dir is whatever you need to appear in a browser, i.e. SEO like URL.

physicalDirOnYourFileSystem is the physical directory where your admin app acutally resides, relative to the root directive in nginx.

Note: precedence is important! Thus, you need to have your /admin location block defined before root location /!

In your example this should be correct:

server {
listen 80;
server_name site.com;
root /home/srv/http; #pay attention this is now different
index index.php;

access_log off;
error_log /var/log/nginx/site.com_error.log;

# admin location block first
location /admin {
    try_files $uri $uri/ /site.com/admin/public/index.php$is_args$query_string;
}

#main site location block
location / {
    try_files $uri $uri/ /site.com/public/index.php$is_args$query_string;
}

include /etc/nginx/php.conf;

I see you've opened SO thread, did you tried what I wrote here?