M HYPE SPLASH
// general

Nginx failing to redirect to docker container

By Michael Henderson

I have the setup

docker-compose:

nginx: restart: always build: nginx/. // loads nginx:alpine image + copies the config ports: - 80:80 - 1337:1337 links: - rose:rose
rose: build: rose/. // simple website, based on node:latest restart: always expose: - 1337

nginx.conf:

upstream docker-rose { server rose:1337;
}
server { listen 0.0.0.0:1337; gzip on; // + some other gzip crap location ~ ^/(.*)/ { proxy_pass proxy_redirect off; proxy_set_header Host $http_host; // + some other proxy headers thingy }
}

docker-compose ps:

nginx_1 nginx -g daemon off; Up 0.0.0.0:1337->1337/tcp, 0.0.0.0:80->80/tcp
rose_1 npm start Up 1337/tcp

So i expect nginx to listen to outside 1337 port and pass everything to rose via 1337 internal port.

However, when i open localhost:1337 in the browser, i get

nginx_1 | 2018/04/29 14:57:22 [error] 9#9: *28 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.19.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:1337"

It has one job: to redirect everything_from_outside:1337 to internal_container:1337. Why on Earth it tries to load /etc/nginx/html/index.html?

EDIT: So, i got the container id via docker ps and logged in via docker exec -ti %id% sh. Trying to ping rose:

/ # ping rose
PING rose (172.19.0.11): 56 data bytes
64 bytes from 172.19.0.11: seq=0 ttl=64 time=0.083 ms
64 bytes from 172.19.0.11: seq=1 ttl=64 time=0.116 ms

Trying to ping localhost with 1337 port:

/ # ping localhost:1337
PING localhost:1337 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.080 ms
10

1 Answer

Thanks Thomas for pushing towards irc channel, guys there gave me a correct hint. The problem was with

location ~ ^/(.*)/ {

It was enough to simplify it to

location ~ / {

Now it works!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy