Demonstrate how to add multiple services behind the reverse proxy. This shows the real power of the automated proxy setup for microservices architecture.
Create a second test service (API backend):
mkdir ~/api-service && cd ~/api-service
Create a simple API service:
// api.js
const express = require('express');
const app = express();
const PORT = 4000;
app.get('/health', (req, res) => {
res.json({ status: 'healthy', service: 'api', version: '1.0.0' });
});
app.get('/users', (req, res) => {
res.json({
users: [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Smith' }
]
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`API service running on port ${PORT}`);
});
Create Dockerfile for the API service:
FROM node:18-alpine
WORKDIR /app
RUN npm init -y && npm install express
COPY api.js .
EXPOSE 4000
CMD ["node", "api.js"]
Build and deploy the API service:
docker build -t api-service .
docker run -d --name api-backend \
--network nginx-proxy_nginx-reverse-proxy \
-e VIRTUAL_HOST=api.yourdomain.com \
-e LETSENCRYPT_HOST=api.yourdomain.com \
-e VIRTUAL_PORT=4000 \
api-service
Test both services through the proxy:
curl -H "Host: app.yourdomain.com" http://localhost/
curl -H "Host: api.yourdomain.com" http://localhost/health
Verify SSL certificates were automatically generated:
docker exec nginx-proxy ls -la /etc/nginx/certs/ | grep -E "(app|api).yourdomain.com"
Warning: Each new service must be on the same Docker network as the proxy. Services on different networks won't be discovered automatically.