@FRANZEE

Как подключить HTTPS к связке NGINX + Gunicorn + Flask?

Есть VPS с несколькими сайтами на Flask, конфиг nginx:
server {
        listen 80;
        server_name sitename.ru www.sitename.ru;
        location /.well-known {
		    root /home/username/sites/sitename.ru/flask_app/templates;
		}
        location / {
                include proxy_params;
                proxy_pass http://unix:/home/username/sites/sitename.ru/flask_app/sitename.sock;
    }
}


делал по этому туториалу - https://www.digitalocean.com/community/tutorials/h...

Теперь ума не приложу как подключить HTTPS посредством certbot https://certbot.eff.org/ ???
Попробовал кучу туториалов, но видимо я настолько криворук (((.

добавил во Flask
# wildcard route for ssl verification
@app.route('/', defaults={'path': ''})
@app.route('/.well-known/<path:path>')
def ssl_cert(path):
    return render_template('.well-known/' + path)


Создал тестовую страницу с этой директорией
127.0.0.1:5000/.well-known/test.html
работает

Допустим по этому тутору - https://habrahabr.ru/post/318952/
У меня затык с получением сертификатов после команды
certbot certonly --dry-run -d example.com -d www.example.com

sudo certbot certonly --dry-run -d sitename.ru -d www.sitename.ru
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Nginx Web Server plugin - Alpha (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
-------------------------------------------------------------------------------
Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 3
Plugins selected: Authenticator webroot, Installer None
Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for sitename.ru
http-01 challenge for www.sitename.ru
Input the webroot for sitename.ru: (Enter 'c' to cancel): /home/username/sites/sitename.ru/flask_app/templates/.well-known

Select the webroot for www.sitename.ru:
-------------------------------------------------------------------------------
1: Enter a new webroot
2: /home/username/sites/sitename.ru/flask_app/templates/.well-known
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Waiting for verification...
Cleaning up challenges
Failed authorization procedure. www.sitename.ru (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://www.sitename.ru/.well-known/acme-challenge/2E4cltwPGNZR-Z5lJZfYES5q0fQPCyg6mbtaJNccp-A: "\ufeff<!DOCTYPE html>
<meta charset="utf-8">
<html lang="ru">
<head>
    <title>\u041e\u0410\u041e \xab\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438\xbb ", sitename.ru (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://sitename.ru/.well-known/acme-challenge/ipbROalh3pstmv_4wA0EzgO0nu8zAk3Hf06k0Wu8bYc: "\ufeff<!DOCTYPE html>
<meta charset="utf-8">
<html lang="ru">
<head>
    <title>\u041e\u0410\u041e \xab\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438\xbb "

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

   Domain: www.sitename.ru
   Type:   unauthorized
   Detail: Invalid response from
   http://www.sitename.ru/.well-known/acme-challenge/2E4cltwPGNZR-Z5lJZfYES5q0fQPCyg6mbtaJNccp-A:
   "<!DOCTYPE html>
   <meta charset="utf-8">
   <html lang="ru">
   <head>
       <title>Welcome to site "

   Domain: sitename.ru
   Type:   unauthorized
   Detail: Invalid response from
   http://sitename.ru/.well-known/acme-challenge/ipbROalh3pstmv_4wA0EzgO0nu8zAk3Hf06k0Wu8bYc:
   "<!DOCTYPE html>
   <meta charset="utf-8">
   <html lang="ru">
   <head>
       <title>Welcome to site "

   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.


letsencrypt.log
https://pastebin.com/bJmcz4X7

Дайте подсказку плиз.
  • Вопрос задан
  • 1350 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Maksimkk
Все делается средствами nginx.
server {
        listen 443 ssl http2;
        keepalive_timeout   70;
        server_name ...;
        ssl_certificate     /etc/letsencrypt/live/.../fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/.../privkey.pem;
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 10m;

        ssl_prefer_server_ciphers on;
        ssl_dhparam /etc/nginx/dhpdo.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
        add_header Access-Control-Allow-Origin *;

        location / {
                uwsgi_pass      unix:///run/uwsgi/app/path/socket;
                include         uwsgi_params;
                uwsgi_read_timeout 600;
        }

        location /static/ {
                alias /var/web/static/;
                expires 30h; # кешируем в браузере на 30 часов
        }
        location /.well-known/ {
                alias /tmp/.well-known/;
                expires 0;
        }

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


Далее в certbot указываем
sudo certbot certonly -a webroot --webroot-path=/tmp -d example.site -d www.example.site


Данный способ никак не затрагивает сам сервер при обновлении сертификата.

Есть официальная инструкция по настройке, которая генерирует свой конфиг для nginx. https://certbot.eff.org
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы