Fin*_*ish 0 linux nginx ubuntu-10.04
我想在我的 nginx 服务器(ubuntu 14.04)上托管多个域名,每个域名都有子域:
home/serve/
domain1.com
www
subdomain1
subdomain2
domain2.com
www
subdomain1
subdomain2
Run Code Online (Sandbox Code Playgroud)
我希望 www.domain1.com 和 domain1.com 都根到 /home/serve/domain1/www 和 subdomain1.domain1.com 根到 /home/serve/domain1/subdomain1。
我在有和没有 www 的域中都有这个工作。(见下文)但我不知道如何扩展它以启用子域生根。
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name ~^(www\.)?(?<domain>.+)$;
root /home/serve/$domain/www/;
location / {
index index.html index.htm index.php;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以扩展正则表达式以包含任何子域,而不仅仅是 www。此外,如果请求的子域的文件夹不存在,最好设置默认文件夹。
这样的事情应该可以正常工作:
server_name ~^(?<subdomain>\w*?)?\.?(?<domain>\w+\.\w+)$;
if ($subdomain = "") {
set $subdomain "www";
}
if (!-d "/home/serve/$domain/$subdomain") {
set $subdomain "www";
}
root "/home/serve/$domain/$subdomain";
Run Code Online (Sandbox Code Playgroud)
请注意,即使使用“if”指令通常是不可取的,但在这种特殊情况下它是完全安全和可接受的,因为这些指令是在服务器上下文中定义的。
| 归档时间: |
|
| 查看次数: |
1820 次 |
| 最近记录: |