location = /sub_by_ip {
#channel id is the subscriber's IP address
nchan_subscriber;
nchan_channel_id $remote_addr;
}
location /sub_by_querystring {
#channel id is the query string parameter chanid
# GET /sub/sub_by_querystring?foo=bar&chanid=baz will have the channel id set to 'baz'
nchan_subscriber;
nchan_channel_id $arg_chanid;
}
location ~ /sub/(\w+)$ {
#channel id is the word after /sub/
# GET /sub/foobar_baz will have the channel id set to 'foobar_baz'
# I hope you know your regular expressions...
nchan_subscriber;
nchan_channel_id $1; #first capture of the location match
}
channel id 可以跟任何nginx的變數結合,像是第一個例子就是用ip當作channel id
location ~ /multisub/(\w+)/(\w+)$ {
nchan_subscriber;
nchan_channel_id "$1" "$2" "common_channel";
#GET /multisub/foo/bar will be subscribed to:
# channels 'foo', 'bar', and 'common_channel',
#and will receive messages from all of the above.
}
這個設定就支援同時訂閱三個channel
一個固定的 common_channel
另外兩個是變動的channel
但如果我要訂閱n個呢?
location ~ /multisub-split/(.*)$ {
nchan_subscriber;
nchan_channel_id "$1";
nchan_channel_id_split_delimiter ",";
#GET /multisub-split/foo,bar,baz,a will be subscribed to:
# channels 'foo', 'bar', 'baz', and 'a'
#and will receive messages from all of the above.
}