Answer the question
In order to leave comments, you need to log in
How to set up long polling on nginx?
I'm trying to organize a primitive messaging through long-polling using nginx.
I took this article as an example: habrahabr.ru/post/252349
Problem: I can’t add a subscriber to /sub/ - the browser swears:
GET localhost/sub/subscriber1 404 (Not Found)
Although, as I understand it, this path must be processed nginx.
nginx config:
http {
server {
listen 80 default;
location ~ /\.ht {
deny all;
}
location / {
proxy_pass http://127.0.0.1:8080;
}
location /channels-stats {
# activate channels statistics mode for this location
push_stream_channels_statistics;
# query string based channel id
push_stream_channels_path $arg_id;
}
location /pub {
# activate publisher (admin) mode for this location
push_stream_publisher admin;
# query string based channel id
push_stream_channels_path $arg_id;
}
location ~ /sub/(.*) {
# activate subscriber (streaming) mode for this location
push_stream_subscriber;
# positional channel path
push_stream_channels_path $1;
push_stream_longpolling_connection_ttl 30s;
push_stream_last_received_message_time $arg_time;
push_stream_last_received_message_tag $arg_tag;
}
}
push_stream_shared_memory_size 32M;
...
var channelId = 'subscriber1';
var last_etag=0;
var last_time=null;
function new_message() {
$.ajax({
url: '/sub/' + channelId,
type: "GET",
dataType: 'json',
beforeSend: function(xhr){xhr.setRequestHeader('Etag', last_etag);xhr.setRequestHeader('Last-Modified', last_time);},
success: function(data, status, xhr) {
last_etag =xhr.getResponseHeader('Etag');
last_time =xhr.getResponseHeader('Last-Modified');
console.log(data);
setTimeout(new_message, 50);
}
})
}
new_message();
$ curl -s -v --no-buffer 'http://localhost/sub/my_channel_1'
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /sub/my_channel_1 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 404 Not Found
* Server nginx/1.2.6 is not blacklisted
< Server: nginx/1.2.6
< Date: Tue, 19 May 2015 13:35:12 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 288
< Connection: keep-alive
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /sub/my_channel_1 was not found on this server.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address>
</body></html>
* Connection #0 to host localhost left intact
Answer the question
In order to leave comments, you need to log in
Quote from the manual
First, matches are searched for in prefix locations, and only if nothing is found among them, then regexp.
So location / works for you instead of location ~ /sub/(.*) .
write like this
location / {
location ~ /sub/(.*) {
# activate subscriber (streaming) mode for this location
push_stream_subscriber;
# positional channel path
push_stream_channels_path $1;
push_stream_longpolling_connection_ttl 30s;
push_stream_last_received_message_time $arg_time;
push_stream_last_received_message_tag $arg_tag;
}
proxy_pass http://127.0.0.1:8080;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question