I'm trying to serve my PHP project under mydomain/backend as I want to reserve my base url for a Node / Vue project. I boiled down the problem to the fact that I am not able to have the PHP app run under /backend due to the interaction between the try+files and the ~.php block.
Any suggestions on how to realize this? Everything works fine if I simply remove /backend and access the app through my root location /
server {
listen 80;
listen [::]:80;
root /var/www/backend/public;
index index.php index.html index.htm;
server_name your-domain.com;
#location ^~ /backend/ {
location /backend {
# This is the folder that index.php is in
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The weird thing is, the relative routing of the PHP project is actually triggered when navigating to /backend, but the actual content of the project is missing. I want the project to be served as if the root of the project was /domain/backend, with all URLs in the project having that /backend prefixed
I tried to add /backend in the ~.php block as well, to no avail.
UPDATE:
Thank you for the response. I have come closer to the solution, but not quite there yet.
Following your tip, and the following link, I've learned the following: How to properly configure alias directive in nginx?
- try_files should not be used if using alias inside a location block due to the longstanding bug
- fastcgi_param SCRIPT_FILENAME $request_filename; should be used instead of fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; if you use alias
- If you do point 2, you should use index index.php; instead of fastcgi_index /index.php;
However using the updated.conf file, my PHP app reroutes still to the original URL.
What is wrong with the rewrite line? Why does /backend still route back to as if it were the project was in /? It is being fetched by the PHP app, as the Node app goes 404 for all other random non existing sub-URI's.
Do the lines
fastcgi_split_path_info ^(.+?.php)(/.*)$;
and
fastcgi_param PATH_INFO $fastcgi_path_info;
undergo any side effects due to placing them nested in an aliased block?
How do I properly try_files $uri $uri/ /index.php?_url=$uri&$args; to work inside the alias block?
server {
listen 80;
listen [::]:80;
server_name your-domain.com;
location ^~ /backend {
alias /var/www/back/public;
index index.php index.html index.htm;
if (!-e $request_filename) { rewrite ^ /backend/index.php?_url=$uri&$args last; }
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location / {
# node/Vue project
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}