告诉浏览器使用ProxyPass和ProxyPassReverse Apache

时间:2018-01-12 02:47:47

标签: node.js apache proxypass base-url

My BaseURL:https://mywebsite.com/fun通过Apache虚拟主机代理http://localhost:3000上的nodejs应用。

这是回复:

<html><body>
<a href="test.html">Click Me</a>
</body></html>

但是当我将鼠标悬停在浏览器中的链接上时,会显示https://mywebsite.com/test.html而不是https://mywebsite.com/fun/test.html

我尝试在代码中发送 Content-Location:标题,但无济于事。我还将/ fun路径添加到ProxyPass的末尾。

我可以在生成的代码中修复此问题并添加base-url,但这意味着即使在我获得的模板中也可以修复所有相对URL。会很痛苦的。任何想法如何解决?

以下是我的Apache虚拟主机

#
# Apache virtual host proxypass to localhost:3000
#
<VirtualHost *:443>
    ServerName mywebsite.com
    ServerAlias www.mywebsite.com
    ServerAdmin admin@mywebsite.com
    DocumentRoot /var/www/mywebsite.com/public_html

    ProxyPreserveHost On
    ProxyRequests off

    ProxyPass /fun http://localhost:3000
    ProxyPassReverse /fun http://localhost:3000

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLCertificateFile /etc/letsencrypt/live/mywebsite.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mywebsite.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

示例nodejs app。

//
// nodejs code running on 3000
//
const express = require('express')
const app = express()
app.get('/', (req, res) => {
    res.send('<a href="test.html">Click Me</a>')
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))

1 个答案:

答案 0 :(得分:0)

也许有更好的解决方案,所以这是我的解决方案,以防有人在寻找同样的问题:

    RewriteEngine  On
    RewriteRule "^/fun$" "https://%{HTTP_HOST}/fun/"  [L]
    ProxyPass "/fun/" http://localhost:3000/
    ProxyPassReverse "/fun/" http://localhost:3000/