[英]Run NodeJs application with socket.io using Nginx as proxy server on AWS EC2
I am trying to run NodeJs application with socket.io using Nginx as proxy server on AWS EC2 and getting this error: 我正在尝试使用Nginx作为AWS EC2上的代理服务器,通过socket.io运行NodeJs应用程序并收到以下错误消息:
Failed to load resource: the server responded with a status of 404 (Not Found) /socket.io/?EIO=3&transport=polling&t=MIsbMS_:1 无法加载资源:服务器以404(未找到)状态响应/socket.io/?EIO=3&transport=polling&t=MIsbMS_:1
I am following this page directives https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/ 我正在遵循此页面指令https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/
My nginx settings are as follows: 我的nginx设置如下:
upstream socket_nodes {
ip_hash;
server 127.0.0.1:8080;
}
server {
listen 80 ;
listen [::]:80 ;
root /home/ubuntu/NodejsProjects/Node3;
index index.html index.htm index.nginx-debian.html;
server_name mydomainname.com www.mydomainname.com;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://socket_nodes;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
My index.js has following code 我的index.js具有以下代码
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection',function (socket){
socket.on('test', function () {
console.log(data);
socket.emit('testCl', 'Test successful!!');
});
});
In my index.html I have 在我的index.html中
<p id='pp'></p>
<button id="Btn1" onclick="cc();">Get test results</button><br/>
<script>
var socket = io();
var cc=function(){
socket.emit('test');
};
socket.on('testCl', function (data) {
console.log(data);
$("#pp").text(data);
});
</script>
After spending hours searching net I tried many things without success in html file such as 花了几个小时在网上搜索后,我尝试了很多没有成功的HTML文件操作,例如
var socket = io();
var socket = io.connect('http://localhost');
var socket = io.connect('http://localhost:8080');
var socket = io.connect('http://www.mydomainname.com:8080');
var socket = io.connect('http://myprivateIP:8080');
var socket = io.connect('http://mypublicIP:8080');
Any help please . 请帮忙 。
Finally It is working. 终于可以了。
All I have to do is create a new file in /etc/nginx/sites-available by copying nginx default configration file 我要做的就是在/ etc / nginx / sites中创建一个新文件-通过复制nginx默认配置文件可用
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mynewfile
Edit this file and put nginx proxy settings as given above in it and save. 编辑该文件,并将上面给出的nginx代理设置放入其中并保存。
link this mynewfile to sites-available directory 将此mynewfile链接到站点可用目录
sudo ln -s /etc/nginx/sites-available/mynewfile /etc/nginx/sites-enabled/
Now tested nginx files: 现在测试了nginx文件:
sudo nginx -t
Restarted nginx 重新启动nginx
sudo systemctl restart nginx
Previously I was editing some existing file to work as reverse proxy for nodejs apps. 以前,我正在编辑一些现有文件以用作nodejs应用程序的反向代理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.