Score:0

NGINX forwarding by 3rd level domain name

ge flag
Ars

I have two local web apps which should be accessible from external network, but only one external IP. Both web apps use 443 port which can't be changed. So the task is to configure nginx in the way so that it can forward the incoming requests based on the 3rd level domain. Let's say my web apps live on 192.168.1.55 and 192.168.1.65 My public IP is 9*.23*.15*.12 and I have configured 2 dns A entries site1.example.com and site2.example.com which are both mapped to my public IP. In my ideal world I want nginx to forward all incoming requests with $server_name = site1.example.com to 192.168.1.55 and with $server_name = site2.example.com to 192.168.1.65 Please advise how it can be achieved. At the moment I was able to configure only simple forwarding to one of the web apps.

server {
    listen 443 ssl http2;
    server_name site1.example.com;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/blog.zeroxzed.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/blog.zeroxzed.ru/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    location /.well-known/acme-challenge/ {
    root /web/sites/example.com/www/;
    }

    location / {
    proxy_pass https://192.168.1.55;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    }
}
us flag
You need to create a second `server` block for `site2.example.com`.
djdomi avatar
za flag
Does this answer your question? [In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?](https://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom)
Ars avatar
ge flag
Ars
@TeroKilkanen do you mean that Nginx will distinguish the request just by server_name?
us flag
Yes, that is how nginx virtual host selection algorithm works, as documented in http://nginx.org/en/docs/http/server_names.html.
Ars avatar
ge flag
Ars
@TeroKilkanen oh, so easy! Thanks a lot!
I sit in a Tesla and translated this thread with Ai:

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.