1

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?

2
  • Do you have a specific reason for using the given regex for the PHP files instead of the standard location ~ \.php(/|$) ? Commented Jul 11, 2018 at 16:46
  • @IVOGELOV Yes, I need this specific configuration but I don't see the relation with try_files. Commented Jul 12, 2018 at 6:14

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.