quo*_*nt7 16 subdomain rewrite nginx
我从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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
当我在寻找同一问题的解决方案的同时在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;
}
}
Run Code Online (Sandbox Code Playgroud)
这使得root配置指令依赖于子域.
您应该看看http://wiki.nginx.org/IfIsEvil。您在这个配置文件中做了很多错误。
server {
server_name domain.tld www.domain.tld;
location / {
try_files $uri /index.php;
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/etc/nginx/sockets/domain.socket;
}
}
server {
server_name "~^(?<sub>.+)*\.(?<domain>.*)$";
return 301 $scheme://$domain/$sub$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
如果你想要保留它的内部,你将无法重写它。根据定义,跨站点重写需要发送回浏览器。您必须代理该请求。
server {
server_name "~^(?<sub>.+)*\.(?<domain>.*)$";
proxy_pass http://$domain/$sub$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
您应该阅读 Nginx wiki。所有这些都得到了深入的解释。
| 归档时间: |
|
| 查看次数: |
24546 次 |
| 最近记录: |