I have a website with the following architecture:
- Many services running as part of a docker stack
- Fronted by single nginx as reverse proxy
- nginx does HTTPS termination with certbot
- Developed locally with
docker-compose up
- Deployed to production with
docker stack deploy
(on a public server with somedomain.com pointing to it)
The important part of the nginx config looks something like this:
server {
server_name somedomain.com;
listen 0.0.0.0:443 ssl http2 reuseport;
# Route service A to path /service-a
location /a-service {
# Trick to avoid nginx aborting at startup (set server in variable)
set $upstream_server http://service-a;
proxy_pass $upstream_server;
}
# Route service B to path /service-b
location /b-service {
# Trick to avoid nginx aborting at startup (set server in variable)
set $upstream_server http://service-b;
proxy_pass $upstream_server;
}
}
The problem arises when I am developing on the site. At that point it is running locally and somedomain.com won't resolve since we are simply on localhost / 127.0.0.1. Also certbot
will cause problems since locally we are not interested in HTTPS
and we are not available from somedomain.com.
I have solved this very crudely by simply not having nginx as part of the stack at all while running locally, however as the complexity and number of services is growing this becomes less feasible. I need to be able to test the whole solution including nginx before deploying.
So my question is, what is a practical way to maintain only ONE nginx config while allowing the site to be started both locally for development with HTTP on localhost/127.0.0.1 and in a production environment with HTTPS
under a domain and work both places?