How to install symfony2 application in a subdirectory in nginx

I need to install multiple symfony2 applications on the same host, but in different subdirectories (or in location blocks).

Using this configuration, nginx displays a โ€œfile not foundโ€ message or a redirect message when trying to access any URL.

Example:

/login -> /base/login /app1 -> /base/app1 /app2 -> /base/app2 

Current configuration:

 root /base/default; #Points to an empty directory # Login Application location ^~ /login { alias /base/login/web; try_files $uri app_dev.php; } # Anything else location ~ ^/([\w\-]+) { alias /base/$1/web; try_files $uri app_dev.php; } location / { # Redirect to the login rewrite ^ /login redirect; } # Handle PHP location ~ \.php$ { include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param HTTPS off; fastcgi_pass unix:/var/run/php5-fpm.sock; } 
+4
source share
2 answers

After a few hours, to find this (sf2 doc does not explain how the cgi parameters are needed and interpreted, you need to go through Request.php to understand), so I share this.

This is a config that looks normal with sf2 in the {subdir} directory (and web access to files other than {subdir} / web / * is prohibited).

It works for me with php-fpm (socket).

Of course, replace "{subdir}" with your / path / from / docroot / to / symfony _root /

You can select dev environmentnement by adding "dev" to "{subdir" "(since app_dev.php in the URL no longer works with this conf)

 server { # general directives location ~ ^/{subdir}(/.*)$ { try_files /{subdir}/web$1 @sf2; } location ~ ^/{subdir}dev(/.*)$ { expires off; try_files /{subdir}/web$1 @sf2dev; } location @sf2 { expires off; fastcgi_pass {your backend}; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/{subdir}/web/app.php; fastcgi_param SCRIPT_NAME /{subdir}/app.php; fastcgi_param REQUEST_URI /{subdir}$1; } location @sf2dev { expires off; fastcgi_pass {your backend}; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/{subdir}/web/app_dev.php; fastcgi_param SCRIPT_NAME /{subdir}dev/app_dev.php; fastcgi_param REQUEST_URI /{subdir}dev$1; } # other location directives # if some others apps needs php, put your usual location to cactch php here } 

Hope this helps (and there is no wrong configuration) without warranty ...

Of course, you can choose prod / dev conf if you don't need it. And instead, you can use var and only one place @ sf2:

  set $sf2_root /{subdir}; location ~ ^/{subdir}(/.*)$ { set $sf2_prefix /{subdir}; set $sf2_ctrl app.php; try_files $sf2_root/web$1 @sf2; } location ~ ^/{subdir}dev(/.*)$ { set $sf2_prefix /{subdir}dev; set $sf2_ctrl app_dev.php; expires off; try_files $sf2_root/web$1 @sf2; } location @sf2 { expires off; fastcgi_pass {your backend}; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$sf2_root/web/$sf2_ctrl; fastcgi_param SCRIPT_NAME $sf2_prefix/$sf2_ctrl; fastcgi_param REQUEST_URI $sf2_prefix$1; } 
+15
source

Here is a simpler configuration for symfony2 in the "/ front /" subdirectory. Route creation and assets work fine.

Configuration

 set $frontRoot /your/project/path/web; set $sfApp app_dev.php; # Change to app.php for prod location /front/ { # Static files root $frontRoot; rewrite ^/front/(.*)$ /$1 break; try_files $uri @sfFront; } location @sfFront { # Symfony fastcgi_pass phpfcgi; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $frontRoot/$sfApp; fastcgi_param SCRIPT_NAME /front/$sfApp; fastcgi_param REQUEST_URI /front$uri?$args; fastcgi_param HTTPS off; } 

Some explanation

The trick is to get symfony to believe that the app.php script is in / before /, so it generates routes and assets using this path.

I looked at what Apache was giving MOD-PHP to use the same values.

  • SCRIPT_FILENAME: Absolute path to the PHP file. Here is always /your/project/path/app_dev.php
  • REQUEST_URI: URI entered by the user. Here we must manually add /front at the beginning of the path, since it was deleted by the file hosting service (via rewrite ^/front/(.*)$ /$1 break; )
  • SCRIPT_NAME: value /front/app_dev.php . This is the most important part. Symfony will shorten app_dev.php and add /front to all its routes.
+3
source

Source: https://habr.com/ru/post/1432304/


All Articles