3

我尽我所能,我不能让它工作。

我想使用 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;
  }
4

3 回答 3

4

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

事实上,它混合了旧的 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;
}
于 2013-08-21T13:47:26.680 回答
3

我必须结合所有找到的解决方案来创建自己的工作.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;
    }
}
于 2016-10-18T16:36:11.990 回答
2

正确的形式是:

server {
    listen 8080;
    server_name  ~^(?P<user>.+)\.example\.net$;
    location / {
         root /srv/www/example.net/$user;
    }
}
于 2013-08-20T14:37:27.573 回答