Reputation: 13
Good morning, I want to make the same nginx vhost that can work with different php applications (symfony and Thelia). My problem is with the try_files instruction. In symfony the try_files must target app.php but in Thelia it must target index.php. So I wanted to modify the try_files statement as follows:
server {
listen 80;
server_name *.tld;
root /var/www/web;
location / {
try_files $uri /app.php$is_args$args /index.php$is_args$args;
}
location ~ ^/(app|app_dev|config|index|index_dev)\.php(/|$) {
fastcgi_pass php_alias:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param APP_ENV dev;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
Unfortunately, it doesn't work. Php is no longer interpreted. So how can I register multiple php files in the try_files statement?
Upvotes: 1
Views: 1116
Reputation: 49702
The try_files
directive can only have one default URI which is the last element and comes after all of the file elements. The problem you are encountering is that the file elements cause the request to be processed within the current location
. See this document for more.
You could use a named location
to handle the optional default URI, for example:
location / {
try_files $uri @rewrite;
}
location @rewrite {
if (-f $document_root/app.php ) {
rewrite ^ /app.php last;
}
rewrite ^ /index.php last;
}
See this caution on the use of if
.
Upvotes: 2