1

I want to setup symfony with nginx and this config is working fine

server {
    listen 80;
    root /src/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

However i also want that on my server i should also be able to access files via app_dev.php and app_test.php as well

so far with above config. http://127.0.0.1/api/check is working fine

but i also want

http://127.0.0.1/app_dev.php/api/check and http://127.0.0.1/app_test.php/api/check to work as well.

Currently its gives me 404 error

7
  • Does it work with http://127.0.0.1/app.php/api/check? Commented Oct 24, 2018 at 23:53
  • @Phil no it dont work , because the look like all the urls are automatically prefixed with app.php try_files $uri /app.php$is_args$args;. i tried chaging it to app_dev and app_test and it works fine as well but they dont work all together , with apache2 it works , bow now i want to use nginx only Commented Oct 25, 2018 at 0:13
  • You might need to add fastcgi_param PATH_INFO $fastcgi_path_info;. See nginx.com/resources/wiki/start/topics/examples/phpfcgi/…. You also haven't enabled php-fpm for anything other that app.php Commented Oct 25, 2018 at 0:17
  • @Phil You mean , i need to delete the line fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; and use fastcgi_param PATH_INFO $fastcgi_path_info; , also i tried chaging the regex to use app_dev as well but still i get 404 error Commented Oct 25, 2018 at 2:58
  • Honestly, I don't know as I'm a lousy sysadmin. You might get some traction over at serverfault.com if you can't get an answer here. Commented Oct 25, 2018 at 2:59

2 Answers 2

1

Assuming your using this for development you can list each file (Environment) in a capturing group () and separate with | which basically means "or".

location ~ ^/(app|app_dev|app_test)\.php(/|$) {
    ....
}

It is important not to use this example on a production server, as it is completely unsecure.

On a production server your current conf, is correct to only allow app.php.

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

1 Comment

Just for clarification you will need to restart nginx after making the changes.
0

For more information you can use description from symfony and nginx official docs.

As you can see you should add some lines into your nginx conf file:

location ~ ^/(app_dev|config)\.php(/|$) {
    ...
}

In case you also need app_test.php, then it should be changed a little bit. Just by including the file name:

location ~ ^/(app_dev|app_test)\.php(/|$) {
    ...
}

But please have a note:

In production, don't include this and don't deploy app_dev.php or app_test.php

Also it would be better to have following configuration before error_log ...:

location ~ \.php$ {
    return 404;
}

1 Comment

Did you do restart/reload of nginx?

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.