NGINX and dynamic virtual hosts for Symfony

Standard

Consider that you have 3 virtual hosts running on your dev host. Add one more means that you have to configure, again, a virtual host. That happens to me!

Working with symfony3 I just created a virtualhost to group all by symfony projects.
My first rule is: Every host will follow the expression *.symfony.dev
Second rule: each project has a directory under folder /share

And finaly my nginx config file is like:

server {
listen 80;
server_name ~^(?<sname>.+?).symfony.dev$;
root /share/$sname/web;

charset utf-8;

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

location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}

# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don’t want to be accessible.
location ~ \.php$ {
return 404;
}

error_log /var/log/nginx/$sname_error.log;
access_log /var/log/nginx/symfony_access.log;
}

After add this config file, do not forget to create the symLink to sites-enabled and restart nginx service

Hope it Helps!

Leave a comment