用于域的动态URL的Nginx动态通配符子域

时间:2017-10-07 10:02:09

标签: nginx yii2 subdomain

我想为以下网址配置nginx.conf

domain.com/user/suvojit => suvojit.domain.com

直到现在我的nginx配置如下

server {
listen 80; ## listen for ipv4

server_name domain.com;
root        /var/www/html/project/frontend/web/;
index       index.php;

access_log  /var/www/html/project/vagrant/nginx/log/frontend-access.log;
error_log   /var/www/html/project/vagrant/nginx/log/frontend-error.log;

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    try_files $uri =404;
}

location ~ /\.(ht|svn|git) {
    deny all;
}}

我正在使用Yii2应用程序。有什么需要在那里指定的吗?

1 个答案:

答案 0 :(得分:0)

您需要为nginx配置提供服务器块

server {

listen 80; ## listen for ipv4

server_name ~^(?<user>[^.]+)\.example\.com$;

return 301 http://example.com/user/$user;

}

您还需要确保在DNS中创建通配符子域条目,以便*.domain.com解析为您的服务器