Apache配置反向代理
阿新 • • 發佈:2019-02-02
Apache的反向代理功能雖然沒有Nginx強大,但也是Apache提供的功能之一,當你的伺服器因過多的歷史包袱而不能切換到Nginx或因技術原因而不能熟練使用Nginx時,它也許會派上大用場。
配置中涉及到的主要指令:
ProxyPass 代理
ProxyPassMatch 區別於ProxyPass就只是匹配方式是通過regex,其他類似
ProxyRequests 正向代理開關
ProxyPassReverse 反向代理,修改返回的http header資訊
ProxyPreserveHost 反向代理,是否修改請求的header資訊
ProxyPassReverseCookieDomain 修改Cookie的域資訊
ProxyPassReverseCookiePath 修改Cookie的路徑資訊
假定我們要把對/foo和/bar的請求定向到後端的http://localhost:8080/foo和http://localhost:8080/bar,配置檔案如下:
ProxyPass /foo/ http://localhost:8080/foo/ ProxyPassReverse /foo/ http://localhost:8080/foo/ ProxyPassReverseCookieDomain http://localhost:8080 http://www.somesite.com ProxyPassReverseCookiePath / /foo/ ProxyPass /bar/ http://localhost:8080/foo/ ProxyPassReverse /bar/ http://localhost:8080/bar/ ProxyPassReverseCookieDomain http://localhost:8080 http://www.somesite.com ProxyPassReverseCookiePath / /bar/
如果使用ProxyPassMatch,則為
ProxyPassMatch ^/(foo|bar)/.*$ http://localhost:8080
而按照Apache的官方文件配置為:
ProxyPassMatch ^/(foo|bar)/(.*)$ http://localhost:8080/$1/$2
則是錯誤的,這算是Apache文件的一個坑吧。