Apache ProxyPass配置下图片/JS/CSS等静态资源无法加载问题咨询
解决Apache代理后静态资源404问题
问题根源是当前代理规则将除/index.php外的所有请求都转发给了Node.js服务,但core、themes、modules、libraries、sites这些目录下的静态资源实际存储在Apache的document root中,应该由Apache直接处理,而非被代理到Node.js。
你需要在VirtualHost配置中添加针对性的Rewrite规则,让这些静态资源目录的请求优先由Apache处理,仅将其他请求代理到Node.js。具体配置如下:
完整VirtualHost配置示例
<VirtualHost *:443> ServerName phpnodejs.mydomain.com DocumentRoot /path/to/your/document/root # 启用Rewrite模块 RewriteEngine On # 允许访问静态资源目录,直接由Apache处理 RewriteRule ^/(core|themes|modules|libraries|sites)/ - [L] # 处理index.php,由Apache的PHP模块处理 RewriteRule ^/index.php$ - [L] # 将剩余所有请求代理到Node.js服务 RewriteRule ^/(.*)$ http://localhost:8000/$1 [P,L] # SSL相关配置(根据你的实际情况调整) SSLEngine On SSLCertificateFile /path/to/your/cert.crt SSLCertificateKeyFile /path/to/your/private.key # 代理相关的必要配置 ProxyPassReverse / http://localhost:8000/ </VirtualHost>
规则说明
RewriteRule ^/(core|themes|modules|libraries|sites)/ - [L]:匹配以这些静态资源目录开头的请求,-表示不做任何修改,[L]表示停止后续规则处理,直接由Apache返回对应资源。RewriteRule ^/index.php$ - [L]:保留原有的index.php处理规则,交给Apache的PHP模块处理。RewriteRule ^/(.*)$ http://localhost:8000/$1 [P,L]:将未被上述规则匹配的请求,全部代理到Node.js服务的8000端口,[P]表示启用代理,[L]表示停止后续规则。
配置完成后,重启Apache服务使规则生效:
sudo systemctl restart apache2
内容的提问来源于stack exchange,提问作者Kaushal
相关产品推荐
相关产品推荐

