反向代理充當將客戶端請求轉發到其他伺服器的中介。它通常用於負載平衡、安全性、快取或將 HTTP 請求轉送到後端伺服器(例如,在 Node.js、Python、PHP 或其他伺服器上執行的應用程式)。
Apache 允許您使用其 mod_proxy 和 mod_proxy_http 模組進行設定。這是有關如何執行此操作的指南。
我們將設定 Apache 作為後端服務的反向代理,例如在連接埠 8080 上的本機上執行的伺服器。
1.啟用必要的模組
首先,您需要啟用Apache中的代理模組:
sudo a2enmod proxy sudo a2enmod proxy_http
重啟Apache以使模組生效:
sudo systemctl restart apache2
2.使用反向代理程式設定虛擬主機
現在編輯我們之前建立的虛擬主機的設定檔以新增代理指令。
開啟設定檔:
sudo your_editor /etc/apache2/sites-available/php.conf
在
ServerAdmin webmaster@localhost ServerName php.info DocumentRoot /var/www/html/php # Reverse Proxy Directives ProxyPreserveHost On ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ AllowOverride All Require all granted # Logs for debugging ErrorLog ${APACHE_LOG_DIR}/php_error.log CustomLog ${APACHE_LOG_DIR}/php_access.log combined
這些指令執行以下操作:
3.重啟Apache
更改後,再次重新啟動Apache:
sudo systemctl restart apache2
4.測試反向代理
現在,當您造訪http://php.info時,Apache會將請求轉送到監聽http://localhost:8080的後端。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3