在 socket.io 的情况下出现错误“XMLHttpRequest 无法加载 /socket.io”

Posted

技术标签:

【中文标题】在 socket.io 的情况下出现错误“XMLHttpRequest 无法加载 /socket.io”【英文标题】:Getting error 'XMLHttpRequest cannot load /socket.io' in case of socket.io 【发布时间】:2015-05-05 06:25:14 【问题描述】:

我收到错误消息:“XMLHttpRequest cannot load /socket.io” for socket call。有什么问题,我不明白。我是网络套接字的新手,我的其他 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
    );

【问题讨论】:

【参考方案1】:

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

只需将此块附加到当前的 nginx 配置。

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

【讨论】:

以上是关于在 socket.io 的情况下出现错误“XMLHttpRequest 无法加载 /socket.io”的主要内容,如果未能解决你的问题,请参考以下文章

使用 Nginx 的 socket.io 出现 CORS 错误

即使我在服务器上允许它,Socket.io 也会出现 CORS 错误

将 socket.io 与 Mean 集成时反复出现 404 错误

通过 SSL 使用 Socket.io Node.js 出现网关错误

Node.js 和 socket.io 的 Redis 身份验证错误

在没有 node.js 的情况下独立使用 socket.io