Skip to content

Instantly share code, notes, and snippets.

@cbmd
Created December 9, 2012 21:13
Show Gist options
  • Save cbmd/4247040 to your computer and use it in GitHub Desktop.
Save cbmd/4247040 to your computer and use it in GitHub Desktop.
nginx config - dynamic virtual hosts
server {
index index.php;
set $basepath "/var/www";
set $domain $host;
# check one name domain for simple application
if ($domain ~ "^(.[^.]*)\.dev$") {
set $domain $1;
set $rootpath "${domain}";
set $servername "${domain}.dev";
}
# check multi name domain to multi application
if ($domain ~ "^(.*)\.(.[^.]*)\.dev$") {
set $subdomain $1;
set $domain $2;
set $rootpath "${domain}/${subdomain}/www/";
set $servername "${subdomain}.${domain}.dev";
}
server_name $servername;
access_log "/var/log/nginx/server.${servername}.access.log";
error_log "/var/log/nginx/server.dev.error.log";
root $basepath/$rootpath;
# check file exist and send request sting to index.php
location / {
try_files $uri $uri/ /index.php?$args;
}
# allow php only in root index.php
#location ~ "^/index\.php$" {
# allow execute all php files
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# configure phpmyadmin path
location /phpmyadmin {
root /usr/share/;
index index.php;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# disallow access to apache configs
location ~ /\.ht {
deny all;
}
# disallow access to git configs path
location ~ /\.git {
deny all;
}
# disallow access to yii code path
location ~ /(protected|themes/classic/views)/ {
deny all;
}
}
# sudo apt-get install dnsmasq
# echo "address=/.dev/127.0.0.1" >> /etc/dnsmasq.conf
@AhmadFazliIsmail
Copy link

Hi,

Kindly remove the .dev in line 8, 11, 15, 19, 25

Thank you.

Regards,
Ahmad Fazli Ismail,
http://www.cloud.org.my | http://www.k-ict.org.my | http://www.k-ict.org | http://www.afi-pcmaster.net

@Ekstazi
Copy link

Ekstazi commented Sep 14, 2014

Thank you. I'll use that config in my vagrant environment.

@Furgas
Copy link

Furgas commented Jun 11, 2015

In case you have other static vhosts in your nginx installation use

listen 80 default_server;

to catch unknown virtual hosts by this configuration.

@bogdandynamic
Copy link

Thank you. After two days of failed tests your solution saved me.

@bvelastegui
Copy link

Arigatow! 😸 💯

@webevt
Copy link

webevt commented Mar 11, 2017

additionally you should set

fastcgi_param SERVER_NAME $servername;

this will set in php "$_SERVER['SERVER_NAME']" variable to proper host name instead of plain "$servername"

@digitalhuman
Copy link

Nice thing, sadly you need LUA for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment