我的 Apache 服务器上有以下目录结构
/var/www/domain.com/
index.html
site-1/
site-2/
.
.
.
site-N/
每个站点使用以下配置文件,其中变量“port”、“site”和“hub”是唯一会改变的值
<IfModule mod_ssl.c>
<VirtualHost *:443>
# DOMAIN CONFIGURATION
Define domain domain.dev
ServerName ${domain}
ServerAlias www.${domain}
DocumentRoot /var/www/${domain}
<Directory "/var/www/${domain}">
AuthType Basic
AuthName "${domain} Authentication"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
<Files "manifest.json">
Require all granted
</Files>
</Directory>
SSLEngine on
RewriteEngine on
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/${domain}/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/${domain}/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/${domain}/fullchain.pem
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
# SITE CONFIGURATION
Define site site-1
Define port 5001
Define protocol https
Define hub site-1-hub
ProxyPassMatch "^/${site}/api/(.+)" "${protocol}://localhost:${port}/api/$1"
ProxyPassReverse "/" "${protocol}://localhost:${port}/"
ProxyPassMatch "^/${site}/${hub}/(.+)" "${protocol}://localhost:${port}/${hub}/$1"
ProxyPassReverse "/" "${protocol}://localhost:${port}/${hub}"
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule "^/${site}/(.+)" "wss://localhost:${port}/$1" [P]
</VirtualHost>
有没有办法共享、重用或集中“域配置”和“站点配置”部分,这样我就不必在同一个域下的多个站点之间重复配置?如果是这样,我将能够对一个共享配置进行更改,该更改将应用于任何使用它的域或站点。
apache2ctl -S 输出
VirtualHost configuration:
*:80 localhost (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
以下是提供的解决方案的实际实现艾萨·约基宁(注:mod_macro模块需要通过sudo a2enmod macro
命令行运行来启用)
/etc/apache2/sites-available/domain.dev.conf 内容:
<Macro Domain $domain>
ServerName $domain
ServerAlias www.$domain
DocumentRoot /var/www/$domain
# Basic authentication
<Directory "/var/www/$domain">
AuthType Basic
AuthName "$domain Authentication"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
<Files "manifest.json">
Require all granted
</Files>
</Directory>
RewriteEngine on
SSLEngine on
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/$domain/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/$domain/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/$domain/fullchain.pem
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</Macro>
<Macro Site $site $port $protocol $hub>
ProxyPassMatch "^/$site/api/(.+)" "$protocol://localhost:$port/api/$1"
ProxyPassReverse "/" "$protocol://localhost:$port/"
ProxyPassMatch "^/$site/$hub/(.+)" "$protocol://localhost:$port/$hub/$1"
ProxyPassReverse "/" "$protocol://localhost:$port/$hub"
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule "^/$site/(.+)" "wss://localhost:$port/$1" [P]
</Macro>
<IfModule mod_ssl.c>
<VirtualHost *:443>
Use Domain domain.dev
Use Site site-1 5003 https site-1-hub
Use Site site-2 5004 https site-2-hub
.
.
.
Use Site site-N 500N https site-N-hub
</VirtualHost>
</IfModule>
答案1
Apache 模块 mod_macro可能正是您要找的。您可以定义一个宏:
<Macro MyProxySites $site $port $protocol $hub>
ProxyPassMatch "^/$site/api/(.+)" "$protocol://localhost:$port/api/$1"
ProxyPassReverse "/" "$protocol://localhost:$port/"
ProxyPassMatch "^/$site/$hub/(.+)" "$protocol://localhost:$port/$hub/$1"
ProxyPassReverse "/" "$protocol://localhost:$port/$hub"
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule "^/$site/(.+)" "wss://localhost:$port/$1" [P]
</Macro>
然后在你的配置中多次使用它,例如
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
# . . .
Use MyProxySites site-1 5001 https site-1-hub
Use MyProxySites site-2 5002 https site-2-hub
</VirtualHost>
<VirtualHost *:443>
ServerName example.net
ServerAlias www.example.net
DocumentRoot /var/www/example.net
# . . .
Use MyProxySites site-3 5003 https site-3-hub
Use MyProxySites site-4 5004 https site-4-hub
</VirtualHost>
</IfModule>
从您的问题来看很难说出您的确切目标,但如果每个站点的域名都会发生变化,并且一个域名只有一个代理,那么您也可以将其直接构建到宏中:
<Macro MyProxySites $domain $site $port $protocol $hub>
<VirtualHost *:443>
ServerName $domain
ServerAlias www.$domain
DocumentRoot /var/www/$domain
ProxyPassMatch "^/$site/api/(.+)" "$protocol://localhost:$port/api/$1"
ProxyPassReverse "/" "$protocol://localhost:$port/"
ProxyPassMatch "^/$site/$hub/(.+)" "$protocol://localhost:$port/$hub/$1"
ProxyPassReverse "/" "$protocol://localhost:$port/$hub"
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule "^/$site/(.+)" "wss://localhost:$port/$1" [P]
</VirtualHost>
</Macro>
Use MyProxySites example.com site-1 5001 https site-1-hub
Use MyProxySites example.net site-2 5002 https site-2-hub
Use MyProxySites example.org site-3 5003 https site-3-hub