This guide demonstrates how to host multiple servers on the same domain using Nginx as a reverse proxy. For example, you can serve:
- Main application at
<domain_name>
on port 8000 - Secondary application at
<domain_name>/app2
, on port 8001
Nginx Configuration
events {
worker_connections 1024;
}
http {
include mime.types;
types {
application/javascript js mjs;
}
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location /app2/ {
proxy_pass http://localhost:8001/;
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;
}
location = /app2/index.html {
proxy_pass http://localhost:8001/index.html;
}
location / {
proxy_pass http://localhost:8000;
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;
}
}
}
Dockerfile
# Build applications to run, on port 8000 and 8001
# <implementaion....>
# Run applications in parallel, and start Nginx
CMD ["/app/startup.sh"]
EXPOSE 80
Startup Script (startup.sh)
#! /bin/sh
<command to run 1st app> on 8000 &
<command to run 1st app> on 8001 &
exec nginx -g "daemon off;"
App Configuration for Vite its running as Secondary App
export default defineConfig({
base: 'https://<domain_name>/app2',
})