[英]nginx server configuration: subdomain to folder
I migrated from Apache 2 to nginx and I've got problems to handly my subdomain control. 我从Apache 2迁移到nginx,我遇到了问题,以便我的子域控制。 What I want: When x.domain.tld is requested, internally rewrite to domain.tld/x
我想要的:当请求x.domain.tld时,在内部重写到domain.tld / x
The problem I've got is that nginx always redirects the page by telling the browser to redirect to. 我遇到的问题是nginx总是通过告诉浏览器重定向来重定向页面。 But what I really want is to do this internally, like Apache 2 did.
但我真正想要的是在内部执行此操作,就像Apache 2那样。 Also, if I only request x.domain.tld, nginx returns a 404. It only works when I do x.domain.tld/index.php
此外,如果我只请求x.domain.tld,则nginx返回404.它仅在我执行x.domain.tld / index.php时有效
Here's my config: 这是我的配置:
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;
}
}
Thanks! 谢谢!
As I found this Q&A on Google while looking for a solution for the same problem, I wanted to post the solution I finally used. 当我在寻找同一问题的解决方案的同时在Google上发现此问答时,我想发布我最终使用的解决方案。
The first server block by MTeck looks pretty nice, but for the subdomains part you could simply do the following: 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;
}
}
This makes the root
configuration directive dependent on the subdomain. 这使得
root
配置指令依赖于子域。
I spent hours beating my head against the wall and this is what works for me 我花了好几个小时撞在墙上,这对我有用
server {
listen 80;
server_name ~^(?P<sub>.+)\.example\.com$; #<-- Note P before sub, it was critical for my nginx
root /var/www/$sub; #<-- most important line cause it defines $document_root for SCRIPT_FILENAME
location / {
index index.php index.html; #<-- try_files didn't work as well
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; #<-- probably you have another option here e.g. fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
}
You should take a look at http://wiki.nginx.org/IfIsEvil . 你应该看看http://wiki.nginx.org/IfIsEvil 。 You're doing a whole lot wrong in this configuration file.
你在这个配置文件中犯了很多错误。
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;
}
If what you want is to keep that internal, you won't be able to rewrite it. 如果你想要的是保持内部,你将无法重写它。 By definition, a cross site rewrite needs to be sent back to the browser.
根据定义,需要将跨站点重写发送回浏览器。 You'll have to proxy the request.
您必须代理请求。
server {
server_name "~^(?<sub>.+)*\.(?<domain>.*)$";
proxy_pass http://$domain/$sub$request_uri;
}
You should read the Nginx wiki. 你应该阅读Nginx wiki。 All of this is explained in depth.
所有这些都得到了深入的解释。
This will work for www also. 这也适用于www。
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.php index.html index.htm index.nginx-debian.html;
server_name ~^www\.(?P<sub>.+)\.domain\.com$ ~^(?P<sub>.+)\.domain\.com$;
root /var/www/html/$sub;
location / {
try_files $uri $uri/ /index.php?$args;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.