Created
May 23, 2017 22:03
nginx setup for dynamic domains and their subdomains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: https://stackoverflow.com/questions/8199231/how-to-setup-mass-dynamic-virtual-hosts-in-nginx | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
# Match any server name with the format [subdomain.[.subdomain...]].domain.tld.dev | |
server_name ~^(?<subdomain>([\w-]+\.)*)?(?<domain>[\w-]+\.[\w-]+)\.dev$; | |
# Map by default to (projects_root_path)/(domain.tld)/www; | |
set $rootdir "/var/www/$domain/www"; | |
# Check if a (projects_root_path)/(subdomain.)(domain.tld)/www directory exists | |
if (-f "/var/www/$subdomain.$domain/www"){ | |
# in which case, set that directory as the root | |
set $rootdir "/var/www/$subdomain.$domain/www"; | |
} | |
root $rootdir; | |
index index.php index.html index.htm index.nginx-debian.html; | |
# Front-controller pattern as recommended by the nginx docs | |
location / { | |
try_files $uri $uri/ /index.php; | |
} | |
# Standard php-fpm based on the default config below this point | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/run/php/php7.0-fpm.sock; | |
} | |
location ~ /\.ht { | |
deny all; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment