【问题标题】:How to proxy https to http and enable ssl?如何将https代理到http并启用ssl?
【发布时间】:2020-12-30 19:14:31
【问题描述】:

我已将我的应用程序配置为通过 WebSocket (WS) 协议进行通信,并且我想通过 https 加载应用程序。使用Nginx 设置 SSL 后出现以下错误:

混合内容:页面通过 HTTPS 加载,但尝试连接到不安全的 WebSocket 端点。

经过一番研究,我可以使用来自Nginxredirect 指令将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-ipproxy_pass。 任何帮助我在这里做错了什么?

【问题讨论】:

    标签: http ssl nginx websocket nginx-reverse-proxy


    【解决方案1】:

    你得到的错误无法在你的服务器配置中解决。

    它来自于从 HTTPS 服务的站点内部访问纯 HTTP 或 WS 资源。 因此,这是您提供的实际内容的问题,需要在那里修复。虽然您尝试的重定向最终可能会将不安全的访问重定向到安全的访问,但它不会改变它首先需要进行不安全的访问,因为混合内容已经被阻止。

    【讨论】:

    • 有没有办法代理 ws 端点?在location 指令中,我正在尝试proxy_pass http://ws-endpoint 但仍然没有运气。
    • @Yahya:这实际上很常见,well documented 告诉你如何做到这一点。
    • 谢谢,你能检查一下问题的更新部分吗?
    • @Yahya:请不要再把你的问题变成移动目标。但是既然你现在反向代理了你的 websocket,你当然需要通过反向代理访问它(即wss://proxy/,而不是像以前那样直接访问。
    • 对不起,我无法得到。你能解释一下在哪里/如何使用wss://proxy/吗?
    猜你喜欢
    • 2014-10-10
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2019-09-29
    • 2023-03-15
    相关资源
    最近更新 更多