
我有一個 Django Web 應用程式與另一台伺服器共用外部連接埠 80 和 443。localhost
在沒有反向代理的情況下工作正常,但是當啟用它時,我遇到了各種錯誤。
如何讓反向代理正常運作?
反向代理.conf:
# SSL Certificate and other SSL configurations
SSLProxyEngine on
ProxyRequests on
SSLProxyVerify require
SSLProxyCheckPeerCN on
SSLProxyCheckPeerName on
SSLProxyCheckPeerExpire on
ProxyPreserveHost on
RequestHeader set X-Forwarded-Proto https
# Reverse Proxy Configuration
ProxyPass "/" "https://192.168.1.83/"
ProxyPassReverse "/" "https://192.168.1.83/"
# Additional SSL configurations if needed
我將所有內容重定向http
到https
上述代理並將其包含在我的 ssl-https conf 檔案中。網站運作正常沒有包含(即何時Include .../reverse-proxy.conf
被註解掉)。當包含反向代理時,我得到:
[Thu Jan 18 07:09:39.835368 2024] [ssl:error] [pid 46505:tid 133251102926528] [remote 192.168.1.83:443] AH02039: Certificate Verification: Error (20): unable to get local issuer certificate
[Thu Jan 18 07:09:39.835470 2024] [ssl:error] [pid 46505:tid 133251102926528] [remote 192.168.1.83:443] AH02040: Certificate Verification: Certificate Chain too long (chain has 2 certificates, but maximum allowed are only 1)
[Thu Jan 18 07:09:39.835773 2024] [proxy:error] [pid 46505:tid 133251102926528] (20014)Internal error (specific information not available): [client 119.74.38.81:51224] AH01084: pass request body failed to 192.168.1.83:443 (192.168.1.83), referer: https://acupunctureclassique.duckdns.org/
[Thu Jan 18 07:09:39.835832 2024] [proxy:error] [pid 46505:tid 133251102926528] [client 119.74.38.81:51224] AH00898: Error during SSL Handshake with remote server returned by /login/, referer: https://acupunctureclassique.duckdns.org/
[Thu Jan 18 07:09:39.835861 2024] [proxy_http:error] [pid 46505:tid 133251102926528] [client 119.74.38.81:51224] AH01097: pass request body failed to 192.168.1.83:443 (192.168.1.83) from 119.74.38.81 (), referer: https://acupunctureclassique.duckdns.org/
在前端:
Proxy Error
The proxy server could not handle the request
Reason: Error during SSL Handshake with remote server
Apache/2.4.58 (Ubuntu) Server at acupunctureclassique.duckdns.org Port 443
更新:
apachectl -S
VirtualHost configuration:
*:443 acupunctureclassique.duckdns.org (/etc/apache2/sites-enabled/acu-le-ssl.conf:2)
*:80 acupunctureclassique.duckdns.org (/etc/apache2/sites-enabled/acu.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
答案1
這裡的實際問題是——經過聊天討論——用戶使用 nginx 將流量代理到同一個 nginx 實例,創建了一個簡潔的重定向循環,最終導致太大的標頭錯誤訊息。
您將流量傳送到https://192.168.1.83
.您聲稱這是 Let's crypt 頒發的證書,但 LE 會絕不頒發證書192.168.1.83
,任何公共 CA 也不會頒發此類證書。請記住,一個有效的證書還不夠,有符合預期的名稱 - 在本例中為192.168.1.83
.您的日誌對此也非常明確。
您有幾種選擇:
- 使用HTTP
- 使用自簽名憑證並使 Apache 信任它
SSLProxyCACertificate
指示。 - 使用有效的網域名稱並取得該網域的有效憑證。網域名稱可能解析為
192.168.1.83
,也可能添加到/etc/hosts
,但是 Apache將要符合憑證中的主機名稱和公用名稱(或 SAN)。 - 停用使用驗證姓名
SSLProxyCheckPeerName = off
禁用驗證使用SSLProxyVerify = none
,有效地禁用驗證。這或多或少相當於使用 http...
答案2
若要反向代理,您需要在 /etc/apache2/sites-available 中建立虛擬主機。這是此類 VirtualHost 配置的範例
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /path/to/your/django/static/files
Alias /static/ /path/to/your/django/static/files/
<Directory /path/to/your/django/static/files>
Require all granted
</Directory>
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /path/to/your/django/static/files
Alias /static/ /path/to/your/django/static/files/
<Directory /path/to/your/django/static/files>
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /path/to/your/ssl/certificate.crt
SSLCertificateKeyFile /path/to/your/ssl/private.key
SSLCertificateChainFile /path/to/your/ssl/chainfile.pem
ProxyPass / https://localhost:8000/
ProxyPassReverse / https://localhost:8000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
https 部分是可選的,如果不需要 https,可以省略