Apache-2.4

apache mod_rewrite / mod_proxy 將埠 443 添加到 https 上的代理 url 到 http

  • April 6, 2019

我們使用 mod_rewrite 和 mod_proxy 將爬蟲和機器人的請求代理到 prerender.io。prerender 服務目前不支持 https,但我的客戶站點只支持,所以代理是從 https 到 http。這曾經可以正常工作,但自從我們更新到 Apache 2.4.29 後,代理 url 總是添加了埠 443。

Old: https://www.example.com/news/something -> http://service.prerender.io/https://www.example.com/news/something
New: https://www.example.com/news/something -> http://service.prerender.io:443/https://www.example.com/news/something

如何在不恢復 apache 的情況下恢復到舊行為?

這是 .htaccess 程式碼:

RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|baiduspider|rogerbot|embedly|quora\ link\ preview|showyoubot|outbrain [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Yandex(Bot|Images|Video|Media) [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_

RewriteRule ^(.*)$ http://service.prerender.io/https://%{HTTP_HOST}$1 [P,L]

答案非常簡單明了,這可能是我錯過它的原因。

只需將埠 80 添加到代理 url:

RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|baiduspider|rogerbot|embedly|quora\ link\ preview|showyoubot|outbrain [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Yandex(Bot|Images|Video|Media) [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_

RewriteRule ^(.*)$ http://service.prerender.io:80/https://%{HTTP_HOST}$1 [P,L]

我不知道 Apache 2.4.29 和 Apache 2.2 在這方面有什麼區別,但是,如果您是從 Apache 2.0(或更早版本?)升級,那麼預設值會發生變化。

如果您沒有在替換 URL 中明確包含埠號,則 Apache 2.2+ 將從ServerName,UseCanonicalNameUseCanonicalPhysicalPort指令和/或請求派生埠號。在 Apache 2.0 上,預設值為(僅在 Apache 2.2 中引入) - 這在 Apache 的更高版本中更改為UserCanonicalName-因此,除非您明確設置它,否則它將使用請求中的埠。On``UseCanonicalPhysicalPort``Off

因此,如果您在伺服器更新後遇到差異,那麼我會懷疑這些指令之一的更改,除非您是從非常舊版本的 Apache 升級。

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