Nginx使用

Nginx根据参数跳转到不同服务器

1.通过HTTP URL参数跳转

upstream servera {
        #weigth参数表示权值,权值越高被分配到的几率越大
        least_conn;
        server 10.0.0.134:8620 weight=1;         #服务器A
}
upstream serverb {
        #weigth参数表示权值,权值越高被分配到的几率越大
        least_conn;
        server 10.0.0.135:8620 weight=1;         #服务器B
}
#设定虚拟主机配置
server {
        #侦听80端口
        listen       80;
        #定义使用 www.army16.com访问
        server_name www.army16.com;

        access_log  /var/log/nginx/access.log  main;

        set $backend_server "servera";
        #根据URL参数判断
        if ( $arg_project ~ '^blog$' ) {
               set $backend_server  "serverb";
        }

        #默认请求
        location / {
            #定义服务器的默认网站根目录位置
            root   html;
            #定义首页索引文件的名称
            index  test.html index.html index.htm index.shtml index.jsp;

            proxy_pass http://$backend_server;
        }
}
http://www.army16.com?project=hao
http://www.army16.com?project=blog

2.通过HTTP Header跳转

第一种: 简单情况, 可以通过判断处理:

upstream servera {
        #weigth参数表示权值,权值越高被分配到的几率越大
        least_conn;
        server 10.0.0.134:8620 weight=1;         #服务器A
}

upstream serverb {
        #weigth参数表示权值,权值越高被分配到的几率越大
        least_conn;
        server 10.0.0.135:8620 weight=1;         #服务器B
}

#设定虚拟主机配置
server {
        #侦听80端口
        listen       80;
        #定义使用 www.army16.com访问
        server_name www.army16.com;

        access_log  /var/log/nginx/access.log  main;

        #set $backend_server "servera";
        #根据自定义Header(header-project)判断
        if ( $http_header_project ~ '^blog$' ) {
               set $backend_server "serverb";
        }

        #默认请求
        location / {
            #定义服务器的默认网站根目录位置
            root   html;
            #定义首页索引文件的名称
            index  test.html index.html index.htm index.shtml index.jsp;

            proxy_pass http://$backend_server;
        }
}
C:\Users\ArmyQin>curl --header "header-project:hao" http://www.army16.com
C:\Users\ArmyQin>curl --header "header-project:blog" http://www.army16.com

第二种: 复杂性, 比如一个project对应一台服务器, 多个project对应一台服务器.

#Nginx对于URL里的请求参数, 以$http_开头, 如: 请求Header是header-project, 到了Nginx接收就是$http_header_project, 把中间线变成下划线, 变成小写字母
#$serve_name是通过$http_header_project在map的key找到返回的value值, 如果$http_header_project = blog, 那$serve_name就等于10.0.0.134:8620

map $http_header_project $serve_name {
        default 10.0.0.135:8620;

        blog    10.0.0.134:8620;
        hao     10.0.0.135:8620;
        app     10.0.0.134:8620;
}
include servers/server.conf;


#设定虚拟主机配置
server {
        #侦听80端口
        listen       80;
        #定义使用 www.army16.com访问
        server_name www.army16.com;

        access_log  /var/log/nginx/access.log  main;

        #默认请求
        location / {
            #定义服务器的默认网站根目录位置
            root   html;
            #定义首页索引文件的名称
            index  test.html index.html index.htm index.shtml index.jsp;

            proxy_pass http://$serve_name; #$serve_name是在上面server.conf文件定义的变量.

        }
}
C:\Users\ArmyQin>curl --header "header-project:blog" http://www.army16.com
C:\Users\ArmyQin>curl --header "header-project:hao" http://www.army16.com

 

滚动至顶部