我从Apache 2迁移到nginx,但在处理子域控件时遇到了问题。
我想要的是:当请求x.domain.tld时,在内部重写为domain.tld / x
我遇到的问题是nginx总是通过告诉浏览器重定向到页面来重定向页面。但是我真正想要的是内部执行此操作,就像Apache 2一样。
另外,如果我仅请求x.domain.tld,nginx会返回404。仅当我执行x.domain.tld / index.php时它才有效
这是我的配置:
server {
listen 80 default;
server_name _ domain.tld www.domain.tld ~^(?<sub>.+)\.domain\.tld$;
root /home/domain/docs/;
if ($sub) {
rewrite (.*) /$sub;
}
# HIDDEN FILES AND FOLDERS
rewrite ^(.*)\/\.(.*)$ @404 break;
location = @404 {
return 404;
}
# PHP
location ~ ^(.*)\.php$ {
if (!-f $request_filename) {
return 404;
}
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/etc/nginx/sockets/domain.socket;
}
}
谢谢!
请您参考如下方法:
当我在寻找相同问题的解决方案时在Google上找到此问答,因此我想发布最终使用的解决方案。
MTeck的第一个服务器块看起来非常漂亮,但是对于子域部分,您可以简单地执行以下操作:
server {
listen 80;
server_name "~^(?<sub>.+)\.domain\.tld$";
root /path/to/document/root/$sub;
location / { try_files $uri $uri/ /index.php; }
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/etc/nginx/sockets/domain.socket;
}
}
这使得
root配置指令取决于子域。


