简体   繁体   English

使Socket.io路径与NGiNX proxy_pass一起使用

[英]Making Socket.io path work with NGiNX proxy_pass

I would like to use NGiNX to proxy pass the following: 我想使用NGiNX代理传递以下内容:

https://something.com/node.js/foo/bar/baz

into this: 到这个:

https://something.com:3000/foo/bar/baz

I have successfully done it with the following NGiNX config: 我已经使用以下NGiNX配置成功完成了此操作:

location ~ /node.js/(.*) {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "";

    add_header Cache-Control "public";

    proxy_set_header   X-Forwarded-Host   $host;
    proxy_set_header   X-Forwarded-Server $host;
    proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_pass http://127.0.0.1:3000/$1$is_args$args;
}
location / {
    try_files $uri /index.php$is_args$args;
}

The problem is the following: when I instantiate socket.io with the default path, it serves socket.io.js here: 问题如下:当我使用默认路径实例化socket.io时,它将在此处服务socket.io.js

https://something.com/node.js/socket.io/socket.io.js

So far so good, but then this javascript tells the client to make requests here, which fail for obvious reasons: 到目前为止一切顺利,但是此javascript告诉客户端在这里发出请求,由于明显的原因失败:

https://something.com/socket.io/...

So then I try to instantiate socket.io with the path option like so: 因此,我尝试使用path选项实例化socket.io ,如下所示:

io.listen(server, { path: '/node.js/socket.io'} );

But the problem is that now the socket.io.js file is hosted here: 但是问题是现在在这里托管了socket.io.js文件:

https://something.com/node.js/node.js/socket.io/socket.io.js

Is there a way to tell socket.io.js where to host the file socket.io.js but still use the given path ? 有没有办法告诉socket.io.js在哪里托管文件socket.io.js但仍使用给定的path

What is the normal solution to this proxy_pass stuff with socket.io ?? socket.io解决此proxy_pass东西的正常解决方案是什么?

Well different ways to solve your issue but I would use below one 解决您问题的不同方法,但是我会使用以下一种方法

location ~ /node.js/(.*) {
    rewrite "^/node.js/node.js/(.*)$" /node.js/$1 last;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "";

    add_header Cache-Control "public";

    proxy_set_header   X-Forwarded-Host   $host;
    proxy_set_header   X-Forwarded-Server $host;
    proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_pass http://127.0.0.1:3000/$1$is_args$args;
}
location / {
    try_files $uri /index.php$is_args$args;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM