需求让Nginx跟据请求的url不同,而转发到不同的server上
举例:
url http://127.0.0.1/json/0769/
url http://127.0.0.1/json/0734/
需求把url后缀为0769的请求转发到server1, url 后缀为0734的请求转发到server1
nginx.conf实现代码
-
worker_processes 2;
-
#error_log /var/log/nginx/error.log;
-
#pid /var/run/nginx.pid;
-
events {
-
worker_connections 1024;
-
use epoll;
-
}
-
http {
-
charset utf-8;
-
map $zone $up_stream {
-
^~0769 frontends_0769;
-
^~0734 frontends_0734;
-
default frontends_0769;
-
}
-
# Enumerate all the Tornado servers here
-
upstream frontends_0769 {
-
server 127.0.0.1:3333;
-
#server 127.0.0.1:3334;
-
#server 127.0.0.1:3335;
-
#server 127.0.0.1:3336;
-
}
-
upstream frontends_0734 {
-
server 127.0.0.1:3334;
-
#server 127.0.0.1:3334;
-
#server 127.0.0.1:3335;
-
#server 127.0.0.1:3336;
-
}
-
include mime.types;
-
default_type application/octet-stream;
-
#access_log /var/log/nginx/access.log;
-
keepalive_timeout 65;
-
proxy_read_timeout 200;
-
sendfile on;
-
tcp_nopush on;
-
tcp_nodelay on;
-
gzip on;
-
gzip_min_length 1000;
-
gzip_proxied any;
-
gzip_types text/plain text/css text/xml
-
application/x-javascript application/xml
-
application/atom+xml text/javascript;
-
# Only retry if there was a communication error, not a timeout
-
# on the Tornado server (to avoid propagating "queries of death"
-
# to all frontends)
-
proxy_next_upstream error;
-
server {
-
listen 8089;
-
location / {
-
proxy_pass_header Server;
-
proxy_set_header Host $http_host;
-
proxy_redirect off;
-
proxy_set_header X-Real-IP $remote_addr;
-
proxy_set_header X-Scheme $scheme;
-
#proxy_pass http://frontends;
-
#if ( $request_uri ~* /json/(\d\d\d\d)\d+/ ) {
-
# set $zone $1;
-
# proxy_pass http://frontends_$zone;
-
#}
-
if ( $request_uri ~ ^/json/(\d+)/$ ) {
-
set $zone $1;
-
proxy_pass http://$up_stream;
-
}
-
}
-
}
-
}