I have two application served in a single server app1.domain.com
and app2.domain.com
.
I want to proxy pass the request from app1
to serve static file and app2
to proxy_pass
localhost:8000
I already have both SSL certificate using letsencrypt, how can I serve both apps using a single server?
I thought I can use different server_name
. I read from here: the article use multiple NGINX server
with the same port 80. But when I try for the port 443 the test fails "duplicate listen options for..". Here is my current config
server {
server_name app1.domain.com;
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/app1.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app1.domain.com/privkey.pem;
index index.html;
root /usr/share/nginx/html/;
location ~* \.(css|js|png|jpg|jpeg)$ {
try_files $uri $uri/ =404;
gzip_static on;
}
location / {
try_files $uri /index.html =404;
gzip_static on;
}
}
server {
server_name app2.domain.com;
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/app2.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app2.domain.com/privkey.pem;
index index.html;
root /usr/share/nginx/html/;
location / {
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;
# Fix the “It appears that your reverse proxy set up is broken" error.
proxy_pass http://localhost:8000/;
proxy_read_timeout 90;
}
}
server {
if ($host ~ "app1.domain.com|app2.domain.com") {
return 301 https://$host$request_uri;
}
listen 80 default_server;
listen [::]:80 default_server;
server_name domain.id;
return 404;
}
And I thought maybe I could use if ($host = domain)
inside the location
but it fails too