NGINX SSL Reverse Proxy Xác minh SSL ngược dòng

Aug 18 2020

Tôi đã thiết lập NGINX làm proxy ngược để lưu trữ nhiều trang web chỉ sử dụng một địa chỉ IP. Tôi có chứng chỉ Cho phép mã hóa trên proxy và chứng chỉ Cho phép mã hóa khác trên máy chủ ngược dòng. Về cơ bản, tôi cần NGINX để chuyển tiếp lưu lượng đến máy chủ ngược dòng và xác minh rằng máy chủ ngược dòng có chứng chỉ TLS hợp lệ. Nếu tôi vô hiệu hóa proxy_ssl_verify, nó sẽ hoạt động. Nếu tôi truy cập https://app.local.example.comvào mạng nội bộ của mình, ứng dụng hoạt động tốt.

Giản đồ hệ thống:

https://app.example.com --> NGINX Reverse Proxy --> https://app.local.example.com (Local IP Address)

Tệp cấu hình proxy ngược NGINX:

server {
    listen 80;
    rewrite ^ https://$host$request_uri? permanent;
}

server {
        server_name app.example.com;

        listen                                  443 ssl;

        ssl_certificate                         /etc/letsencrypt/live/app.example.com/cert.pem;
        ssl_certificate_key                     /etc/letsencrypt/live/app.example.com/privkey.pem;
        ssl_trusted_certificate                 /etc/letsencrypt/live/app.example.com/chain.pem;

        location / {

                proxy_redirect off;
                proxy_set_header Host $http_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-Ssl on; proxy_set_header X-Forwarded-Protocol $scheme;
                proxy_set_header X-Forwarded-HTTPS on;

                proxy_ssl_session_reuse        off;
                proxy_ssl_name                 app.local.example.com

                proxy_ssl_verify               on;
                proxy_ssl_verify_depth         2; # I've tried 1,2,3,4
                proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; 
                            
                proxy_pass                     https://app.local.example.com
        }
}

Đây là thông báo lỗi mà tôi nhận được.

[error] 1087#1087: *2 upstream SSL certificate verify error: (20:unable to get local issuer certificate) while SSL handshaking to upstream, client: [Client IP], server: app.example.com, request: "GET / HTTP/1.1", upstream: "https://192.168.1.5:443/", host: "app.local.example.com", referrer: "https://app.example.com/">

Phiên bản OpenSSL: OpenSSL 1.1.1f 31 Mar 2020

nginx -v: nginx version: nginx/1.18.0 (Ubuntu)

cat / etc / issue: Ubuntu 20.04.1 LTS \n \l

Đầu ra của openssl s_client -connect app.local.example.com:443

CONNECTED(00000003)
depth=0 CN = app.local.example.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = app.local.example.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:CN = app.local.example.com
   i:C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIFeDCCBGCgAwIBAgISA8NkbZ6wz2EnKcedXujKT9AmMA0GCSqGSIb3DQEBCwUA
...
-----END CERTIFICATE-----
...
SSL-Session:
    Protocol  : TLSv1.3
    ...
    Verify return code: 21 (unable to verify the first certificate)

Trả lời

2 SteffenUllrich Aug 18 2020 at 02:42
Certificate chain
 0 s:CN = app.local.example.com
   i:C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3

Máy chủ của bạn không được định cấu hình đúng. Thay vì gửi chứng chỉ lá (app.local.example.com) và chứng chỉ trung gian, nó chỉ gửi chứng chỉ lá. Do chứng chỉ trung gian bị thiếu, không có đường dẫn tin cậy nào có thể được tạo tới neo tin cậy cục bộ, có nghĩa là việc xác thực chứng chỉ không thành công với "không thể lấy chứng chỉ của tổ chức phát hành cục bộ" . Các trình duyệt thường giải quyết các vấn đề như vậy bằng cách lấy chứng chỉ trung gian bị thiếu từ một nơi khác, nhưng các ngăn xếp TLS khác thường không giải quyết như vậy.

Khi bạn đã định cấu hình máy chủ đúng cách, bạn sẽ thấy phần sau openssl s_client. Khi điều này được thực hiện, nginx cũng sẽ hoạt động.

Certificate chain
 0 s:CN = app.local.example.com
   i:C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
 1 s:C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
   i:O = Digital Signature Trust Co., CN = DST Root CA X3
MarcWoodyard Aug 18 2020 at 13:17

Tôi đã thay đổi ssl_certificategiá trị từ cert.pemthành fullchain.pemtrên máy chủ ngược dòng.

ssl_certificate         /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/app.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/app.example.com/chain.pem;

Đây là liên kết đến tệp cấu hình NGINX mà tôi đã sử dụng