我尽我所能,我不能让它工作。
我想使用 NGinx 将我的子域重定向到我的 Debian 服务器中的特定文件夹,这是我尝试过的配置:
server {
listen 8080;
server_name ~^(?<user>.+)\.example\.net$;
root /srv/www/example.net/$user;
}
=> 错误是:
启动 nginx: [emerg]: unknown "user" variable configuration file /etc/nginx/nginx.conf test failed
(注意:我也尝试过不使用 ^ ,如下所示:Nginx server_name regexp not working as variable)
如果我尝试这样做:
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;
}