I'm trying to host a website on a Debian server. For this I have set up docker containers for frontend and backend (backend being an aspnet6 API). I am using nginx in another container as a reverse proxy. Nginx is configured to route API-traffic ("domain.com/api/...") to the backend container exposing port 5000.
Using Curl (on the server machine) to retrieve from that API works:
curl http://127.0.0.1:5000/api/Oracle
true
However trying to connect to that API from my local machine via "domain.com/api/Oracle" results in a 502-Bad Gateway.
nginx logs show that the upstream is the same exact URL i tried with curl before
2022/07/21 01:30:08 [error] 30#30: *1 connect() failed (111: Connection refused) while connecting to upstream, client: <localmachineip>, server: unixile.de, request: "GET /api/Oracle HTTP/1.1", upstream: "http://127.0.0.1:5000/api/Oracle", host: "<serverip>"
It is even possible to access the backend api from my local machine directly, just not via the nginx rerouting. I have tried several different nginx configurations, including explicitly naming an upstream for the backend and routing the location more specifically, none worked, I'm at my wits' end. :(
This is my nginx configuration:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server {
listen 0.0.0.0:80;
root /var/www/unixile.de/html;
server_name unixile.de www.unixile.de;
index index.html;
location = / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://127.0.0.1:5000/api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
CodePudding user response:
As nginx is running inside a container 127.0.0.1
points to the container itself, where there is no backend running. Please replace that ip with the name of the backend container.
You also need to run all three containers to be in the same docker network