子域不使用Nginx的动态根目录

子域不使用Nginx的动态根目录,nginx,Nginx,我尽了我所能,我不能使它工作 我想使用NGinx将我的子域重定向到Debian服务器中的特定文件夹,以下是我尝试的配置: server { listen 8080; server_name ~^(?<user>.+)\.example\.net$; root /srv/www/example.net/$user; } 请求中出现错误: 2013/08/20 15:38:42[错误]5456#0:*6目录索引 “/srv/www/example.net/”被

我尽了我所能,我不能使它工作

我想使用NGinx将我的子域重定向到Debian服务器中的特定文件夹,以下是我尝试的配置:

server {
    listen 8080;
    server_name  ~^(?<user>.+)\.example\.net$;
    root /srv/www/example.net/$user;
}
请求中出现错误:

2013/08/20 15:38:42[错误]5456#0:*6目录索引 “/srv/www/example.net/”被禁止,客户端:xxx.xxx.xxx,服务器: *.example.net,请求:“GET/HTTP/1.1”,主机:“test.example.net:8080”

又名,$1是空的

那么文件是错误的:

更新:

这是有效的(摘自):

但是我想显示PHP页面,如果我在我的服务器{}中添加以下内容,那么$1是空的(wtf?):

正确的形式是:

server {
    listen 8080;
    server_name  ~^(?P<user>.+)\.example\.net$;
    location / {
         root /srv/www/example.net/$user;
    }
}
服务器{
听8080;
服务器名称^(?P.+)\.example\.net$;
地点/{
root/srv/www/example.net/$user;
}
}

我终于找到了解决方案,但它并不漂亮

事实上,它是旧版NGinx(Debian Squence中的0.7.67)和NGinx配置的一些奇怪反应(可能来自这个版本)的混合

以下代码工作正常,但仅在NGinx版本1.2.1中成功(在0.7.67中失败,在其他版本中未测试):

map$host$username{
~^(?P.+)\.example\.com$$user;
}
服务器{
听80;
服务器名称*.example.com;
root/var/www/example.com/$username;
index.php index.html;
位置=/favicon.ico{
未发现注销日志;
访问/注销;
}
位置=/robots.txt{
允许一切;
未发现注销日志;
访问/注销;
}
#确保nginx不会加载具有以下扩展名的文件,因为nginx会显示源代码,并且这些文件可以包含密码!
位置~*\(引擎公司信息安装模块配置文件测试po主题tpl(\.php)?xtmpl)$$条目.$php{
否认一切;
}
#拒绝访问隐藏文件(如.htaccess、.htpasswd、.DS_Store(Mac))的所有尝试。
位置~/\{
否认一切;
访问/注销;
未发现注销日志;
}
位置~*\(jpg | jpeg | png | gif | css | js | ico)${
最大值;
未发现注销日志;
}
位置~\.php${
服务器_令牌关闭;
try_files$uri$uri//index.php?$args;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index.php;
包括fastcgi_参数;
fastcgi\参数脚本\文件名$document\根$fastcgi\脚本\名称;
fastcgi_参数路径_信息$fastcgi_脚本_名称;
fastcgi_参数地理IP_国家代码$GEOIP_国家代码;
fastcgi_截获_错误关闭;
fastcgi\u发送\u超时30秒;
fastcgi_读取超时30秒;
}
}
此替代方案也适用(适用于较新的PCRE版本):

map$host$username{
~^(?.+)\.example\.com$$user;
}

我必须结合所有找到的解决方案来创建我自己的工作
.conf
这是我的答案

服务器{
听80;
服务器名称^(?P.+)\.example\.com$;
root/var/www/$sub;
地点/{
index.php index.html;
}
}

Nope,
location{
失败,说它缺少一个参数,我添加了
/
,现在它失败了,出现了相同的错误:“[emerg]:未知的“用户”变量:/
P
添加到regex中?我直接复制了你的代码,只是重命名了“example.net”部分,所以是的,使用P:)这个配置也会出错,因为它试图获取
/var/www//index.php
server {
    server_name ~^(.+)\.example\.com$;
    root    /var/www/example.com/$1/;
}
  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;
  }
server {
    listen 8080;
    server_name  ~^(?P<user>.+)\.example\.net$;
    location / {
         root /srv/www/example.net/$user;
    }
}
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;
  }
}
map $host $username {
  ~^(?<user>.+)\.example\.com$ $user;
}
server {
    listen       80;
    server_name ~^(?P<sub>.+)\.example\.com$;
    root /var/www/$sub;

    location / {
        index index.php index.html;
    }
}