📜 ⬆️ ⬇️

Nginx + symfony. Create virtual hosts automatically

I have been using symfony for a long time as a php framework. In the role of the web server nginx + php-fpm, I noticed that many do not use the wonderful thing that nginx can do.

The script described below is suitable for projects written in symfony in principle for any projects, but I personally use this example for symfony. Create virtual hosts without a hands-on! ;)

Suppose you have a directory with projects - / var / www / git / ... The condition is that we refer to the project directories as well as hosts. For example, /var/www/git/sait.ru. (if you want www. to work, then you just need to make a symlink to this folder (ln -s ...)).
Total: just create a host in / etc / hosts and log in to nginx. He will pick up the projects in your working directory.

server {
listen 80;
root /var/www/git/$host/web; #
server_name _;
charset utf-8;

location / {
if (-f $request_filename) {
expires max;
break;
}
if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
rewrite ^(.*) /index.php last;
}
}

location ~ \.php$ {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
root /var/www/git/$host;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/git/$host/web$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/git/$host/web;
fastcgi_param PATH_INFO $path_info;
include /usr/local/nginx/conf/fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

')

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


All Articles