I have the nginx.conf file shown below.
I want to run both ssh and a webserver on port 443/SSL.
Also known as SSL-port-multiplexing.
At the same time, I want to use ssl-passthrough with SNI.
For ssh-multiplexing, I use $ssl_preread_protocol.
For SSL-SNI-passthrough, I use $ssl_preread_server_name
If I set proxy_pass $upstream;
, then ssh works fine, but the webpage(s) don't work.
If I set proxy_pass $name;
, then SSL-SNI-passthrough works, but ssh can't be accessed.
How can I combine the two map instructions ? e.g. something like
if $upstream = ssh
then proxy_pass $upstream
else proxy_pass $name;
endif
The problem is I need a way to combine the protocol-selection with the server_name-selection.
if(ssh) => forward to port 22
else => forward to port xy depending on server_name
Here's my config file:
stream{
upstream ssh
{
server 127.0.0.1:22;
}
upstream https_default_backend
{
server 127.0.0.1:443;
}
upstream daniel_backend
{
server 127.0.0.1:5005;
}
map $ssl_preread_protocol $upstream
{
default ssh;
"TLSv1.3" https_default_backend;
"TLSv1.2" https_default_backend;
"TLSv1.1" https_default_backend;
"TLSv1" https_default_backend;
}
map $ssl_preread_server_name $name
{
localhost daniel_backend;
prodesk daniel_backend;
daniel-steiger.ch daniel_backend;
www.daniel-steiger.ch daniel_backend;
default https_default_backend;
}
# SSH and SSL on the same port
server {
listen 443;
ssl_preread on;
#proxy_protocol on;
# proxy_pass $upstream;
proxy_pass $name;
}
}
Already find the solution?
I also have this problem, and I try this. It seems ok.
Necromancing.
Answering my own question for the benefit of others.
This is NOT possible with nginx (AFAIK).
You can, however, achive the goal with HAproxy.
The configuration is not quite that easy, so see below the hack that works for me.
Note that I've changed all values with search-and replace (there might be errors) in notepad.
This configuration assumes the following:
Server with public IP of 44.33.22.11
local http-servers running on ports (8000+x on the same server where HAproxy runs, hence 127.0.0.1)
sshd running on 127.0.0.1:22 (port 22 of the same machine as HAproxy)
two domains, each http and https:
http://firstname-lastname.com/
https://firstname-lastname.com/
http://forename-familyname.com/
https://forename-familyname.com/
all of these domains DNS-resolving to 44.33.22.11
If you want to use the proxy-protocol (proxy v2 is newest), uncomment
# send-proxy-v2
e.g. the linebecomes
Note that sni-passthrough reverses the proxy-order.
In nginx, the order is
-> request -> decrypt -> proxy headering decrypted request -> re-encrypt request -> forward
In haproxy SNI-passthough, the order becomes
-> request -> proxy headering encrypted request -> forward
Thus the middleware processing order in your http servers (on port 8000+x)
using nginx is
-> SSL-decrypt -> unheader -> process
while using HAproxy, it is
-> unheader -> SSL-decrypt -> process
This is due to using sni-passthrough on HAproxy, and using the SSL-keys in nginx (no passthrough). This nastly little fact caused me a lot of head-scratching.
Also note, I set up example.int, foo.int and bar.int in the hosts file resolving to 10.0.0.2 (internal-network IP address of the machine with HAproxy) in the local network for testing purposes. You still see these entries in this haproxy.cfg file
This config forwards all requests for
to
127.0.0.1:22
and all requests for
http://firstname-lastname.com to 127.0.0.1:800X where X = 2n (even)
https://firstname-lastname.com to 127.0.0.1:800X where X = 2n+1 (odd)
(the better idea would have been to use 800X for http and 900X for https)