具有子域的动态根不与Nginx一起使用

时间:2013-08-20 13:45:49

标签: nginx

我尽我所能,我无法使它发挥作用。

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

server {
    listen 8080;
    server_name  ~^(?<user>.+)\.example\.net$;
    root /srv/www/example.net/$user;
}

=&GT;错误是:

  

启动nginx:[emerg]:未知“user”变量配置文件   /etc/nginx/nginx.conf测试失败

(注意:我也试过没有这里所示的^: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”

Aka,$ 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;
  }

3 个答案:

答案 0 :(得分:4)

我终于找到了解决方案,并不是那么漂亮。

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

以下代码工作正常,但仅在NGinx版本的1.2.1中成功(在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;
}

答案 1 :(得分:2)

正确的形式是:

server {
    listen 8080;
    server_name  ~^(?P<user>.+)\.example\.net$;
    location / {
         root /srv/www/example.net/$user;
    }
}

答案 2 :(得分:2)

我必须结合所有找到的解决方案才能创建自己的工作.conf这是我similar question 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;
    }
}