如何在localhost上使用nginx反向代理

new9mtju  于 2023-06-21  发布在  Nginx
关注(0)|答案(1)|浏览(133)

我的理解是,在这个设置中,我在localhost上运行一个WebSocket服务器(节点),并使用位于它前面的Nginx反向代理来处理握手和持久连接。
我面临的问题是:
混合含量:位于“domain.com”的页面已通过HTTPS加载,但试图连接到不安全的WebSocket终结点“ws://domain.com:8020/”。此请求已被阻止;此端点必须通过WSS可用。
未捕获的DOMException:无法构造“WebSocket”:不安全的WebSocket连接不能从通过HTTPS加载的页面启动。
那么,我是否还需要为WebSocket提供另一个证书?
下面是nginx的配置:

http {
...

upstream websocket {
    server 127.0.0.1:8080;
}

    server {
        listen 8020;

        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_read_timeout 1d;
            proxy_send_timeout 1d;
        }
    }
    
    server {
        listen 80 default_server;
        listen [::]:80 default_server;

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        ssl_certificate     /location/cert.pem;
        ssl_certificate_key /location/cert-key.pem;

        root /var/www/html/app;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {        
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }

    }

}
----------------------------------------
Node:
const wss = new WebSocketServer({port: 8080})

Client:
const socket = new WebSocket('ws://domain.com:8020')

应该注意的是,在使用http时,一切都正常工作,只是不确定如何使它与https一起工作。

0aydgbwb

0aydgbwb1#

找到了,只是在http块中移动ssl_certificatesl_certificate_key,nginx是新的,但我猜这样它们在两个服务器块中都继承了。
在js中也将ws更改为wss

http {
...

ssl on; # or use listen ssl on is deprecated

ssl_certificate     /location/cert.pem;
sl_certificate_key /location/cert-key.pem;

upstream websocket {
    server 127.0.0.1:8080;
}

    server {
        listen 8020;

        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_read_timeout 1d;
            proxy_send_timeout 1d;
        }
    }
    
    server {
        listen 80 default_server;
        listen [::]:80 default_server;

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        root /var/www/html/app;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {        
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }

    }

}
----------------------------------------
Node:
const wss = new WebSocketServer({port: 8080})

Client:
const socket = new WebSocket('wss://domain.com:8020')

相关问题