我无法从 NGINX 中的 URL 获取子域名

我无法从 NGINX 中的 URL 获取子域名

我无法从 NGINX 中的 URL 获取子域。

这是我的配置:

server {
    listen  80;
    server_name     ~^(?<appname>)\.example\.com$;

    rewrite ^ https://$appname.example.com$request_uri? permanent;
}

当我做:

http://bob.example.com/

我被派往:

https://.example.com/

我不知道我做错了什么。

我正在使用 NGiNX 1.2.7。

我有另一个配置:

http://example.com/

因此,我有一个用于没有子域的域的服务器块,第二个用于有子域的服务器块...这是关于子域的。

更新

这里的重定向(这是我尝试提取子域名的原因):

server {
    listen 443 ssl;

    server_name     ~^(?<appname>)\.example\.com$;

    ssl                     on;
    ssl_certificate         /etc/ssl/certs/example.com.crt;
    ssl_certificate_key     /etc/ssl/private/example.key;

    root    /var/www/example.com/apps/$appname/;

    include /var/nginx/general/php;
    include /var/nginx/general/upload;
    include /var/nginx/general/error_page_50x;
}

最新更新

我遇到的新错误(没有 P):

2013/06/30 00:49:02 [error] 7707#0: *64 directory index of "/var/www/example.com/apps//" is forbidden, client: 00.000.000.00, server: ~^(?<appname>)\.example\.com$, request: "GET / HTTP/1.1     ", host: "ebooks.example.com"
2013/06/30 00:49:02 [error] 7707#0: *64 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 00.000.000.00, server: ~^(?<appname>)\.example\.com     $, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ebooks.example.com"
2013/06/30 00:49:04 [error] 7707#0: *64 directory index of "/var/www/example.com/apps//" is forbidden, client: 00.000.000.00, server: ~^(?<appname>)\.example\.com$, request: "GET / HTTP/1.1     ", host: "ebooks.example.com"
2013/06/30 00:49:04 [error] 7707#0: *64 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 00.000.000.00, server: ~^(?<appname>)\.example\.com     $, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ebooks.example.com"

这是 ? 后面的 P:

2013/06/30 00:55:57 [error] 17915#0: *74 directory index of "/var/www/example.com/apps//" is forbidden, client: 00.000.000.00, server: ~^(?P<appname>)\.example\.com$, request: "GET / HTTP/1     .1", host: "drive.example.com"
2013/06/30 00:55:57 [error] 17915#0: *74 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 00.000.000.00, server: ~^(?P<appname>)\.example\.c     om$, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "drive.example.com"

答案1

您所做的只是将 HTTP 重定向到 HTTPS,因此您根本不需要这个正则表达式。

相反,做这样的事情:

server {
    listen [::]:80;
    server_name *.example.com;

    return 301 https://$http_host$request_uri$is_args$args;
}

答案2

如果您运行的是 CentOS 5(或其他较旧的操作系统),则可能是由于较旧的 PCRE 库。与 Python 兼容的语法可能适合您:

server_name     ~^(?P<appname>)\.example\.com$;

变化的是P后面的?。此功能在 PCRE 4.0 或更高版本中受支持,不带 的方法P需要 PCRE 7.0 或更高版本。

这在服务器名称Nginx 手册的页面,位于正则表达式名称

答案3

我只需要在 ?< subdomain > 后添加 .+ 即可:

server {
    listen  80;
    server_name     *.example.com;

    return 301 https://$http_host$request_uri$is_args$args;
}

server {
    listen 443 default_server ssl;

    server_name     ~^(?<subdomain>.+)\.example\.com$;

    ssl                     on;
    ssl_certificate         /etc/ssl/certs/example.com.crt;
    ssl_certificate_key     /etc/ssl/private/example.key;

    root    /var/www/example.com/apps/$subdomain/;

    include /var/nginx/general/php;
    include /var/nginx/general/upload;
    include /var/nginx/general/error_page_50x;
}

相关内容