将 Apache VirtualHost 转换为动态子域的 nginx 服务器块
Convert Apache VirtualHost to nginx Server Block for Dynamic Subdomains
我在 Apache 上有一个 Web 应用程序 运行,其中虚拟主机文件配置为将请求路由到子域到特定文件夹。不必在每次创建子域时都修改主机文件,这让我可以动态地将 URLs 路由到相关文件夹(如果文件夹不存在,则使用 catchall)-
<VirtualHost *:8080>
ServerName localhost.com
ServerAlias *.localhost.com
VirtualDocumentRoot "/var/www/clients/%1"
ErrorLog "logs\errors.log"
<directory "/var/www/clients/%1">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
我正在尝试将上述内容转换为 nginx,但找不到正确的逻辑来从 URL 中提取子域,然后在配置文件中设置 root
变量。
如果 root
路径不存在,谁能帮我为 nginx 编写 server {}
块以及一个包罗万象的块?
在 server_name 中使用命名的正则表达式捕获,您稍后可以参考。
server {
listen 8080;
server_name ~^(?<subdir>.*)\.localhost\.com$ ;
set $rootdir "/var/www/clients";
if ( -d "/var/www/clients/${subdir}" ) { set $rootdir "/var/www/clients/${subdir}"; }
root $rootdir;
}
您正在做的是将默认根目录设置为变量 $rootdir
,然后如果 $subdir
设置的子目录存在则覆盖它。
我在 Apache 上有一个 Web 应用程序 运行,其中虚拟主机文件配置为将请求路由到子域到特定文件夹。不必在每次创建子域时都修改主机文件,这让我可以动态地将 URLs 路由到相关文件夹(如果文件夹不存在,则使用 catchall)-
<VirtualHost *:8080>
ServerName localhost.com
ServerAlias *.localhost.com
VirtualDocumentRoot "/var/www/clients/%1"
ErrorLog "logs\errors.log"
<directory "/var/www/clients/%1">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
我正在尝试将上述内容转换为 nginx,但找不到正确的逻辑来从 URL 中提取子域,然后在配置文件中设置 root
变量。
如果 root
路径不存在,谁能帮我为 nginx 编写 server {}
块以及一个包罗万象的块?
在 server_name 中使用命名的正则表达式捕获,您稍后可以参考。
server {
listen 8080;
server_name ~^(?<subdir>.*)\.localhost\.com$ ;
set $rootdir "/var/www/clients";
if ( -d "/var/www/clients/${subdir}" ) { set $rootdir "/var/www/clients/${subdir}"; }
root $rootdir;
}
您正在做的是将默认根目录设置为变量 $rootdir
,然后如果 $subdir
设置的子目录存在则覆盖它。