Dockerized NGINX Обратный прокси SSL - PullRequest
0 голосов
/ 21 февраля 2020

Итак, у меня есть технический стек, который составляет:

  • NodeJS API Backend
  • ReactJS Внешний интерфейс
  • NGINX Обратный прокси
  • MongoDB (размещен на Mon go Atlas)

API-интерфейс, ReactJS app и NGINX обратный прокси-сервер настроены с использованием docker -compose. Таким образом, они все находятся в отдельных контейнерах.

У меня проблемы с настройкой NGINX веб-сервера для SSL. Я пробовал различные уроки безрезультатно. Этот получит меня ближе всего. Но он не получится при вызове acme с ошибкой "отказано в соединении".

Так выглядит моя конфигурация nginx (nginx .dev.conf):

worker_processes  1;

events {
    worker_connections  1024;
}

http {

    sendfile             on;
    keepalive_timeout    65;
    client_max_body_size 5M;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Block alihack
    deny 23.27.103.106/32;

    upstream api {
        least_conn;
        server api:8080 max_fails=3 fail_timeout=30s;
    }

    upstream app {
        least_conn;
        server app:3000 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 80;

        location / {
            return 301 https://app;
        }

        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
    }

    server {
        listen 443 ssl;

        if ($request_method = 'OPTIONS') {
            return 200;
        }

        ssl_certificate /etc/letsencrypt/live/*censoredurl*/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/*censoredurl*/privkey.pem;

        # To allow POST on static pages
        error_page  405     =200 $uri;

        location / {
            proxy_pass http://app;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            break;
        }

        location ~* \.(eot|otf|ttf|woff|woff2)$ {
            add_header Access-Control-Allow-Origin *;
        }

        location ~ /api/(?<url>.*) {
            proxy_pass http://api/$url;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

        location /health-check {
            return 200;
            access_log off;
        }
    }

}

И мой docker состав:

version: '3.7'

services:
  server:
    build:
      context: ./server
      dockerfile: Dockerfile
    image: myapp-server
    ports:
      - '80:80'
    links:
      - api:api
      - app:app
    volumes:
      - ./server/certbot/conf:/etc/letsencrypt
      - ./server/certbot/www:/var/www/certbot
    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;'"
  app:
    container_name: app
    build:
      context: ./frontend
      dockerfile: Dockerfile
    volumes:
      - './frontend:/usr/app/frontend/'
      - '/usr/app/frontend/node_modules'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development
  api:
    container_name: api
    build:
      context: ./backend
      dockerfile: Dockerfile
    volumes:
      - './backend:/usr/app/backend/'
      - '/usr/app/backend/node_modules'
    ports:
      - '8080'
    environment:
      - NODE_ENV=development

EDIT Ошибка, которую я получаю

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for *domain*.com
http-01 challenge for www.*domain*.com
Using the webroot path /var/www/certbot for all unmatched domains.
Waiting for verification...
Challenge failed for domain *domain*.com
Challenge failed for domain www.*domain*.com
http-01 challenge for *domain*.com
http-01 challenge for www.*domain*.com
Cleaning up challenges
Some challenges have failed.

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: *domain*.com
   Type:   connection
   Detail: Fetching
   http://*domain*.com/.well-known/acme-challenge/76vCqW16lDgy00XfjLoSGg9WnX5a757Xbdj2lzO2lnY:
   Connection refused

   Domain: www.*domain*.com
   Type:   connection
   Detail: Fetching
   http://www.*domain*.com/.well-known/acme-challenge/0Sfl0yltK75pcp_NVWcUaJhGNKgaR2thfdRcUxuF7zA:
   Connection refused

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

Мой сервер работает правильно, когда у меня не установлена ​​SSL-конфигурация. Итак, я знаю, что мои настройки DNS работают как положено.

1 Ответ

0 голосов
/ 21 февраля 2020

nginx обрабатывает ваши маршруты местоположения сверху вниз , поэтому вы хотите, чтобы ваш конфиг acme был прежде всего

это моя конфигурация;

server { .... location ~ /\.well-known/acme-challenge { allow all; root /yourroot; try_files $uri $uri/ /$uri =418; } location / { return 301 https://$host$request_uri; } .... }

...