我在端口 444 上运行 Node.js 服务器,用于文档和 API 服务,使用<Location />
指令和ProxyPass
directrive 连接到 apache 服务器。连接本身运行完美...
但是,我希望 Apache 服务器提供常量资源(图像、图标)和文件,而不是将请求传递给节点服务器。我希望 Apache 提供的文件位于 和https://example.com/*/resources/
下https://example.com/files/
。
我如何指定位置和覆盖
<Location />
以使 apache 在特定的 url 目录下提供文件?
这是我的完整虚拟主机文件...
Listen 443
SSLCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLProxyCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLHonorCipherOrder on
SSLProtocol all -SSLv3
SSLProxyProtocol all -SSLv3
SSLPassPhraseDialog builtin
<VirtualHost _default_:443>
DocumentRoot "/usr/local/www/apache24/data"
ServerName www.example.com:443
ServerAdmin [email protected]
ErrorLog "/var/log/httpd-error.log"
TransferLog "/var/log/httpd-access.log"
ProxyRequests Off
SSLEngine on
SSLCertificateFile "/usr/local/etc/apache24/server.crt"
SSLCertificateKeyFile "/usr/local/etc/apache24/server.key"
SSLCACertificateFile "/usr/local/etc/apache24/ssl.crt/ca-bundle.crt"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/usr/local/www/apache24/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
CustomLog "/var/log/httpd-ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
# NODE.JS SERVER PROXY
# Sends request to Node.js server running on localhost:444
<proxy *>
Allow from all
</proxy>
#<LocationMatch "/*/resources">
#AllowOverRide None
#</LocationMatch>
#
#<Location "/files">
#AllowOverRide None
#</Location>
<Location />
ProxyPass http://localhost:444/
ProxyPassReverse http://localhost:444/
</Location>
</VirtualHost>
答案1
您可以使用Alias
指令将 URL 映射到文件系统。
这https://example.com/files/
很简单:
Alias "/files" "/the/dir/where/files/are/stored/files"
<Directory "/the/dir/where/files/are/stored/files">
Require all granted
</Directory>
URLhttps://example.com/*/resources/
稍微复杂一些,因为 * 可以匹配所有内容,这取决于您想要如何组织文件系统。在这种情况下,您可以使用AliasMatch
一种选择是:
AliasMatch "^/(.*)/resources/(.*)" "/the/dir/where/files/are/stored/$1/resources/$2"
<Directory "`/the/dir/where/files/are/stored/`">
Require all granted
</Directory>
如果你请求,https://example.com/aaa/resources/bbbb.jpg
它将寻找/the/dir/where/files/are/stored/aaa/resources/bbbb.jpg
但也许您想丢弃该*
值或以不同的方式组织文件:
AliasMatch "^/(.*)/resources/(.*)" "/the/dir/where/files/are/stored/resources/$1/$2"
<Directory "/the/dir/where/files/are/stored/resources">
Require all granted
</Directory>
这样,如果你请求https://example.com/aaa/resources/bbbb.jpg
它就会寻找/the/dir/where/files/are/stored/resources/aaa/bbbb.jpg
更多信息请访问:https://httpd.apache.org/docs/2.4/mod/mod_alias.html