In my container for nginx output following error message.
connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.10.1, server: localhost, request: “GET / HTTP/1.1”, upstream: “http://192.168.10.2:8000/”, host: “127.0.0.1”
I want to solve this. In the docker-compose, three services are defined; “dp”: PostgreSQL, “web” : Django, and “nginx” :nginx. these services are connected in networks.
services:
db:
...some descriptions...
web:
...some descriptions...
ports:
- "8000:8000"
depends_on:
- db
networks:
- backend
- frontend
nginx:
...some descriptions...
ports:
- "80:80"
depends_on:
- web
networks:
- frontend
...some descriptions...
networks:
frontend:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.10.0/24
...some descriptions...
The “db” and “web” are connected (since migrations worked). However, I can’t build the connection beteween “web” and “nginx” through the “frontend” network.
I have updated a configuration file “etc/nginx/nginx.conf”, as shown below, and “nginx -t” command said these comments;
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I suspect that the configuration file is the cause of the error, but I don’t fully understand it.
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
upstream web {
server web:8000;
}
server {
listen 80;
listen [::]:80;
# server_name _;
server_name localhost 127.0.0.1;
root usr/share/nginx/html;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /static/ {
alias /opt/static/;
}
location /media/ {
alias /opt/media/;
}
location / {
try_files $uri @proxy_to_web;
}
location @proxy_to_web {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
# proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://web;
}
}
}
Django app is activated by following command. Without nginx, it works well on the docker container environment. However, nginx can’t connect to it.
RUN gunicorn –bind 0.0.0.0:8000 config.settings.wsgi -D