Nginx: location for subfolder

1 answer

I want to move my root project to subfolder.

My current config is:

server {     listen 80;     index index.php index.html;     error_log  /var/log/nginx/error.log;     access_log /var/log/nginx/access.log;     root /var/www/public;     location ~ \.php$ {         try_files $uri =404;         fastcgi_split_path_info ^(.+\.php)(/.+)$;         fastcgi_pass app:9000;         fastcgi_index index.php;         include fastcgi_params;         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;         fastcgi_param PATH_INFO $fastcgi_path_info;     }     location / {         try_files $uri $uri/ /index.php?$query_string;         gzip_static on;     } } 

And current project structure as expected:

/var/www/public  ﹂index.php 

The problem is to rewrite locations ~ \.php$ and / with new variable of subfolder (project name).

/var/www/new-project/public  ﹂index.php 

I've tried to rewrite locations but it didn't work:

server {     listen 80;     index index.php index.html;     error_log  /var/log/nginx/error.log;     access_log /var/log/nginx/access.log;     # root /var/www/public;     root /var/www;      location ~ ([a-zA-Z0-9_\/\.]+)\.php$ {         try_files $uri =404;         fastcgi_split_path_info ^$1(.+\.php)(/.+)$;         fastcgi_pass app:9000;         fastcgi_index index.php;         include fastcgi_params;         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;         fastcgi_param PATH_INFO $fastcgi_path_info;     }      location ~/([a-zA-Z0-9_\/\.]+)$ {         alias /var/www/$1/public;         try_files $uri $uri/ /index.php?$query_string;         gzip_static on;     } } 

I think second location is pretty valid but I can't get how to rewrite it for \.php$ ending.

Received answer to this question:
The best answer according to the author of the question:

Found some overcome for this problem:

server {     listen 80;     index index.php index.html;     error_log  /var/log/nginx/error.log;     access_log /var/log/nginx/access.log;     root /var/www/;      location / {         deny all;         return 404;     }      location ~ /([a-zA-Z0-9-_]+)/(.+\.php$) {         alias /var/www/$1/public/$2;         # try_files $uri =404;         fastcgi_split_path_info ^/$1/(.+\.php)(/.+)$;         fastcgi_pass app:9000;         fastcgi_index index.php;         include fastcgi_params;         fastcgi_param SCRIPT_FILENAME /var/www/$1/public/$2/$fastcgi_script_name;         fastcgi_param PATH_INFO $fastcgi_path_info;     }      location ~ /([a-zA-Z0-9-_]+)/$ {         alias /var/www/$1/public/;         # try_files $uri $uri/ /index.php?$query_string;s         gzip_static on;     } } 

But the only one disadvantage of this approach that you can not use try_files with the alias.