【发布时间】:2020-12-30 19:14:31
【问题描述】:
我已将我的应用程序配置为通过 WebSocket (WS) 协议进行通信,并且我想通过 https 加载应用程序。使用Nginx 设置 SSL 后出现以下错误:
混合内容:页面通过 HTTPS 加载,但尝试连接到不安全的 WebSocket 端点。
经过一番研究,我可以使用来自Nginx 的redirect 指令将HTTPS 重定向到HTTP。但是,它会通过HTTP(没有 SSL)加载应用程序。我也想启用 SSL。
经过进一步研究,我正在尝试使用以下配置代理https:
server {
listen 443;
server_name my.server.com;
ssl_certificate /etc/letsencrypt/live/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
location / {
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 $scheme;
proxy_pass http://127.0.0.1:8000;
proxy_read_timeout 86400;
}
}
server {
listen 8000;
root /home/ubuntu/app;
# Add index.php to the list if you are using PHP
index index.html;
}
但是,我仍然收到相同的“混合内容”错误。
在启用 SSL 的同时,有没有办法将 HTTPS 代理/重定向到 HTPP?
非常感谢
更新
在我的应用程序中,我使用 ws 端点进行设置,如下所示:
const web3 = new Web3 ('ws://server-ip:7546')
注意:我不能使用const web3 = new Web3 ('wss://server-ip:7546')
我已经更新了Nginx 这样的配置来代理上述 ws 端点:
server {
listen 443;
server_name my.app.com;
root /home/ubuntu/app;
index index.html;
ssl_certificate /etc/letsencrypt/live/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
location / {
proxy_pass http://server-ip:7546;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
我在我的应用程序中使用相同的server-ip 和proxy_pass。
任何帮助我在这里做错了什么?
【问题讨论】:
标签: http ssl nginx websocket nginx-reverse-proxy