I am trying to setup my development nginx/PHP server with a basic master/catch-all vhost config so that I can created unlimited ___.framework.loc
domains as needed.
server {
listen 80;
index index.html index.htm index.php;
# Test 1
server_name ~^(.+)\.frameworks\.loc$;
set $file_path $1;
root /var/www/frameworks/$file_path/public;
include /etc/nginx/php.conf;
}
However, nginx responds with a 404 error for this setup. I know nginx and PHP are working and have permission because the localhost
config I'm using works fine.
server {
listen 80 default;
server_name localhost;
root /var/www/localhost;
index index.html index.htm index.php;
include /etc/nginx/php.conf;
}
What should I be checking to find the problem? Here is a copy of that php.conf they are both loading.
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_index index.php;
# Keep these parameters for compatibility with old PHP scripts using them.
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Some default config
fastcgi_connect_timeout 20;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_pass 127.0.0.1:9000;
}
Nginx config is not a program, it's a declaration. When you are using config like this:
There's no way to ensure that your
set
directive will execute beforeroot
.But there's a trick with
map
directive I like to use. It relies on fact thatmap
is evaluated beforelocation
Why not just use:
In addition to great DukeLion's answer, I needed to change line
~^(.?<mypath>+)\.frameworks\.loc$ $mypath;
to
~^(?P<mypath>.+)\.frameworks\.loc$ $mypath;
in my
/etc/nginx/nginx.conf
file as suggested here.Adding
root /var/www/frameworks/$rootpath
in
/etc/nginx/sites-available/default
worked fine after that.Maybe you can look into lighttpd also. It has build in support for exactly what you are asking here. It is call mod_evhost.
Enable evhost
Add following lines into your lighttpd.conf. If you are using Debian/Ubuntu base distribution, just soft link or copy from
/etc/lighttpd/conf-available/10-evhost.conf
to/etc/lighttpd/conf-enabled/
.The
%_
(wildcard) in evhost.path-patten means use the full domain name (eg. www.example.com). A request for www.example.com will automatically direct to document-root/home/www/www.example.com/
.Adding additional site is as easy as creating another directory under
/home/www
with full domain name. No change to Lighttpd config file.There are other wildcards and can be used to build directory structure. They are as follow
Detail information is here.
PS: Enabling PHP is also easy if you are on debian/ubuntu platform. Just enable
10-fastcgi.conf
and15-fastcgi-php.conf
.NGINX uses the PCRE regular expression library.
As of NGINX v0.8.25
server_name
directive allows named captures.I use the following snippet to "fence" developers environments. «user» refers to their username and «proj» the project they work on:
Note that nginx configuration is declarative, and as such, static declarations might always be faster compared to run-time calculated values and variables. Regular expressions evaluation is relatively costly, I guess it has to be used with parsimony in heavy loaded (production) environments.