Apache-2.2

為 google.com 設置反向代理,同時維護託管在 apache 上的先前 Web 服務

  • December 28, 2021
server {
   listen 80;
   server_name example.com;
   location / {
       rewrite ^/(.*)$ https://$host$1 permanent;
   }
}

server {
   listen 443 ssl;
   server_name example.com;

   ssl_certificate      /etc/ssl/example.crt;
   ssl_certificate_key  /etc/ssl/example.key;

   ssl_session_cache    shared:SSL:1m;
   ssl_session_timeout  5m;

   location / {
       proxy_pass https://www.google.com;
       proxy_set_header Host www.google.com;
       proxy_set_header Referer https://www.google.com;

       proxy_set_header User-Agent $http_user_agent;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header Accept-Encoding "";
       proxy_set_header Accept-Language $http_accept_language;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       sub_filter google.com example.com;
       sub_filter_once off;
   }
}

我正在嘗試使用它,但是 apache 正在使用 0.0.0.0:80,但問題是如何在仍然讓 webservice.example.com 重定向的同時將來自 bi.example.com 的請求反向代理到 google.com請求託管 API 網路服務的 example.com。我不認為它可以做到,但我可能錯了。

嘗試啟動 nginx 時出現此錯誤:

Dec 28 10:52:22 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address al...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address alr...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address al...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address alr...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address al...use)
Dec 28 10:52:24 example.com nginx[27291]: nginx: [emerg] still could not bind()

我檢查了埠,它被 httpd 使用,它對應於 Apache。

為了進一步解釋,我有兩個 CNAME 重定向到 example.com,我想重定向到與 Google 不同的頁面,但這只是一個範例,所以因為兩個 CNAME 都重定向到相同的 url,我不認為這是如果 Apache 使用埠 80,則不可能從一個 url 重定向到另一個 url 以向 Google.com 以外的另一個連結提供 SSL 證書。除了為此使用全新的伺服器之外,還有其他可能性嗎?現在,有一個從 bi.example.com 到 Google 以外的另一個 URL 的 CNAME 重定向,我嘗試通過將 CNAME 從 bi.example.com 指向 example.com 而不是另一個 URL 來設置使用 nginx 的重定向。

您不能讓兩個程序(Apache 和 nginx)在同一個埠上偵聽,但您可以讓一個程序(Apache 或 nginx)將兩個單獨的主機名反向代理到兩個不同的服務,甚至可以在一個主機名上提供內容並反向代理另一個一。

因此,解決方案是首先決定兩個程序之一,Apache 或 nginx,然後在該程序中為您通過 CNAME 記錄重定向到的兩個主機名設置兩個虛擬主機。然後,這兩個虛擬主機中的一個可以反向代理到 Google,而另一個可以為您的網路服務提供服務(或反向代理)。

SSL 證書的問題是另一回事。您可以將反向代理配置為對兩個虛擬主機使用單獨的證書,也可以使用對它們都有效的證書。

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