我有node.js和socket.io在端口3000上运行。为了避免最后输入端口,我将来自该端口的请求代理到80,下面是相应的nginx配置:
server {
listen 80;
server_name http://мой_ip/;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
proxy_pass http://мой_ip:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}
location /assets/ {
alias /var/www/html/assets/;
}
location /node_modules/ {
alias /var/www/html/node_modules/;
}
location ^~ /pma/ {
alias /usr/share/phpmyadmin/;
index index.php index.html;
location ~ /pma(/.*\.php) {
include fastcgi_params;
fastcgi_param SERVER_NAME localhost;
fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
}
如果您输入:3000,一切都会开始正常。如果不输入,外观是一样的,但在浏览器控制台中是这样的:
GET http://мой_ip/socket.io/?EIO=4&transport=polling&t=OuAIRXk [HTTP/1.1 404 Not Found 55ms]
随着时间的推移,此类消息越来越多地出现。当然,该应用程序不起作用。为了实验的纯粹性,我用测试应用程序替换了我的应用程序:
应用程序.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/game.html');
});
//Whenever someone connects this gets executed
io.on('connection', function(socket) {
console.log('A user connected '+socket.id);
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected '+socket.id);
});
socket.on('clientEvent', function(data) {
console.log(data);
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});
游戏.html
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
</head>
<script src = "http://мой_ip/node_modules/socket.io/client-dist/socket.io.js"></script>
<script>
var socket = io();
socket.emit('clientEvent', 'Sent an event from the client!');
</script>
<body>Hello world</body>
</html>
浏览器控制台中的一切都是一样的。它只在端口 3000 上工作。我如何确保在端口 80 上也一切正常?