Hello

I’m trying to make my dockerized React app served by Nginx to connect to backend using the proxy_pass configuration in nginx.conf file and it keep throwing below error.

2021/03/03 23:41:35 [error] 28#28: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: 127.0.0.1, request: "POST /authentication/login/ HTTP/1.1", upstream: "http://127.0.0.1:5000/login/", host: "127.0.0.1:8888", referrer: "http://127.0.0.1:8888/login"

Both API backend and containerized React app are running on local. React is running on http://127.0.0.1:8888 and the API is running on http://127.0.0.1:5000.

My nginx.conf file is looking like below

upstream api_server {
  server 127.0.0.1:5000;
}

server {
  charset UTF-8;
  listen 80;
  root /usr/share/nginx/html;
  index index.html index.htm;

  server_name 127.0.0.1;

  location /authentication/ {
    proxy_pass http://api_server/;
  }

  location / {
    try_files $uri /index.html;
  }

  error_page   500 502 503 504  /50x.html;

  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

I’ve tried different variations of above, but same results. Can anybody suggest me how to resolve this issue?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

It sounds like that the containers might not be connected to one Docker network. You need to make sure that you create a Docker network, and attach all of the containers to that network.

Feel free to share your docker-compose.yaml file here so I could try to advise you further.

Regards,
Bobby

  • Hi Bobby,

    I only have one container running on my local machine which is the React frontend. And I’m trying to make that connect to non-container backend application also sitting in my local. So I only have one container that is trying to connect to backend application running on localhost 5000.

    Here is my docker-compose.yaml

    version: '3.7'
    
    services:
    
      my_frontend:
        container_name: my_frontend
        build:
          context: .
          dockerfile: Dockerfile.prod
        ports:
          - '8888:80'
    
    
    • Hi there,

      Thanks for sharing the docker-compose.yaml file!

      I believe that the problem is that your frontend container is trying to connect to localhost:5000 relative to the container itself, so basically it is trying to connect to a backend service that is running inside the frontend container.

      To fix that, you could try adding network_mode: "host" to your docker-compose.yaml file:

      version: '3.7'
      
      services:
      
        my_frontend:
          container_name: my_frontend
          build:
            context: .
            dockerfile: Dockerfile.prod
          ports:
            - '8888:80'
          network_mode: "host"
      

      As per the documentation here. That way your container should have access to the services running on your host/laptop.

      Let me know how it goes!
      Regards,
      Bobby