Reverse-Proxy

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

  • March 4, 2021

編輯 - 為目標子域添加了 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-app.conf

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

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

兩件事情:

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

引用自:https://serverfault.com/questions/1055913