我有两组接收websocket(socket.io)连接的应用程序。示例uri字符串可能是
http://myurl.com/socket.io/?EIO=3&transport=polling&t=1435096473367-0&b64=1&type=device
我需要使用一个上游的查询参数/值type=device
路由的所有请求,并且所有其他请求都转到另一个上游。
我在#nginx irc中被告知使用if
是解析查询参数的最佳方法,所以这就是我提出的:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /opt/nginx/conf/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# My settings
##
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
proxy_temp_path /var/tmp;
upstream normal_upstream {
least_conn;
server 127.0.0.1:9000 fail_timeout=20s;
#server 127.0.0.1:9001 fail_timeout=20s;
#server 127.0.0.1:9002 fail_timeout=20s;
#server 127.0.0.1:9003 fail_timeout=20s;
}
upstream device_upstream {
least_conn;
server 127.0.0.1:8000 fail_timeout=20s;
#server 127.0.0.1:8001 fail_timeout=20s;
#server 127.0.0.1:8002 fail_timeout=20s;
#server 127.0.0.1:8003 fail_timeout=20s;
}
server {
client_max_body_size 10m;
listen 443 ssl;
ssl_certificate /root/ssl/bundle.crt;
ssl_certificate_key /root/ssl/myurl.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name myurl.com www.myurl.com;
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
set $upstream $normal_upstream;
if($arg_type = device){
set $upstream $device_upstream;
}
proxy_pass https://$upstream;
}
}
}
简而言之,我正在使用
set $upstream $my_upstream;
if ( $arg_type = device ){
$upstream $device_upstream;
}
proxy_pass http://$upstream;
基本上,如果arg_type设置为device或者为空,则会更改上游。我相信这会奏效,但我不确定这是否是解决问题的正确方法。