Nginx

Nginx L4 代理適用於 HTTPS 但不適用於 HTTP

  • October 1, 2021

我有以下配置作為 Nginx 上的 L4 代理,一切正常。

 stream {


   
   map $ssl_preread_server_name $name {
   hostnames;
   .ipchicken.com          $ssl_preread_server_name;
   .bbc.com                $ssl_preread_server_name;
   .bbc.co.uk              $ssl_preread_server_name;
   .bbci.co.uk             $ssl_preread_server_name;
   .neverssl.com           $ssl_preread_server_name;  #<-------
   
}


server {

   resolver 8.8.8.8;
   listen 443;
   ssl_preread on;
   proxy_connect_timeout 5s;
   proxy_pass $name:$server_port;
}

但是當請求 HTTP 站點時,例如“http://neverssl.com/”,Nginx 沒有響應。對這個問題有任何想法嗎?

據我所知ssl_preread,指令僅適用於 HTTPS 協議。我不知道如何Hostngx_stream_core_module. 您可以嘗試在http上下文中使用附加server塊,如下所示

http {
   ...
   map $http_host $proxy {
       hostnames;
       .neverssl.com    $http_host;
       ...
   }

   server {
       listen 80;
       resolver 8.8.8.8;
       location / {
           if ($proxy = '') { return 403; } # Return HTTP 403 Forbidden for unlisted domains
           proxy_set_header Host $proxy;
           proxy_redirect off;
           proxy_connect_timeout 5s;
           proxy_pass http://$proxy;
       }
   }
}

stream {
   ... # stream configuration for HTTPS port 443 here
}

在流伺服器塊不需要使用$server_port變數,你只在 443 埠上監聽,所以你可以使用proxy_pass $name:443;

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