将子目录请求重定向到 LAN 中的另一个 IP

将子目录请求重定向到 LAN 中的另一个 IP

我正在尝试将 myfakedomain.com 的 /net 重定向到 LAN 中的另一个 IP xxx.xxx.xxx.120。

即,当我尝试访问 myfakedomain.com/net 时,它应该显示 xxx.xxx.xxx.120 的内容

我还将 http 请求重定向到 https,如下所示

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName myfakedomain.com
    Redirect 301 / https://myfakedomain.com/

    ServerAdmin webmaster@localhost

</VirtualHost>


<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.myfakedomain.com
    Redirect 301 / https://www.myfakedomain.com/

    ServerAdmin webmaster@localhost
</VirtualHost>



<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName myfakedomain.com

    ServerAdmin webmaster@localhost

    RewriteEngine on

    # just in case (don't want to accidentally expose all the internal servers) !
    ProxyRequests off

    # define a log file
    LogLevel warn rewrite:trace3

    # add the tailing / if not there
    RewriteRule     ^/net$          https://myfakedomain.com/net/  [R] [L]

    # proxy the request to internal url
    RewriteRule     ^/net/*      http://xxx.xxx.xxx.120/$1 [P]

    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/myfakedomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/myfakedomain.com/privkey.pem
    DocumentRoot /var/www/html/myfakedomain
    <Directory /var/www/html/myfakedomain/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    </Directory>
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.myfakedomain.com

    ServerAdmin webmaster@localhost

    RewriteEngine on

    # just in case (don't want to accidentally expose all the internal servers) !
    ProxyRequests off

    # define a log file
    LogLevel warn rewrite:trace3

    # add the tailing / if not there
    RewriteRule     ^/net$          https://www.myfakedomain.com/net/  [R] [L]

    # proxy the request to internal url
    RewriteRule     ^/net/*      http://xxx.xxx.xxx.120/$1 [P]

    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/myfakedomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/myfakedomain.com/privkey.pem
    DocumentRoot /var/www/html/myfakedomain
    <Directory /var/www/html/myfakedomain/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    </Directory>
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

它尝试从 xxx.xxx.xxx.120 加载页面,但所有图像、css 和 js 都尝试从 myfakedomain.com 加载,如下所示

GET https://myfakedomain.com/themes/default/js/functions.js 404 (Not Found)

因此无法加载并出现 404 未找到错误。

我尝试使用 mod_proxy_html,下面是我的虚拟主机配置文件。它仍然是一样的,唯一的区别是所有图像都有一个边框(轮廓),而之前没有。

<VirtualHost *:80>
    ServerName myfakedomain.com
    ServerAdmin webmaster@localhost
        Redirect 301 / https://myfakedomain.com/
</VirtualHost>


<VirtualHost *:80>
    ServerName www.myfakedomain.com
    ServerAdmin webmaster@localhost
        Redirect 301 / https://www.myfakedomain.com/
</VirtualHost>


<VirtualHost *:443>
    ServerName myfakedomain.com
    ServerAdmin webmaster@localhost

    <Location /net>
            ProxyHTMLLinks a href
            ProxyHTMLLinks area href
            ProxyHTMLLinks link href
            ProxyHTMLLinks img src longdesc usemap
            ProxyHTMLLinks object classid codebase data usemap
            ProxyHTMLLinks q cite
            ProxyHTMLLinks blockquote cite
            ProxyHTMLLinks ins cite
            ProxyHTMLLinks del cite
            ProxyHTMLLinks form action
            ProxyHTMLLinks input src usemap
            ProxyHTMLLinks head profile
            ProxyHTMLLinks base href
            ProxyHTMLLinks script src for

            ProxyHTMLEvents onclick ondblclick onmousedown onmouseup \
                        onmouseover onmousemove onmouseout onkeypress \
                        onkeydown onkeyup onfocus onblur onload \
                        onunload onsubmit onreset onselect onchange

            ProxyPass http://xxx.xxx.xxx.120/
            ProxyPassReverse http://xxx.xxx.xxx.120/
            ProxyHTMLEnable On
            ProxyHTMLExtended On
            SetOutputFilter INFLATE;proxy-html;DEFLATE;
            ProxyHTMLURLMap http://xxx.xxx.xxx.120/ /net
    </Location>

    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/myfakedomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/myfakedomain.com/privkey.pem
    DocumentRoot /var/www/html/myfakedomain

    <Directory /var/www/html/myfakedomain/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    </Directory>


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

</VirtualHost>

<VirtualHost *:443>

    ServerName www.myfakedomain.com

    ServerAdmin webmaster@localhost

    <Location /net>
            ProxyHTMLLinks a href
            ProxyHTMLLinks area href
            ProxyHTMLLinks link href
            ProxyHTMLLinks img src longdesc usemap
            ProxyHTMLLinks object classid codebase data usemap
            ProxyHTMLLinks q cite
            ProxyHTMLLinks blockquote cite
            ProxyHTMLLinks ins cite
            ProxyHTMLLinks del cite
            ProxyHTMLLinks form action
            ProxyHTMLLinks input src usemap
            ProxyHTMLLinks head profile
            ProxyHTMLLinks base href
            ProxyHTMLLinks script src for

            ProxyHTMLEvents onclick ondblclick onmousedown onmouseup \
                        onmouseover onmousemove onmouseout onkeypress \
                        onkeydown onkeyup onfocus onblur onload \
                        onunload onsubmit onreset onselect onchange

            ProxyPass http://xxx.xxx.xxx.120/
            ProxyPassReverse http://xxx.xxx.xxx.120/
            ProxyHTMLEnable On
            ProxyHTMLExtended On
            SetOutputFilter INFLATE;proxy-html;DEFLATE;
            ProxyHTMLURLMap http://xxx.xxx.xxx.120/ /net
        </Location>

        SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/myfakedomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/myfakedomain.com/privkey.pem
    DocumentRoot /var/www/html/myfakedomain

        <Directory /var/www/html/myfakedomain/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
        </Directory>

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

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

答案1

这是一个非常常见的问题。您想将要在 '/' 下提供服务的内容代理为在其他 URI 路径下(在本例中/net/)。

如果底层服务器仅发送位置、内容位置或 URI 标头重定向,那么您可以使用 修复这些问题ProxyPassReverse,但如果底层 HTML 具有指向具有完全限定 URL 或以“/”开头的 URI 路径的资源链接,那么您只有 3 个真正的选择。

  1. 更改底层服务器以在您希望使用的 URI 路径下提供内容。(对于部署到 tomcat 等应用服务器的应用程序,这绝对是最佳选择)
  2. 使用 Apache v2.4 的 mod_proxy_html 来正确的底层服务器提供的 HTML/css/js。
  3. 使用 mod_sed/mod_substitute 进行一些修改来手动更正 HTML 中的引用。

还有第四种选择,您可以尝试重写底层服务器的任何其他 URI 路径,但这比上面描述的更混乱,而且如果您代理多个底层服务的资源,则很少可能。

相关内容