I'm trying to create subdomains with my web application, however, I'm not experienced in nginx that much, I've been trying to find a stable solution from SF, but unfortunately I can't find any good solution.
The problem I'm trying to do is create flexible subdomains, for example if I have any subdomain like dev.example.com
it should go along the file directory of /var/www/example.com/www/dev
, and any type of subdomain (except the WWW) will attempt to find a directory, if it exists, make it the root.
/var/www/example.com/www/{subdomain}
Is the current directory to look for, if it doesn't exists, the default root would be:
/var/www/example.com/www/
This is my sites-enabled
configuration file for my domain.
server {
server_name example.com www.example.com;
root /var/www/example.com/www;
index index.php index.htm index.html;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/errors.log;
error_page 404 /index.php;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/www$fastcgi_script_name;
include fastcgi_params;
}
location /pma {
auth_basic "Website development";
auth_basic_user_file /var/www/example.com/www/dev/authfile;
}
location /dev {
auth_basic "Website development";
auth_basic_user_file /var/www/example.com/www/dev/authfile;
}
location ~ /\.ht
{
deny all;
}
}
server {
server_name pma.example.com;
index index.php;
root /var/www/example.com/www/pma;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/errors.log;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/www$fastcgi_script_name;
include fastcgi_params;
}
location / {
auth_basic "Website development";
auth_basic_user_file /var/www/example.com/www/dev/authfile;
}
}
server {
server_name dev.example.com;
index index.php;
root /var/www/example.com/www/dev;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/errors.log;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/www$fastcgi_script_name;
include fastcgi_params;
}
location / {
auth_basic "Website development";
auth_basic_user_file /var/www/example.com/www/dev/authfile;
if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
}
location ~ /\.ht
{
deny all;
}
}
EDIT: Updated conf file:
server {
#regex capture assigning the subdomain to $subdomain
server_name ~^(?<subdomain>.+)\.example\.com$;
if ($host ~* ^www\.(.*)) {
set $remove_www $1;
rewrite ^(.*)$ http://$remove_www$1 permanent;
}
#if the directory doesn't exist, redirect to the main site
if (!-d /var/www/example.com/www/$subdomain) {
rewrite . example.com redirect;
}
#if we have made it here, set the root to the above directory
root /var/www/example.com/www/$subdomain;
#the rest of your config
index index.php;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/errors.log;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/$subdomain$fastcgi_script_name;
include fastcgi_params;
}
# this needs to be enabled for dev.example.com and pma.example.com only
location / {
auth_basic "Authentication Required";
auth_basic_user_file /var/www/example.com/$subdomain/authfile;
}
location ~ /\.ht{
deny all;
}
}