nginx in docker fails to talk to localhost

Anton Hlinisty

The goal is to:

  1. Return the same index.html for all requests
  2. Except the case when pathname starts from "/api" (e.g. localhost/api/asd) - in that case forward this request to an app running on localhost:8080

I've dealt with the task successfully just running nginx on my ubuntu. But when I try the same in docker container I get 502 for each "/api" request:

2019/11/03 14:35:39 [error] 6#6: *22 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: 127.0.0.1, request: "GET /api/library/source/find?page=0&size=40&q=JTdCJTIycXVlcnklMjIlM0ElMjIlMjIlMkMlMjJwZXJpb2QlMjIlM0ElMjJBTlRJUVVJVFklMjIlMkMlMjJjbGFzc2lmaWNhdGlvbnMlMjIlM0ElNUIlNUQlMkMlMjJ0eXBlcyUyMiUzQSU1QiU1RCU3RA== HTTP/1.1", upstream: "http://127.0.0.1:8080/api/library/source/find?page=0&size=40&q=JTdCJTIycXVlcnklMjIlM0ElMjIlMjIlMkMlMjJwZXJpb2QlMjIlM0ElMjJBTlRJUVVJVFklMjIlMkMlMjJjbGFzc2lmaWNhdGlvbnMlMjIlM0ElNUIlNUQlMkMlMjJ0eXBlcyUyMiUzQSU1QiU1RCU3RA==", host: "localhost", referrer: "http://localhost/library/period/antiquity" 172.17.0.1 - - [03/Nov/2019:14:35:39 +0000] "GET /api/library/source/find?page=0&size=40&q=JTdCJTIycXVlcnklMjIlM0ElMjIlMjIlMkMlMjJwZXJpb2QlMjIlM0ElMjJBTlRJUVVJVFklMjIlMkMlMjJjbGFzc2lmaWNhdGlvbnMlMjIlM0ElNUIlNUQlMkMlMjJ0eXBlcyUyMiUzQSU1QiU1RCU3RA== HTTP/1.1" 502 559 "http://localhost/library/period/antiquity" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36" "-"

'172.17.0.1' looks weird. I use "nginx" image and it has the following in its /etc/hosts file:

127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  1a0b306a3a80

note '172.17.0.2', not '172.17.0.1'.

Dockerfile and nginx config I'm running may be found here: https://github.com/ahlinist/tempvs-ui

The image is built and run as: sudo docker build . -t tempvs-ui sudo docker run -p 80:80 tempvs-ui

What am I doing wrong? How to make the docker image send requests to localhost:8080?

V. Yalovchuk

Quick answer: run docker with additional parameter network:

docker run -p 80:80 --network=host tempvs-ui

The thing is that localhost in your docker (host) server and docker container are not the same.
With your current implementation redirect is happened inside the docker container. Whereas your target application runs in host machine. That's why there is Connection refused error.
Documentation: docker network, docker run network param.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

nginx / docker / ssl for localhost

nginx: Docker container fails to start

Docker fails to copy nginx conf

Nginx, django, docker, refuses connection over localhost

Nginx hangs "http://localhost" in docker-compose

docker-compose with NGINX not serving Django at localhost

docker-compose up fails for nginx container

docker build fails with "permission denied" on `nginx -t`

How to use nginx on Docker for Windows as reverse proxy for localhost?

how to config nginx docker container to recognize localhost application in different port?

How do I access a server on localhost with nginx docker container?

nginx, Meteor and Docker: Proxy SSL redirection does not work on localhost

docker-compose with nginx can't connect to localhost app

How to serve phpMyAdmin to localhost/phpMyAdmin instead of localhost:8080 using nginx in docker

Nginx Plus fails to resolve service using Docker embedded DNS server

Gunicorn, nginx, django, inside of a docker container. Gunicorn successfully runs on port 80 but nginx fails

winsock fails to connect to localhost

nginx http redirect to localhost

Nginx: route on localhost

How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)

nginx reverse proxy upstream fails in docker-compose with connection refused message

systemctl start nginx fails

Nginx fails to start with no logs

NativeScript, Android - connection to localhost fails

django selenium fails to load localhost

Exporting a database in phpmyadmin fails (localhost)

New-PSSession to localhost fails

Nginx rewrite localhost to /usr/share/nginx/static

Docker Toolbox - Localhost not working