- 网络相关

解决Apache反向代理中,跳转网址与客户端输入网址不一致的问题

摘要

这篇文章将会记录如何解决在Apache反向代理中,出现跳转网址与客户端输入网址不一致的问题,避免由于跳转地址不一致导致的无法访问的错误。

问题

示例配置:

<VirtualHost _default_:443>
    Servername blog.davcloud.top
    SSLEngine on
    SSLCertificateFile ssl_certificate_file
    SSLCertificateKeyFile ssl_certificate_key_file
    RewriteEngine On

    ProxyPass / http://192.168.1.101:3333/
    ProxyPassReverse / http://192.168.1.101:3333/
</VirtualHost>

在这个例子中,部署在公网上的代理服务器的网址为 https://blog.davcloud.top/, 转发到的内网服务器上的网址为http://192.168.1.101:3333

当客户端输入的网址为 https://blog.davcloud.top/test.html时,实际访问的网址是https://192.168.1.101:3333/test.html,该网址对于客户端而言并不存在。

解决方案

在配置中添加一句 “ProxyPreserveHost On” 命令。

配置:

<VirtualHost _default_:443>
    Servername blog.davcloud.top
    SSLEngine on
    SSLCertificateFile ssl_certificate_file
    SSLCertificateKeyFile ssl_certificate_key_file
    RewriteEngine On

    ProxyPass / http://192.168.1.101:3333/
    ProxyPassReverse / http://192.168.1.101:3333/
    ProxyPreserveHost On
</VirtualHost>

当客户端输入的网址为https://blog.davcloud.top/test.html时,实际访问的网址是https://blog.davcloud.top/test.html,此时该网址对于客户端而言是正确的。

About Ziqi.Yang394

Read All Posts By Ziqi.Yang394

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注