我无法从 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"
您所做的只是将 HTTP 重定向到 HTTPS,因此您根本不需要这个正则表达式。
相反,请执行以下操作:
如果您运行的是 CentOS 5(或其他较旧的操作系统),则可能是由于较旧的 PCRE 库所致。与 Python 兼容的语法可能对您有用:
变化是在
P
之后?
。这在 PCRE 4.0 或更高版本中受支持,该方法不需要P
PCRE 7.0 或更高版本。这在 Nginx 手册的服务器名称页面中的正则表达式名称下进行了解释。
我只需要在 ?< 子域 > 之后添加 .+: