Nginx配置子域的文件夹

时间:2017-01-15 13:43:20

标签: nginx configuration virtualhost

如何配置名称为" projects"的单个文件夹,其中每个子文件夹都是子域名? 例: 我有网站example.com 在我的服务器上我有文件夹

/var/www/html/example.com

我创建了新文件夹:

/var/www/html/projects/

'中的每个文件夹' directory是新的子域:

/var/www/html/projects/site1 = site1.exapmle.com

由于

@ richard-smith,我在projects中设置了配置文件/etc/nginx/sites-available,内容为:

server {
listen 80;

root   /var/www/html/projects/$domain;
index index.php index.html index.htm index.nginx-debian.html;

server_name   ~^(www\.)?(?<domain>\.example\.com)$;

location / {
    try_files $uri $uri/ =404;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

} //而不是example.com我的domen名字。

/etc/nginx/sites-enabled中创建了符号链接并运行service nginx reload。 但是在打开site1.example.com之后。我得到"This site can’t be reached"。但我的example.com工作正常。

2 个答案:

答案 0 :(得分:0)

使用server_name指令中的正则表达式将允许您捕获子域的名称并在root指令中使用它。

server {
    server_name   ~^(www\.)?(?<domain>\.example\.com)$;
    root   /var/www/html/projects/$domain;
    ...
}

有关详情,请参阅this document

答案 1 :(得分:0)

对于我来说,我想匹配子域名(不包括'www'),文件夹名称只是子域名,这种语法对我有用。

server {
    ...
    server_name ~^(?!www)(?P<domain>.+)\.example\.com$;
    root /var/www/html/projects/$domain;
    ...
}

test.example.com将使用根位置/var/www/html/projects/test

相关问题