Score:0

Host is unreachable while connecting to upstream from Node Docker container

in flag

I have an API at backend.mysite.local and I am able to access it directly from the browser. But when I try to call the API from within the Node container, I get 502 Bad Gateway with the following error:

connect() failed (113: Host is unreachable) while connecting to upstream, client: 172.20.0.1, server: www.mysite.local, request: "GET /api/items HTTP/1.1", upstream: "http://172.20.0.3:3000/api/items", host: "www.mysite.local"

This is my nginx config (I am new to nginx so I might do it in a shorter way, but it's mostly blocks for redirection from port 80 to port 443 - so would appreciate if you also review it)

server {
    listen 443 ssl;
    server_name backend.mysite.local;
    ssl_certificate /etc/ssl/mysite.local.crt;
    ssl_certificate_key /etc/ssl/mysite.local.key;
    
    index index.php index.html;
    server_name backend.mysite.local;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {        
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;

        
        set $METHODS  'GET, POST, OPTIONS, HEAD';
        set $HEADERS  'Authorization, Origin, X-Requested-With, Content-Type, Accept';
        
        
        if ( $request_method = POST ){
          add_header 'Access-Control-Allow-Origin' 'https://www.mysite.local';
          add_header 'Access-Control-Allow-Credentials' 'true';
        }

    }
    
}

server {
    listen 80;
    server_name http.backend.mysite.local;
    return 301 https://backend.mysite.local$request_uri;

}

server {
    listen 80;
    server_name nginx;
    return 301 https://backend.mysite.local$request_uri;

}



server {
    listen 80;
    server_name www.mysite.local;
    return 301 https://www.mysite.local$request_uri;
}

server {
    listen 80;
    server_name mysite.local;
    return 301 https://www.mysite.local$request_uri;
}

server {
    listen 443 ssl;
    server_name mysite.local;
    ssl_certificate /etc/ssl/mysite.local.crt;
    ssl_certificate_key /etc/ssl/mysite.local.key;
    return 301 https://www.mysite.local$request_uri;
}


  server {   
    listen 443 ssl;
    server_name www.mysite.local;     
    ssl_certificate /etc/ssl/mysite.local.crt;
    ssl_certificate_key /etc/ssl/mysite.local.key;
    

     location / {      
     proxy_pass      http://node:3000;
      
    }
  }

Edit: This is docker-compose.yml:

networks:
    mysite:
        driver: bridge

services:
    nginx:
        image: nginx:stable-alpine
        container_name: nginx
        ports:
            - "8088:8088"
            - "80:80"  
            - "443:443"          
   
        networks:
            - mysite                

    php:        
        build:
            context: ./laravel
            dockerfile: Dockerfile
        container_name: php
        ports:
            - "9000:9000"
        networks:
            - mysite

    node:
        build:
            context: ./react
            dockerfile: Dockerfile
        container_name: react
        
        ports:
            - "3000:3000"       
Michael Hampton avatar
cz flag
How did you create your containers? Show the relevant configuration (e.g. docker-compose.yml).
Stackerito avatar
in flag
Thank you, edited the post, added `docker-compose.yml`. I realized I might have completely misunderstood my whole app architecture. I mixed my host Windows machine hosts name with internal Docker container names and it all got mixed up.
Michael Hampton avatar
cz flag
Are all your containers actually up? The error you got is one I would expect if the container was not running.
Stackerito avatar
in flag
You are a real expert man, the node container shut itself down after I had error in the code. And am I right about what I said in the previous comment? That I mixed my host name with the internal Docker naming? I should use `host` mode instead in my case maybe?
Michael Hampton avatar
cz flag
I don't use Docker on Windows, so I can't really say anything about that. From all the issues I have seen people have, I probably wouldn't recommend it. It's not going to get deployed to production that way anyway.
Stackerito avatar
in flag
thank you. so if that's the right place to ask: in my case where I have Node, Nginx, MySQL and PHP services, how would you recommend me to develop my project? Directly on Windows (As I did in the first place?). I want it to be as close to production as possible (if that's a good thing?). I just started with Docker because I saw some work places ask for experience with it so I wanted to learn
Michael Hampton avatar
cz flag
More like, directly on Linux without intermediaries like WSL2 that occasionally do bizarre things. Start some Linux VMs of your own and play around.
Stackerito avatar
in flag
Thank you. And is there a proper way to install Nginx on Windows? If not I'll probably install Linux as dual boot alongside my Windows machine
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.