使用 Apache 将 blog.example.com 反向代理到 example.com/blog

使用 Apache 将 blog.example.com 反向代理到 example.com/blog

编辑-为目标子域添加了 Vhosts 配置。

我正在尝试反向代理blog.subdomain.comtld.com/blog。我目前的配置重定向到子域,而不是在 TLD 本身中呈现数据。

我在两台服务器上都使用 apache2,并且都使用 AWS 的 Lightsail 实例。

<VirtualHost _default_:443>
  ServerAlias *
  SSLEngine on
  SSLCertificateFile "/opt/bitnami/apache/conf/example.com.crt"
  SSLCertificateKeyFile "/opt/bitnami/apache/conf/example.com.key"
  DocumentRoot "/home/bitnami/htdocs/example-landing/public"

  # BEGIN: Configuration for letsencrypt
  Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
  # END: Configuration for letsencrypt

  # BEGIN: Support domain renewal when using mod_proxy without Location
  <IfModule mod_proxy.c>
    # ProxyPass /.well-known !
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

    ProxyPass /blog http://blog.example.com
    ProxyPassReverse /blog http://blog.example.com
  </IfModule>
  # END: Support domain renewal when using mod_proxy without Location

  <Directory "/home/bitnami/htdocs/example-landing/public">
    Require all granted
  </Directory>
  # This is for the Nodejs application running on the server
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
  
  # This is for the actual blog
  ProxyPass /blog
  ProxyPassReverse /blog http://blog.example.com

  # BEGIN: Support domain renewal when using mod_proxy within Location
  <Location /.well-known>
    <IfModule mod_proxy.c>
     ProxyPass !
    </IfModule>
  </Location>

  # END: Support domain renewal when using mod_proxy within Location
</VirtualHost>

blog.example.com 的配置

httpd-vhosts.conf

<VirtualHost *:80>
    ServerName ghost.example.com
    ServerAlias www.ghost.example.com
    DocumentRoot "/opt/bitnami/apps/ghost/htdocs"

Include "/opt/bitnami/apps/ghost/conf/httpd-app.conf"
</VirtualHost>

<VirtualHost *:443>
    ServerName ghost.example.com
    ServerAlias www.ghost.example.com
    DocumentRoot "/opt/bitnami/apps/ghost/htdocs"
    SSLEngine on
    SSLCertificateFile "/opt/bitnami/apps/ghost/conf/certs/server.crt"
    SSLCertificateKeyFile "/opt/bitnami/apps/ghost/conf/certs/server.key"

Include "/opt/bitnami/apps/ghost/conf/httpd-app.conf"
</VirtualHost>

httpd-应用程序配置文件

Include "/opt/bitnami/apps/ghost/conf/banner.conf"

ProxyPass /bitnami !
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http://127.0.0.1:2368/

答案1

两件事情:

ProxyPass 规则按照配置中列出的顺序进行处理,第一个匹配的规则获胜。

按照以下顺序,请求www.example.com/blog/page.html与第一个 ProxyPass 指令匹配,并且永远不会到达第二个 ProxyPass 指令:

ProxyPass / http://localhost:3000
ProxyPass /blog http://blog.example.com

调换一下顺序:

ProxyPass /blog http://blog.example.com
ProxyPass / http://localhost:3000

www.example.com/blog/page.html将匹配第一个指令并转发到您的博客,而的请求www.example.com/images/logo.png将与第一个 ProxyPass 指令不匹配并且将会失败并由第二个指令处理。

更改后,您的博客仍然无法正确显示,www.example.com/blog并继续重定向到子域:可能有很多原因,您需要使用 Web 开发人员工具集检查是否是因为客户端代码、缓存的永久重定向、深层链接保护、服务器端 URL 重写规则或其他原因。例如,参见;https://serverfault.com/a/561897/546643

相关内容