您的位置:首页 > 运维架构 > Nginx

nginx跟据url进行分流

2014-12-04 10:23 253 查看
本文转自:http://blog.csdn.net/liukeforever/article/details/8282401
方便以后查阅,MARK下。
需求让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; }

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: