Virtual Host Configuration
Detailed guide on configuring virtual hosts using the VIRTUAL_HOST environment variable, including WebSocket support and multiple hosts.
Configure environment VIRTUAL_HOST
in your containers
When you want a container's to be hosted on a domain set VIRTUAL_HOST
environment variable to desired server_name
entry.
For virtual host to work it requires
- nginx-proxy container to be on the same network as that of the container.
- port to be exposed in Docker file or while creating the container. When missing or if it has multiple exposed ports, port 80 is used by default.
- when hosting on port other than 80, make sure that your nginx-proxy container's port is bind-mounted to host port.
Some configurations possible through VIRTUAL_HOST
VIRTUAL_HOST | release address | container path | container port |
---|---|---|---|
example.com | http://example.com | / | exposed port |
example.com:8080 | http://example.com:8080 | / | exposed port |
example.com -> :8080 | http://example.com | / | 8080 |
https://example.com | https://example.com | / | exposed port |
example.com/api | http://example.com/api | /api | exposed port |
example.com/api/ -> / | http://example.com/api | / | exposed port |
example.com/api -> :8080/api | http://example.com/api | /api | 8080 |
https://example.com/api/v1:5001 -> :8080/api | https://example.com/api/v1:5001 | /api | 8080 |
wss://example.com/websocket | wss://example.com/websocket | / | exposed port |
With VIRTUAL_HOST
you can inject nginx directives into location each configuration must be separed with a ;
. You can see the possible directives in nginx documentation.
Example : VIRTUAL_HOST=somesite.example.com -> :8080 ;proxy_read_timeout 900;client_max_body_size 2g;
will generate configuration as follows
server{
server_name somesite.example.com;
listen 80;
location /{
proxy_read_timeout 900;
client_max_body_size 2g;
proxy_pass http://127.2.3.4; // your container ip here
}
}
Support for websocket
Exposing websocket requires the websocket endpoint to be explicitly configured via virtual host. The websocket endpoint can be ws://
or wss://
.
If you want to use both websocket and non-websocket endpoints you will have to use multiple hosts
-e "VIRTUAL_HOST=wss://ws.example.com -> :8080/websocket"
If you are not sure what paths should be exposed on websocket, you can opt for auto upgrade to websocket.
-e "VIRTUAL_HOST=https+wss://example.com"
Multiple Virtual Hosts on same container
To have multiple virtual hosts out of single container, you can use VIRTUAL_HOST1
, VIRTUAL_HOST2
, VIRTUAL_HOST3
and so on. In fact the only thing it matters is that the environment variable starts with VIRTUAL_HOST
.
Example: setting up a go-ethereum node.
docker run -d --network frontend \
-e "VIRTUAL_HOST1=https://ethereum.example.com -> :8545" \
-e "VIRTUAL_HOST2=wss://ethereum.example.com/ws -> :8546/" \
ethereum/client-go \
--rpc --rpcaddr "0.0.0.0" --ws --wsaddr 0.0.0.0