我尽我所能,我无法做到这一点。
我想使用NGinx将我的子域名redirect到Debian服务器中的特定文件夹,下面是我尝试的configuration:
server { listen 8080; server_name ~^(?<user>.+)\.example\.net$; root /srv/www/example.net/$user; }
=>错误是:
启动nginx:[emerg]:未知的“user”variablesconfiguration文件/etc/nginx/nginx.conftesting失败
(注意:我也尝试没有^这里指出: Nginx server_name regexp不能作为variables )
如果我试试这个:
server { listen 8080; server_name *.example.net$; root /srv/www/example.net/$1; }
请求中出现错误:
2013/08/20 15:38:42 [错误] 5456#0:* 6禁止“/srv/www/example.net//”目录索引,客户端:xxx.xxx.xxx.xxx,服务器:* .example.net,请求:“GET / HTTP / 1.1”,主机:“test.example.net:8080”
又,$ 1是空的!
该文档是错误的,然后: http : //nginx.org/en/docs/http/server_names.html
更新:
这是工作(取自https://serverfault.com/questions/457196/dynamic-nginx-domain-root-path-based-on-hostname ):
server { server_name ~^(.+)\.example\.com$; root /var/www/example.com/$1/; }
但是我想显示PHP页面,如果我在我的服务器中添加以下内容,那么$ 1就是空的(wtf?):
index index.php index.html; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS! location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ { deny all; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ { expires max; log_not_found off; } location ~ \.php$ { server_tokens off; try_files $uri $uri/ /index.php?$args; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code; fastcgi_intercept_errors off; fastcgi_send_timeout 30s; fastcgi_read_timeout 30s; }
我终于找到了解决办法,而不是那么漂亮。
事实上,这是一个旧的NGinx版本(Debian Squeeze中的0.7.67)和NGinx配置中的一些奇怪的反应(可能是这个版本)的混合。
下面的代码工作正常,但只在1.2.1的NGinx版本中成功(在0.7.67版本中失败,未在其他版本中测试):
map $host $username { ~^(?P<user>.+)\.example\.com$ $user; } server { listen 80; server_name *.example.com; root /var/www/example.com/$username; index index.php index.html; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS! location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ { deny all; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ { expires max; log_not_found off; } location ~ \.php$ { server_tokens off; try_files $uri $uri/ /index.php?$args; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code; fastcgi_intercept_errors off; fastcgi_send_timeout 30s; fastcgi_read_timeout 30s; } }
这个替代方案也适用(对于较新的PCRE版本):
map $host $username { ~^(?<user>.+)\.example\.com$ $user; }
正确的形式是:
server { listen 8080; server_name ~^(?P<user>.+)\.example\.net$; location / { root /srv/www/example.net/$user; } }
我不得不把所有找到的解决方案,以创建我自己的工作.conf
这是我的回答从类似的问题 https://stackoverflow.com/a/40113553/1713660
server { listen 80; server_name ~^(?P<sub>.+)\.example\.com$; root /var/www/$sub; location / { index index.php index.html; } }