【问题标题】:Getting error 'XMLHttpRequest cannot load /socket.io' in case of socket.io在 socket.io 的情况下出现错误“XMLHttpRequest 无法加载 /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
    });

【问题讨论】:

    标签: node.js sockets nginx websocket


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 2017-03-06
      • 2013-01-06
      • 2013-10-19
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2013-10-20
      相关资源
      最近更新 更多