node.js - 在 socket.io 的情况下出现错误 'XMLHttpRequest cannot load/socket.io'

标签 node.js sockets nginx websocket

我收到错误:“XMLHttpRequest无法加载/socket.io”进行套接字调用。出了什么问题,我不明白。我是网络套接字新手,我的其他 api 调用运行良好,但 socket.io 调用不起作用。我已经为 nginx 安装了 ssl 证书,请帮帮我。

我的nginx配置是

server{
        listen 80;
        server_name example.com;
        return 301 https://$server_name$request_uri;
}
server {
 #listen 80;
listen 443  ssl;

        # Make site accessible from http://localhost/
        server_name www.example.com;
        ssl_certificate "/home/oodles4/sslcertificate/public.crt";
        ssl_certificate_key "/home/oodles4/sslcertificate/keyfile.key";
        proxy_set_header X-Forwarded-For $remote_addr;

        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";

 #location / {
                 root  "/home/oodles4/Documents/dcx/user/dev";
                #root  "/home/oodles4/Documents/dcx/explorer";
                index home.html home.htm;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
  #      }
        location /{
        fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
            fastcgi_param   PATH_INFO           $fastcgi_script_name;
                                                                                                                              9,51           2%
 #location / {
                 root  "/home/oodles4/Documents/dcx/user/dev";
                #root  "/home/oodles4/Documents/dcx/explorer";
                index home.html home.htm;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
  #      }
        location /{
        fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
            fastcgi_param   PATH_INFO           $fastcgi_script_name;

            fastcgi_param   SERVER_PROTOCOL        $server_protocol;
            fastcgi_param   QUERY_STRING        $query_string;
            fastcgi_param   REQUEST_METHOD      $request_method;
            fastcgi_param   CONTENT_TYPE        $content_type;
            fastcgi_param   CONTENT_LENGTH      $content_length;
            fastcgi_param   SERVER_ADDR         $server_addr;
            fastcgi_param   SERVER_PORT         $server_port;
            fastcgi_param   SERVER_NAME         $server_name;
            fastcgi_param   HTTPS               on;
            fastcgi_param   HTTP_SCHEME         https;
}
        location /admin{
                alias "/home/oodles4/Documents/dcx/admin/dev";
                index home.html home.htm;
        }

        location /api {
                proxy_pass http://localhost:8080/api;
        }

        location /admin/api{
                proxy_pass http://localhost:8080/api;
        }


 location /explorer{
                alias "/home/oodles4/Documents/dcx/explorer";
#                try_files $uri $uri/ /views/main/main.html;
                index dev/views/main/main.html;
      }

        location /explorer/api{
                proxy_pass    http://localhost:3000/api;
        }

当我执行“/explorer”时,我的所有 api 调用和套接字都会工作

var socket = io.connect('http://www.example.com:3000', {
      'reconnect': true,
      'reconnection delay': 500
    });

最佳答案

我猜你有服务器 block 来监听端口 80、443。 但你没有任何服务器 block 来监听 3000。 这里我假设您有任何应用程序在端口 3000 上运行。

只需将此 block 附加到当前 nginx 配置即可。

server {
     server_name example.com www.example.com;
     location / {
         proxy_pass    http://localhost:3000/;
     }
}

关于node.js - 在 socket.io 的情况下出现错误 'XMLHttpRequest cannot load/socket.io',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045640/

相关文章:

node.js - Dokku找不到 'webpack'

c++ - C++、Windows 中的简单 SSL 套接字

java - 在Java中确定多播数据包的剩余TTL

php - 网站显示错误的时区

nginx - 在 nginx 位置 block 中使用 try_files 和 rewrite

javascript - 重新格式化数组内的 JSON 对象以在 HTML 中显示它们

node.js - 您应该为应用程序用户使用 AWS IAM 角色和权限吗?

python - 扭曲的框架服务器作为客户端建立连接?

nginx - 配置 nginx 以在 Web 应用程序中提供 manifest.json 文件

javascript - 在 promise 中使用 Async/Awaits