Nginx/Docker - ... ディレクティブはここでは許可されません

Nginx/Docker - ... ディレクティブはここでは許可されません

Docker プロジェクトで Nginx に問題があります。ここには同様の投稿が多数あることは知っていますが、どれも役に立ちませんでした。

私は Docker で Nginx を使用しており、Web サイトは機能していますが、スケーリングとセキュリティの機能を追加し、パフォーマンスのために記述されたコードを改善したいと考えていました。これを行うには、http ブロックを追加し、イベント ブロックを定義したいと思います。

これらを追加しようとすると、次のエラーが発生します:

[emerg] 7#7: "events" directive is not allowed here in /etc/nginx/conf.d/app.conf:1

または、イベント セクションを削除すると次のようになります。

[emerg] 7#7: "http" directive is not allowed here in /etc/nginx/conf.d/app.conf:13

何らかの理由で、/etc/nginx/ ディレクトリに sites-enabled フォルダーと sites-available フォルダーがありません。これが重要かどうかはわかりません。

ただし、私の app.conf ファイルは次のようになります (問題なく動作します)。

#events {
#    worker_processes    auto;
#    worker_connections  1024;
#    worker_rlimit_nofile 2048;
#    multi_accept        on;
#    mutex_accept        on;
#    mutex_accept_delay  500ms;
#    use                 epoll;
#    epoll_events        512;
#}

#http {

   #access_log  logs/access.log   combined;
   #error_log   logs/warn.log     warn;

upstream app {
    least_conn;
    server app_2:8001;
    server app_2:443;
    server app:8000;
    server app:443;
}




server {
    listen 80;
    server_name find-your-spirits.de;
    location / {
        return 301 https://$host$request_uri;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
}
}
server {
    listen 443 ssl;
    server_name find-your-spirits.de;


    location / {
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /app/staticfiles/;
        add_header Access-Control-Allow-Origin *;
    }



    ssl_certificate /etc/letsencrypt/live/find-your-spirits.de/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/find-your-spirits.de/privkey.pem;

    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
#}

# を削除すると、前述したエラーが発生します。

これは私の Dockerfile です:

FROM nginx:1.19.0

RUN rm /etc/nginx/conf.d/default.conf
COPY app.conf /etc/nginx/conf.d

これは私の docker-compose ファイルです:

version: '3'
services:
  app:
    image: django
    build:
      context: ./app
      dockerfile: Dockerfile
    env_file:
      - ./.test.env
    volumes:
      - ./app/:/app/
      - ./app/staticfiles/:/app/staticfiles
      - ./data/.htpasswd:/etc/nginx/.htpasswd
    command: gunicorn --bind 0.0.0.0:8000 --chdir /app/ Webserver.wsgi

  app_2:
    image: django
    build:
      context: ./app
      dockerfile: Dockerfile
    env_file:
      - ./.test.env
    volumes:
      - ./app/:/app/
      - ./app/staticfiles/:/app/staticfiles
      - ./data/.htpasswd:/etc/nginx/.htpasswd
    command: gunicorn --bind 0.0.0.0:8001 --chdir /app/ Webserver.wsgi

  nginx:
    image: nginx:1.19.0
    build:
      context: ./data/nginx
      dockerfile: Dockerfile
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./data/nginx:/etc/nginx/conf.d
      - ./app/staticfiles/:/app/staticfiles
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
      - ./data/.htpasswd:/etc/nginx/.htpasswd
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
  certbot:
    image: certbot/certbot
    volumes:
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"

  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.test.env
    environment:
      - POSTGRES_DB_PORT=${SQL_PORT}
      - POSTGRES_DB_HOST=${SQL_HOST}
      - POSTGRES_PASSWORD=${SQL_PASSWORD}
      - POSTGRES_USER=${SQL_USER}
      - POSTGRES_DB=${SQL_DATABASE}
    ports:
      - 5432:5432


volumes:
  postgres_data

詳細情報や他のファイルの内容が必要な場合は、コメントを残していただければ、後ほど追加します。

また、私は Nginx についてはまったくの初心者なので、何か間違った記述を見つけた場合は、遠慮なく修正または改善してください。

関連情報