Reputation: 5480
I'm trying to use the official nginx docker image (https://hub.docker.com/_/nginx) to serve static content and reverse proxy to an api web server in another docker container. Both of them are brought up by a docker-compose invocation so they should be part of the same network.
When I look at the logs I see errors like this where nginx was unable to talk to the upstream server. What concerns me is I see http://localhost/users?sort=lastName%2Cdesc
and it looks like it isn't honoring the port setting for upstream which is of course going to fail. I have no idea what the configuration should be to make it match that. All the guides I've read have some combination of the uri declaration and the upstream url declaration with end slashes or without, and none of those get me what I need.
[error] 7#7: *3 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/users?sort=lastName%2Cdesc HTTP/1.1", upstream: "http://localhost/users?sort=lastName%2Cdesc", host: "localhost", referrer: "http://localhost/"
API is on 8081
client is on 8080
client should forward anything with /api/* to the 8081 docker http://localhost:8081/ and strip out the /api/ portion
Copies all the static html into /usr/share/nginx/html and the config into /etc/nginx/nginx.conf
FROM nginx
COPY . /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
RUN rm -f /usr/share/nginx/html/Dockerfile
RUN rm -f /usr/share/nginx/html/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:8081/;
}
}
}
2019/10/11 00:21:38 [error] 7#7: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/skills HTTP/1.1", upstream: "http://127.0.0.1:8081/skills", host: "localhost", referrer: "http://localhost/"
2019/10/11 00:21:38 [warn] 7#7: *3 upstream server temporarily disabled while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/skills HTTP/1.1", upstream: "http://127.0.0.1:8081/skills", host: "localhost", referrer: "http://localhost/"
2019/10/11 00:21:38 [error] 7#7: *4 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/jobs?sort=createdDate%2Cdesc HTTP/1.1", upstream: "http://localhost/jobs?sort=createdDate%2Cdesc", host: "localhost", referrer: "http://localhost/"
10.0.2.2 - - [11/Oct/2019:00:21:38 +0000] "GET /api/jobs?sort=createdDate%2Cdesc HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
2019/10/11 00:21:38 [error] 7#7: *2 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/jobs/autosuggest HTTP/1.1", upstream: "http://localhost/jobs/autosuggest", host: "localhost", referrer: "http://localhost/"
10.0.2.2 - - [11/Oct/2019:00:21:38 +0000] "GET /api/jobs/autosuggest HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
10.0.2.2 - - [11/Oct/2019:00:21:39 +0000] "GET /api/users?sort=lastName%2Cdesc HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
2019/10/11 00:21:39 [error] 7#7: *3 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/users?sort=lastName%2Cdesc HTTP/1.1", upstream: "http://localhost/users?sort=lastName%2Cdesc", host: "localhost", referrer: "http://localhost/"
2019/10/11 00:21:39 [error] 7#7: *4 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/users/autosuggest HTTP/1.1", upstream: "http://localhost/users/autosuggest", host: "localhost", referrer: "http://localhost/"
10.0.2.2 - - [11/Oct/2019:00:21:39 +0000] "GET /api/users/autosuggest HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
2019/10/11 00:21:39 [error] 7#7: *2 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/assessments HTTP/1.1", upstream: "http://localhost/assessments", host: "localhost", referrer: "http://localhost/"
10.0.2.2 - - [11/Oct/2019:00:21:39 +0000] "GET /api/assessments HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
10.0.2.2 - - [11/Oct/2019:00:21:39 +0000] "GET /2.bundle.js HTTP/1.1" 200 11611 "http://localhost/jobs" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
10.0.2.2 - - [11/Oct/2019:00:21:40 +0000] "GET /21.bundle.js HTTP/1.1" 200 13624 "http://localhost/jobs" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
2019/10/11 00:21:40 [error] 7#7: *4 no live upstreams while connecting to upstream, client: 10.0.2.2, server: localhost, request: "GET /api/jobs?sort=createdDate%2Cdesc HTTP/1.1", upstream: "http://localhost/jobs?sort=createdDate%2Cdesc", host: "localhost", referrer: "http://localhost/jobs"
10.0.2.2 - - [11/Oct/2019:00:21:40 +0000] "GET /api/jobs?sort=createdDate%2Cdesc HTTP/1.1" 502 559 "http://localhost/jobs" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
Upvotes: 0
Views: 1092
Reputation: 167
You cannot refer to a different container address using localhost
. You can imagine containers as a separate machines and obviously using localhost
won't let you connect to the neighbor's or friend's computer. When two machines are in the same network you can create communication between them using their corresponding network ips or host names.
It works exactly the same in Docker. If you want to connect to other container in the same network you need to use it's ip (you can get it by e.g docker network inspect <NETWORK_NAME>
) or the hostname which in this case will be the same as container name.
So your nginx.conf
instead of:
location /api/ {
proxy_pass http://localhost:8081/;
}
Should contain:
location /api/ {
proxy_pass http://API_CONTAINER_NAME:8081/;
}
Upvotes: 2