Назад | Перейти на главную страницу

Настройка нескольких серверных блоков nginx для обслуживания нескольких доменов с помощью приложения django

У меня есть отлично работающая конфигурация nginx, которая обслуживает приложение django, которое выглядит так:

server {

    listen 443 ssl;
    server_name domain.de;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    charset utf-8;
    ssl_stapling off;
    ssl_stapling_verify off;

    ssl_certificate            /etc/letsencrypt/live/domain.de/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/domain.de/privkey.pem;


    set $my_host $http_host;
    if ($http_host = "domain.de") {
          set $my_host "domain.de";
    }


    location / {
        proxy_pass http://django:5000;
        proxy_set_header Host $my_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

server {
        listen 80 ;
        server_name domain.de;
        return 301 https://domain.de$request_uri;
}
server {
        listen 80 ;
        server_name www.domain.de;
        return 301 https://domain.de$request_uri;
}
server {
        listen 443 ;
        server_name www.domain.de;
        return 301 https://domain.de$request_uri;
        ssl_stapling off;
        ssl_stapling_verify off;

        ssl_certificate           /etc/letsencrypt/live/domain.de/fullchain.pem;
        ssl_certificate_key       /etc/letsencrypt/live/domain.de/privkey.pem;
}

Я хочу добавить новый домен (скажем, domain.com). Сертификаты уже получены. Независимо от того, как я добавляю еще один серверный блок, я получаю ответ 400 о том, что сертификат недействителен.

РЕДАКТИРОВАТЬ: Я запускаю nginx в docker-контейнере:

nginx:
    container_name: 'nginx'
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes: ##The following files are found on the linux server.
       - /root/nginx-conf/hbe.conf:/etc/nginx/conf.d/default.conf
       - /root/nginx-conf/main.conf:/etc/nginx/nginx.conf
       - /etc/letsencrypt/live/domain.de/fullchain.pem:/etc/letsencrypt/live/domain.de/fullchain.pem
       - /etc/letsencrypt/live/domain.de/privkey.pem:/etc/letsencrypt/live/domain.de/privkey.pem
       - /etc/letsencrypt/live/domain.com/fullchain.pem:/etc/letsencrypt/live/domain.com/fullchain.pem
       - /etc/letsencrypt/live/domain.com/privkey.pem:/etc/letsencrypt/live/domain.com/privkey.pem
    depends_on:
      - django

Я попытался или пример скопировать главный серверный блок, регулируя доменное имя и расположение сертификата:

server {

    listen 443 ssl;
    server_name domain.de;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    charset utf-8;
    ssl_stapling off;
    ssl_stapling_verify off;

    ssl_certificate            /etc/letsencrypt/live/domain.de/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/domain.de/privkey.pem;


    set $my_host $http_host;
    if ($http_host = "domain.de") {
          set $my_host "domain.de";
    }


    location / {
        proxy_pass http://django:5000;
        proxy_set_header Host $my_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

server {

    listen 443 ssl;
    server_name domain.com;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    charset utf-8;
    ssl_stapling off;
    ssl_stapling_verify off;

    ssl_certificate            /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/domain.com/privkey.pem;


    set $my_host $http_host;
    if ($http_host = "domain.com") {
          set $my_host "domain.com";
    }


    location / {
        proxy_pass http://django:5000;
        proxy_set_header Host $my_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}


Я также попытался скопировать остальные блоки сервера, настроив URL-адрес, но как бы я это ни делал, я все равно получаю 400 неверных запросов.

Я действительно не эксперт в nginx, был бы очень признателен, если бы мне кто-нибудь помог. Большое спасибо заранее!