基于主机名的dynamicnginx域根path?

我试图设置我的开发nginx / PHP服务器与一个基本的主/全部捕获vhostconfiguration,以便我可以根据需要创build无限___.framework.loc域 。

 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; } 

然而,nginx响应此设置的404错误。 我知道nginx和PHP正在工作,并有权限,因为我使用的localhostconfiguration工作正常。

 server { listen 80 default; server_name localhost; root /var/www/localhost; index index.html index.htm index.php; include /etc/nginx/php.conf; } 

我应该检查什么来发现问题? 这里是他们都加载的php.conf的副本。

 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; } 

为什么不使用:

 server_name *.frameworks.loc; root /var/www/frameworks/$http_host/public; 

Nginxconfiguration不是一个程序,它是一个声明。 当你使用这样的configuration时:

 server { server_name ~^(.+)\.frameworks\.loc$; ... set $file_path $1; root /var/www/frameworks/$file_path/public; } 

没有办法确保您的set指令将在root之前执行。

但是我喜欢使用map指令。 它依赖于maplocation之前被评估的事实

 http { map $http_host $rootpath { ~^(.?<mypath>+)\.frameworks\.loc$ $mypath; default / ; } .... root /var/www/frameworks/$rootpath } 

除了伟大的DukeLion的回答,我需要改变线路

~^(.?<mypath>+)\.frameworks\.loc$ $mypath;

~^(?P<mypath>.+)\.frameworks\.loc$ $mypath;

在我的/etc/nginx/nginx.conf文件中,如这里所build议的。

添加

root /var/www/frameworks/$rootpath

/etc/nginx/sites-available/default工作正常。

也许你也可以看看lighttpd。 它已经build立了对你在这里问的确切的支持。 这是调用mod_evhost

启用evhost

在你的lighttpd.conf文件中添加以下内容。 如果您使用的是Debian / Ubuntu的基础发行版,只需将/etc/lighttpd/conf-available/10-evhost.conf软链接或复制到/etc/lighttpd/conf-enabled/

     #http://redmine.lighttpd.net/wiki/1/Docs:ModEVhost
     server.modules + =(“mod_evhost”)
     evhost.path-pattern =“/ home / www /%_”

evhost.path-patten中的%_ (通配符)表示使用完整的域名(例如www.example.com)。 对www.example.com的请求将自动指向文档根目录/home/www/www.example.com/

添加额外的站点就像在/home/www下创build具有完整域名的另一个目录一样简单。 没有改变Lighttpdconfiguration文件。

还有其他的通配符,可以用来build立目录结构。 他们如下

     %% =>%符号
     %0 =>域名+ tld
     %1 => tld
     %2 =>没有tld的域名
     %3 =>子域名1
     %4 =>子域名2
     %_ =>完整的域名

详细信息在这里 。

PS:启用PHP也很容易,如果你在Debian / Ubuntu平台上。 只要启用10-fastcgi.conf15-fastcgi-php.conf

NGINX使用PCRE正则expression式库。
从NGINX v0.8.25开始, server_name指令允许命名捕获

在正则expression式中的命名捕获创buildvariables( 0.8.25 ),可以稍后在其他指令中使用使用命名括号时,NGINX会自动为每个命名括号设置一个variables,在服务器名称评估期间(我猜)。

我使用下面的代码片段来“围住”开发者环境。 «用户»是指他们的用户名和«proj»他们的项目:

 # ... server_name ~^(?<user>[^.]+)\.(?<proj>[^.]+).dev.local-server.com; root /home/$user/www/$proj; # ... 

请注意,nginxconfiguration是声明性的,因此,与运行时计算的值和variables相比,静态声明可能总是更快。 正则expression式评估是相对昂贵的,我想它必须在重负载(生产)环境中使用简约。