I have some applications running in Docker and I am using Nginx as a reverse proxy. I want to set up a default domain like domain.com and www.domain.com to redirect to a React app, a subdomain admin.domain.com to redirect to container1, and api.domain.com to redirect to container2. However, currently, traffic from admin and api subdomains is being redirected to the default domain.
My current nginx.conf is:
events {}
http {
include mime.types;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain.com www.domain.com;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/domain.com/privkey.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name api.domain.com;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;
location / {
proxy_pass http://container2:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name admin.domain.com;
auth_basic "Protected Site";
auth_basic_user_file /etc/nginx/.htpasswd;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;
location / {
proxy_pass http://container1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
I created three servers to handle each subdomain, but it didn’t work.
Felipe Calixto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.