NGINX. Certbot. Exchange. Настройка NGINX для обратного проксирования Exchange 2019.

Имеем Exchange 2019. Хотим настроить обратное проксирование до него через NGINX.

Установка NGINX

# Install Nginx
apt install -y nginx
# Enable Nginx
systemctl enable nginx
# Headers more - Required for Microsoft Exchange
apt-get install -y libnginx-mod-http-headers-more-filter

Установим Certbot и модуль Python:

# Install certbot and python module
apt install -y certbot python3-certbot-nginx

Удалим линк на сайт по умолчанию:

# Unlink the default site
unlink /etc/nginx/sites-enabled/default

Настройка обратного прокси Nginx для Microsoft Exchange

Создадим файл конфигурации:

nano /etc/nginx/sites-available/mail.domain.tld.conf

Вставим следующий код, чтобы могли получить сертификат через Certbot:

server {
    listen 80;
    listen [::]:80;
    server_name $mail.domain.tld;

    location / {
        proxy_pass https://$exchange_server;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Включим конфигурацию:

# Link the conf
ln -s /etc/nginx/sites-available/mail.domain.tld.conf /etc/nginx/sites-enabled/mail.domain.tld.conf

# Reload Nginx
systemctl reload nginx

Запускаем Certbot, чтобы получить сертификат Let’s Encrypt:

# Run certbot
sudo certbot

# Select the mail.domain.tld in the certbot menu
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: mail.domain.tld

# Follow the cerbot configuration and let certbot manage the Nginx configuration.
root@01:/home/user# certbot
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Which names would you like to activate HTTPS for?

1: mail.one.ru
2: mail.two.ru
3: mail.three.ru

Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter ‘c’ to cancel): 1
Requesting a certificate for mail.one.ru
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mail.one.ru/fullchain.pem
Key is saved at: /etc/letsencrypt/live/mail.one.ru/privkey.pem
This certificate expires on 2023-11-04.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for mail.one.ru to /etc/nginx/sites-enabled/mail.one.ru.conf
Congratulations! You have successfully enabled HTTPS on https://mail.one.ru

If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

После получения сертификата Let’s Encrypt, меняем содержимое файла конфигурации на следующий, чтобы включить шифрование (заменить $mail.domain.tld на имя внешнее имя, а $exchange_server на имя сервера Exchange):

server {
        if ($host = $mail.domain.tld) {
        return 301 https://$host$request_uri;
        } # managed by Certbot

        listen 80;
        listen [::]:80;

        server_name $mail.domain.tld;
        return 404; # managed by Certbot

        ssl_verify_client       off;

}
server {

        listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
        listen 443 ssl http2; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/$mail.domain.tld/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/$mail.domain.tld/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

        server_name $mail.domain.tld;

        ssl_verify_client       off;

        location /                              { proxy_pass https://$exchange_server; }
        location /owa                           { proxy_pass https://$exchange_server/owa; }
        location /exchange                      { proxy_pass https://$exchange_server/exchange; }
        location /ews                           { proxy_pass https://$exchange_server/ews; }
        location /mapi                          { proxy_pass https://$exchange_server/mapi; }
        location /rpc                           { proxy_pass https://$exchange_server/rpc; }
        location /oab                           { proxy_pass https://$exchange_server/oab; }
        location /autodiscover                  { proxy_pass https://$exchange_server/autodiscover; }
        location /Microsoft-Server-ActiveSync   { proxy_pass https://$exchange_server/Microsoft-Server-ActiveSync; }
        location /ecp                           { proxy_pass https://$exchange_server/ecp; }

        proxy_pass_request_headers on;
        proxy_pass_header Date;
        proxy_pass_header Server;

        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-Proto https;
        more_set_input_headers 'Authorization: $http_authorization';
        proxy_set_header Accept-Encoding "";
        more_set_headers -s 401 'WWW-Authenticate: Basic realm="$mail.domain.tld"';

        proxy_read_timeout 3600;

        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;

        access_log /var/log/nginx/$mail.domain.tld-access.log;
        error_log /var/log/nginx/$mail.domain.tld-error.log;

}

Проверяем конфигурацию Nginx и перезапускаем его:

# Verify Nginx configuration
nginx -t
# Reload Nginx configuration
systemctl reload nginx

Добавление доменов в существующий сертификат

Сначала проверим существующий сертификат и домены:

certbot certificates

Теперь заменим пробелы на запятые в перечне доменов и добавим ещё один, например two.ru:

certbot --expand -d one.ru,two.ru

После не забыть отредактировать файлы конфигурации Nginx, так как Certbot lj,fdbn

Удаление доменов из существующих сертификатов

Либо в интерактивном режиме:

certbot delete

Либо указать конкретный сертификат, из которого нужно удалить домены:

certbot delete --cert-name example.com

Ссылки

https://tizutech.com/how-to-setup-nginx-reverse-proxy-for-microsoft-exchange-with-lets-encrypt/

https://netexaminer.net/how-to-add-a-domain-to-existing-certificate-generated-by-let-s-encrypt-certbot.fl

Оставьте ответ

Ваш адрес email не будет опубликован.

÷ 2 = 5