-1

I have no programming background, I just started my first web project, so there is not much background knowledge, please keep that in mind. My project has run so far with wsl ubuntu on my windows machine, but I wanted to transfer it to docker.

The plan is (which I think is standard) that nginx gets all requests and if they start with static it will serve the files or otherwise redirect to daphne.

Sadly under localhost, I always see the nginx failure page:

502 Bad Gateway nginx/1.18.0 (Ubuntu)

When I try to access my static files e.g. http://localhost/static/css/styles.css, I see the content of the files in the browser, also when I try the command:

docker-compose exec nginx curl --unix-socket /tmp/daphne.sock http://localhost/

I get my index.html. But I have no idea what I'm doing wrong. I use the same default.config file for nginx as before when I was using wsl ubuntu, where it worked.

This is my docker-compose.yml:

version: '3'

services:
  web:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/app
      - daphne-socket:/tmp  # Hinzufügen des gemeinsamen Volumes zum web Service
    environment:
      - DAPHNE_SOCKET_PATH=/tmp/daphne.sock
    depends_on:
      - redis
    networks:
      - backend

  redis:
    image: "redis:alpine"
    networks:
      - backend

  nginx:
    image: "nginx"
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf #:ro nur Lesezugriff
      - ./static:/static
      - daphne-socket:/tmp  # Hinzufügen des gemeinsamen Volumes zum nginx Service
    depends_on:
      - web
    networks:
     # - frontend
      - backend
    command: [nginx-debug, '-g', 'daemon off;']

networks:
  frontend:
  backend:

volumes:  # Definition des gemeinsamen Volumes
  daphne-socket:

This is the nginx.conf (which is successfully transfered to here: /etc/nginx/conf.d/default.conf, I can see the file and content in docker desktop):

upstream daphne_server {
    server unix:/tmp/daphne.sock;
}

server {
    location /static/ {
        alias /static/;
    }

    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://daphne_server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        #proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-Forwarded-Host $server_name;
        #proxy_set_header X-Forwarded-Proto $scheme;
    }

    proxy_connect_timeout 10s;
    proxy_send_timeout 10s;
    proxy_read_timeout 10s;

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

I also tried this variation of the nginx.conf:

upstream web {
  ip_hash;
  server web:8000;
}

server {
    location /static/ {
        autoindex on;
        alias /static/;
    }

    location / {
        proxy_pass http://web;
    }
    listen 80;
    server_name localhost;
}

and this is the console output when I start docker (I dont see any problem, also I dont get any feedback when I try to access localhost):

PS C:\GitHub\fox_v3\backend> docker-compose up     
[+] Running 8/8
 ✔ nginx 7 layers [⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                              7.1s 
   ✔ 52d2b7f179e3 Already exists                                                0.0s 
   ✔ fd9f026c6310 Pull complete                                                 4.9s 
   ✔ 055fa98b4363 Pull complete                                                 4.9s 
   ✔ 96576293dd29 Pull complete                                                 4.9s 
   ✔ a7c4092be904 Pull complete                                                 5.0s 
   ✔ e3b6889c8954 Pull complete                                                 5.0s 
   ✔ da761d9a302b Pull complete                                                 5.0s 
[+] Running 5/4
 ✔ Network backend_backend         Created                                      0.1s 
 ✔ Volume "backend_daphne-socket"  Created                                      0.0s 
 ✔ Container backend-redis-1       Created                                      0.1s 
 ✔ Container backend-web-1         Created                                      0.0s 
 ✔ Container backend-nginx-1       Created                                      0.0s 
Attaching to backend-nginx-1, backend-redis-1, backend-web-1
backend-redis-1  | 1:C 23 Aug 2023 20:02:27.622 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
backend-redis-1  | 1:C 23 Aug 2023 20:02:27.623 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
backend-redis-1  | 1:C 23 Aug 2023 20:02:27.623 * Redis version=7.2.0, bits=64, commit=00000000, modified=0, pid=1, just started
backend-redis-1  | 1:C 23 Aug 2023 20:02:27.623 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
backend-redis-1  | 1:M 23 Aug 2023 20:02:27.623 * monotonic clock: POSIX clock_gettime
backend-redis-1  | 1:M 23 Aug 2023 20:02:27.623 * Running mode=standalone, port=6379.
backend-redis-1  | 1:M 23 Aug 2023 20:02:27.624 * Server initialized
backend-redis-1  | 1:M 23 Aug 2023 20:02:27.625 * Ready to accept connections tcp    
backend-web-1    | Starting RQ Worker...
backend-web-1    | Removing old socket...
backend-web-1    | Starting Daphne Server...
backend-nginx-1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
backend-nginx-1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
backend-nginx-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
backend-nginx-1  | 10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
backend-nginx-1  | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
backend-nginx-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
backend-nginx-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
backend-nginx-1  | /docker-entrypoint.sh: Configuration complete; ready for start up 
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: using the "epoll" event method
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: nginx/1.25.2
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: OS: Linux 5.15.90.1-microsoft-standard-WSL2
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker processes
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 21
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 22
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 23
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 24
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 25
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 26
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 27
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 28
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 29
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 30
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 31
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 32
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 33
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 34
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 35
backend-nginx-1  | 2023/08/23 20:02:28 [notice] 1#1: start worker process 36
backend-web-1    | Starting server at unix:/tmp/daphne.sock
backend-web-1    | HTTP/2 support not enabled (install the http2 and tls Twisted extras)      
backend-web-1    | Configuring endpoint unix:/tmp/daphne.sock
backend-web-1    | Worker rq:worker:b577daa603b24448a5187de720b5ba6f started with PID 7, version 1.15.1
backend-web-1    | Subscribing to channel rq:pubsub:b577daa603b24448a5187de720b5ba6f
backend-web-1    | *** Listening on default...
backend-web-1    | Cleaning registries for queue: default
backend-web-1    | Cleaning registries for queue: default

and this is the dockerfile for the app:

FROM python:3.10-slim

WORKDIR /app

# Kopieren des Projekts
COPY . /app

# Installieren von pipenv und den Projekt-Abhängigkeiten
RUN pip install pipenv && pipenv install --deploy --ignore-pipfile

EXPOSE 8000

# Das Startkommando. Wir werden später ein separates Startskript verwenden.
CMD ["./start_container.sh"]

and the start_container.sh:

#!/bin/bash

# Da es sich um einen Docker-Container handelt, brauchen wir nicht nach Redis oder Nginx zu suchen. 
# Sie würden in separaten Containern laufen.

# Starte den RQ-Worker im Hintergrund
echo "Starting RQ Worker..."
pipenv run python manage.py rqworker &

# Socket löschen, falls er bereits existiert
echo "Removing old socket..."
rm -f /tmp/daphne.sock

# Starte den Daphne Server
echo "Starting Daphne Server..."
pipenv run daphne -u /tmp/daphne.sock patentfox.asgi:application 

# Ändern Sie die Berechtigungen des Sockets, damit andere Dienste darauf zugreifen können
chmod 666 /tmp/daphne.sock

echo "Fehler beim Starten des Daphne Servers."

As you can see I added and removed several lines in the code, which have been pretty much useless. My main problem is, that I dont get any error or logs from nginx to better understand the problem.

I would really appreciate if somebody can help, regards

Ken
  • 1
  • 1
  • Are you sure `proxy_pass http://daphne_server;` is correct? I think this should be `proxy_pass http://app:8000;`. – msrumon Aug 23 '23 at 21:08
  • I edited my question and added another nginx.conf, which I tried. It seems to me to go in the same direction, but I can try your proposal in the evening, thanks. – Ken Aug 24 '23 at 08:47

1 Answers1

0

I had nginx running in parallel local :-(

Ken
  • 1
  • 1
  • Answers (and questions) should be written such that future users can learn from them, too. Please either [edit] this answer to provide details about what was wrong, or consider deleting your question entirely (I think you'll have to delete this answer first). – ChrisGPT was on strike Aug 28 '23 at 21:27