I want to proxy multiple domains (that all point to the same single server) to different services (running on different ports).
Basically I want to run 2-3 webservers (e.g. Gitea in a docker-compose setup with Gitea, Postgres, Nginx and Certbot for the HTTPS certificates). I'd like to keep as much generic as possible thus I'd like to avoid having a single Webserver that needs to know about all the specific services and their certificates. Meaning the services should stay as they are.
My current solution for proxying my domains to the unique ports looks like this:
# /etc/nginx/nginx.conf
events {}
http {
server {
listen 80;
server_name domain1.example.org;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name domain2.example.org;
location / {
proxy_pass http://localhost:9080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
stream {
map $ssl_preread_server_name $name {
domain1.example.org domain1;
domain2.example.org domain2;
}
upstream domain1 {
server localhost:8443;
}
upstream domain2 {
server localhost:9443;
}
server {
listen 443;
proxy_pass $name;
ssl_preread on;
}
}
While this works, I wonder if there is a better / less-complicated solution to handle this, especially on the HTTP part?