Dynamic 如何在nginx中设置海量动态虚拟主机?

Dynamic 如何在nginx中设置海量动态虚拟主机?,dynamic,nginx,virtualhost,Dynamic,Nginx,Virtualhost,使用nginx已经玩了大约一个小时,试图设置大规模动态虚拟主机。 如果你在阿帕奇做过,你知道我的意思 目标是为办公室中的少数人(超过50人)提供动态子域只要您熟悉脚本编写,就不难编写一些脚本来快速在nginx中设置vhost。slicehost一文介绍了如何设置几个vHost,并以一种易于编写脚本并保持配置独立的方式进行了设置。唯一的缺点是必须重新启动服务器,但这在配置更改中是意料之中的 更新:如果你不想维护自己的任何配置,那么你唯一的两个选择(无论如何都是安全的)就是找到一个程序,让你的用户

使用nginx已经玩了大约一个小时,试图设置大规模动态虚拟主机。 如果你在阿帕奇做过,你知道我的意思


目标是为办公室中的少数人(超过50人)提供动态子域

只要您熟悉脚本编写,就不难编写一些脚本来快速在nginx中设置vhost。slicehost一文介绍了如何设置几个vHost,并以一种易于编写脚本并保持配置独立的方式进行了设置。唯一的缺点是必须重新启动服务器,但这在配置更改中是意料之中的


更新:如果你不想维护自己的任何配置,那么你唯一的两个选择(无论如何都是安全的)就是找到一个程序,让你的用户管理他们自己的nginx配置块(这将让他们创建他们想要的所有子域),或者自己创建这样一个面向用户的管理控制台


自己做这件事不会太难,特别是如果你已经有脚本来完成设置工作的话。基于web的界面可以调用脚本来执行实际工作,因此web界面所要处理的就是管理谁有权访问哪些内容。

您将需要一些脚本知识才能将其组合起来。我会使用PHP,但如果您擅长bash脚本,请使用它。我会这样做:

  • 首先创建一些文件夹(/usr/local/etc/nginx/domain.com/)

  • 在主nginx.conf中添加命令:include/usr/local/etc/nginx/domain.com/*.conf

  • 此文件夹中的每个文件都应该是不同的vhost名称subdomain.conf

  • 您无需重新启动nginx服务器,config即可执行操作,只需重新加载:/usr/local/etc/rc.d/nginx reload

    或者,您只能创建一个conf文件,其中应设置所有vhost。这可能更好,这样nginx就不需要加载50个文件,而只需要加载一个


    如果您在脚本编写方面有问题,请询问相关问题……

    也许这样做会让您达到您想要的目标:

    server {
    
        root /sites/$http_host;
    
        server_name $http_host;
    
        ...
    
    }
    

    我喜欢这一点,因为我可以动态创建站点,只需创建以域命名的新目录,并将DNS指向服务器ip。

    另一种选择是包含几个级别,以便目录可以按您认为合适的方式进行分类。例如:

    包括已启用的站点/*.conf;
    包括已启用的站点/*/*.conf;
    包括已启用的站点/*/*/*.conf;
    包括已启用的站点/*/*/*/*.conf;
    
    根据user2001260的答案,稍后由partlov编辑,以下是我的结果

    请记住,这适用于位于本地虚拟机上的dev服务器,其中在每个域的末尾使用
    .dev
    前缀。如果要删除它或使用其他内容,可以编辑或完全删除
    server\u name
    指令中的
    \.dev
    部分

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        # Match any server name with the format [subdomain.[.subdomain...]].domain.tld.dev
        server_name ~^(?<subdomain>([\w-]+\.)*)?(?<domain>[\w-]+\.[\w-]+)\.dev$;
    
        # Map by default to (projects_root_path)/(domain.tld)/www;
        set $rootdir "/var/www/$domain/www";
    
        # Check if a (projects_root_path)/(subdomain.)(domain.tld)/www directory exists
        if (-f "/var/www/$subdomain.$domain/www"){
            # in which case, set that directory as the root
            set $rootdir "/var/www/$subdomain.$domain/www";
        } 
    
        root $rootdir;
    
        index index.php index.html index.htm index.nginx-debian.html;
    
        # Front-controller pattern as recommended by the nginx docs
        location / {
            try_files $uri $uri/ /index.php;
        }
    
        # Standard php-fpm based on the default config below this point
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
    服务器{
    监听80个默认_服务器;
    侦听[:]:80默认_服务器;
    #将任何服务器名称与格式[subdomain.[.subdomain…]]匹配。domain.tld.dev
    服务器名称~^(?([\w-]+\)*)(?[\w-]+\.\w-]+\.dev$;
    #默认情况下映射到(projects\u root\u path)/(domain.tld)/www;
    设置$rootdir“/var/www/$domain/www”;
    #检查(projects\u root\u path)/(subdomain.)(domain.tld)/www目录是否存在
    如果(-f)/var/www/$subdomain.$domain/www”){
    #在这种情况下,将该目录设置为根目录
    设置$rootdir“/var/www/$subdomain.$domain/www”;
    } 
    root$rootdir;
    index.php index.html index.htm index.nginx-debian.html;
    #nginx文档推荐的前端控制器模式
    地点/{
    尝试_文件$uri$uri//index.php;
    }
    #基于以下默认配置的标准php fpm
    位置~\.php${
    包括snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    位置~/\.ht{
    否认一切;
    }
    
    }

    server\u name
    中的正则表达式捕获变量
    subdomain
    domain
    子域
    部分是可选的,可以为空。我将其设置为,默认情况下,如果您有子域,比如
    admin.mysite.com
    ,则根目录设置为与
    mysite.com
    相同的根目录。这样,相同的前端控制器(在我的例子中是
    index.php
    )可以基于子域进行路由。但是,如果您想在子域中保留一个完全不同的应用程序,您可以拥有一个
    admin.mysite.com
    dir,它将使用该目录调用
    admin.mysite.com

    小心:在当前的nginx版本中不鼓励使用
    if
    ,因为它会为每个请求增加额外的处理开销,但在开发环境中使用应该可以,这正是此配置的好处。在生产环境中,我建议不要使用大规模虚拟主机配置并分别配置每个站点,以获得更多控制和更好的安全性。

    server\u name~ ^(?[^.]*)\.domain\.com$;
    
    server_name ~^(?<vhost>[^.]*)\.domain\.com$;
    set $rootdir "/var/www/whatever/$vhost";
    root $rootdir;
    
    设置$rootdir“/var/www/whatever/$vhost”; root$rootdir;
    正如@Samuurai所建议的,这里是一个带有nginx构建集成的Angular 5的简短版本:

    server {
        server_name ~^(?<branch>.*)\.staging\.yourdomain\.com$;
        access_log /var/log/nginx/branch-access.log;
        error_log /var/log/nginx/branch-error.log;
        index index.html;
        try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
        root /usr/share/nginx/html/www/theft/$branch/dist;
    }
    
    服务器{
    服务器名称~^(?.*)\.staging\.yourdomain\.com$;
    access_log/var/log/nginx/branch-access.log;
    error\u log/var/log/nginx/branch-error.log;
    index.html;
    try_files$uri$args$uri$args/$uri$uri//index.html=404;
    root/usr/share/nginx/html/www/theft/$branch/dist;
    }
    
    您所说的“动态子域”是什么意思?你想要的“动态”有多大?重新启动服务器是否可以接受?这有点像,但不能同时启动。我运行批处理脚本并创建了50多个子域user####.domain.com,现在许多用户希望创建自定义子域test.user####.domain.com,可能一个用于发布,一个用于共享。控制t