Skip to content

Instantly share code, notes, and snippets.

@mikeda
Last active May 12, 2019 23:21
Show Gist options
  • Save mikeda/4744675 to your computer and use it in GitHub Desktop.
Save mikeda/4744675 to your computer and use it in GitHub Desktop.
Nginxでクエリストリングを見て振り分け先を切り替える。 http://localhost/test.txt?para1=AAA ならバランサ1、 http://localhost/test.txt?para1=CCC ならバランサ2
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
### バランサ定義
upstream balancer01 {
server 192.168.1.62;
server 192.168.1.63;
}
upstream balancer02 {
server 192.168.1.62;
}
upstream balancer03 {
server 192.168.1.63;
}
### パラメータみてバランサ切り替え
map $arg_para1 $balancer {
AAA balancer01;
BBB balancer01;
CCC balancer02;
default balancer03;
}
### server定義
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://$balancer$request_uri;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment