Проксирование Nginx + node.js + socket.io. /socket.io/ отдает 404 на 80 порту
У меня есть 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]
С течением времени таких сообщений появляется все больше. Приложение, естественно, не работает. Для чистоты эксперимента заменила свое приложение на тестовое:
app.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');
});
game.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 порту тоже все работало нормально?
Ответы (1 шт):
Попробуйте так:
server {
listen 80;
...
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:3000/;
или
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
...
}
В документации предложено больше вариантов конфигураций.