Score:0

Nginx L4 Proxy Works with HTTPS but not with HTTP

us flag

I have the following configuration as L4 proxy over Nginx and everything works fine.

  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;
}

But when HTTP site is requested, like "http://neverssl.com/" Nginx is not responding. Any idea for this issue?

Ivan Shatsky avatar
gr flag
`http://` scheme uses TCP port 80, while your server block is listening on port 443.
Zareh Kasparian avatar
us flag
@IvanShatsky. Dear Ivan, Thanks for your reply. to be honest I want both HTTP and HTTPS to be routed through my proxy. Last time you have helped me to correct my configuration for HTTPS. I would be thankful if can guide me on how to have both configured in the same configuration file.
Zareh Kasparian avatar
us flag
@IvanShatsky i think there should be something configured in ssl_preread_server_name;,
Score:1
gr flag

As far a I know ssl_preread directive works only with HTTPS protocol. I don't know how to get HTTP Host header value in the ngx_stream_core_module. You can try to use an additional server block in the http context like shown here:

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
}

There is no need to use $server_port variable at the stream server block, you are listening on the 443 port only, so you can just use proxy_pass $name:443;

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.