cannot run anvil behind nginx-proxy
Component
Anvil
Have you ensured that all of these are up to date?
- [X] Foundry
- [X] Foundryup
What version of Foundry are you on?
forge 0.2.0 (58a2729 2023-05-15T07:53:59.692600892Z)
What command(s) is the bug in?
anvil
Operating System
Linux
Describe the bug
Hi, I am trying to run an anvil avalanche local fork docker instance behind an nginx-proxy.
While I can reach every service of my docker-compose setup, I cannot do this with anvil. Somehow anvil seems to not like some of the nginx configuration automatically done for it but I cannot figure out the problem.
Dockerfile
# Use the latest foundry image
FROM ghcr.io/foundry-rs/foundry
WORKDIR /app
CMD ["anvil", "--host", "0.0.0.0", "--port", "8545", "--fork-url", "https://api.avax.network/ext/bc/C/rpc", "--fork-block-number", "30076731", "--retries", "30"]
docker-compose.yaml
version: '2'
services:
nginx-proxy:
image: nginxproxy/nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
environment:
- TRUST_DOWNSTREAM_PROXY=true
anvil-blockchain:
build:
context: ./
dockerfile: Dockerfile
restart: always
environment:
- VIRTUAL_HOST=anvil-avax-mainnet-fork.example.com
- VIRTUAL_PORT=8545
expose:
- "8545"
whoami:
image: jwilder/whoami
expose:
- "8000"
environment:
- VIRTUAL_HOST=whoami.local
- VIRTUAL_PORT=8000
now lets run this setup
$ docker-compose build
$ docker-compose up -d
proxying the whomai service works flawlessly:
$ curl -H "Host: whoami.local" http://127.0.0.1/
I'm b801b1dca788
trying to access the anvil blockchain from within the container works fine:
$ docker exec -it $(docker ps | grep anvil-blockchain | awk '{print $1}') \
wget -qO- --header="Host: anvil-avax-mainnet-fork.example.com" \
--header="content-type: application/json;" \
--post-data='{ "jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":1}' \
http://127.0.0.1:8545/
{"jsonrpc":"2.0","id":1,"result":"0x0"}%
whereas when I try to access the anvil local blockchain over the proxy, I get an 502 Bad Gateway
$ curl -H "Host: anvil-avax-mainnet-fork.example.com" \
--request POST \
--header 'content-type:application/json;' \
--data '{ "jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":1}' \
http://127.0.0.1/
[..]
<head><title>502 Bad Gateway</title></head>
[..]
therefore I think its a nginx problem.
nginx log shows the following problem
$ docker-compose logs
[..]
nginx.1 | 2023/05/17 12:05:17 [error] 104#104: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: anvil-avax-mainnet-fork.example.com, request: "GET / HTTP/1.1", upstream: "http://172.20.0.2:8545/", host: "anvil-avax-mainnet-fork.example.com"
nginx.1 | anvil-avax-mainnet-fork.example.com 172.20.0.1 - - [17/May/2023:12:05:17 +0000] "GET / HTTP/1.1" 502 157 "-" "curl/7.71.1" "172.20.0.2:8545"
[..]
you can see the actual nginx configuration using
$ docker exec -it $(docker ps | grep nginx-proxy | awk '{print $1}') nginx -T
[...]
# anvil-avax-mainnet-fork.example.com/
upstream anvil-avax-mainnet-fork.example.com {
# Container: test_anvil-blockchain_1
# networks:
# test_default (reachable)
# IP address: 172.20.0.2
# exposed ports: 8545/tcp
# default port: 8545
# using port: 8545
server 172.20.0.2:8545;
}
server {
server_name anvil-avax-mainnet-fork.example.com;
access_log /var/log/nginx/access.log vhost;
listen 80 ;
listen 443 ssl http2 ;
# No certificate found for this vhost, so force nginx to emit a TLS error if
# the client connects via https.
ssl_ciphers aNULL;
set $empty "";
ssl_certificate data:$empty;
ssl_certificate_key data:$empty;
if ($https) {
return 444;
}
location / {
proxy_pass http://anvil-avax-mainnet-fork.example.com;
set $upstream_keepalive false;
}
}
[...]
. Does anybody knows which nginx configuration could be problematic here?
request: "GET / HTTP/1.1", upstream: "http://172.20.0.2:8545/"
hmm, this should be a POST?
sorry, the output posted did not match the curl example I used. It was from another curl I tried using GET. Here is the result from the POST.
nginx.1 | 2023/05/17 12:26:45 [error] 110#110: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: anvil-avax-mainnet-fork.example.com, request: "POST / HTTP/1.1", upstream: "http://172.20.0.2:8545/", host: "anvil-avax-mainnet-fork.example.com"
nginx.1 | anvil-avax-mainnet-fork.example.com 172.20.0.1 - - [17/May/2023:12:26:45 +0000] "POST / HTTP/1.1" 502 157 "-" "curl/7.71.1" "172.20.0.2:8545"
Got it resolved.
In my Dockerfile I used CMD which did not respect my parameters to anvil. It was starting anvil without any cli parameters so it did not change the host to 0.0.0.0. Therefore it did not listen on all interfaces so nginx did not get through my anvil backend.
After changing to ENTRYPOINT all parameters have been correctly applied and everything is working well.