(转)nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理

Nginx除了能够用做web服务器外,他还能够用来作高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案。nginx能够用轮询、IP哈希、URL哈希等方式调度后端服务器,同时也能提供健康检查功能。目前有众多公司均已经部署使用nginx实现基于七层的负载均衡功能。php

1)Nginx负载均衡
为了实现Nginx的反向代理以及负载均衡功能,应用中须要用到两个模块,HttpProxyModule和HttpUpstreamModule模块;其中HttpProxyModule模块的做用是将用户的数据请求转发到其余服务器上,HttpUpstreamModule模块是提供负载均衡技术。css

Nginx目前提供的负载均衡算法:
a)ngx_http_upstream_round_robin:加权轮询,可均分请求,是默认算法,集成在框架中。
b)ngx_http_upstream_ip_hash_module:IP哈希,可保持会话。
c)ngx_http_upstream_least_conn_module:最少链接数,可均分链接。
d)ngx_http_upstream_hash_module:一致性哈希,可减小缓存数据的失效。html

nginx的upstream负载均衡目前支持的几种方式:
a)轮询(默认) 
默认选项,当weight不指定时,各服务器weight相同, 每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除。前端

1
2
3
4
upstream bakend {
server 192.168.1.10;
server 192.168.1.11;
}

b)weight 
指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的状况。若是后端服务器down掉,能自动剔除。 好比下面配置,则1.11服务器的访问量为1.10服务器的两倍(后端节点中配置高的服务器能够适当将weight设置大点)。linux

1
2
3
4
upstream bakend {
server 192.168.1.10 weight=1;
server 192.168.1.11 weight=2;
}

c)ip_hash 
每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个后端服务器,能够解决session不能跨服务器的问题,实现session共享。若是后端服务器down掉,要手工处理。nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
upstream resinserver{
ip_hash;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
    
============================================================================================================
以前遇到的一个问题:Nginx反向代理两台tomcat应用服务器,发如今nginx的upstream负载配置里:
1)若是不配置ip_hash负载到后端的两台节点上,则访问后台页面,输入用户名和密码,点击 "登陆" 则无反应或没法访问后台!
2)若是不配置ip_hash负载到后端的任意一台单节点上(另外一台节点的负载配置注释掉),则访问后台页面,输入用户名和密码,点击 "登陆" 则会成功登陆进去!
3)配置ip_hash负载到后端的两台节点上,则访问后台页面,输入用户名和密码,点击 "登陆" 则会成功登陆进去!
     
以下配置,在后面两台机器192.168.10.20和192.168.10.21的tomcat端口(8080)都活着的状况下,前面nginx代理层的upstream配置里,
若是注释掉了ip_hash,则会形成访问http: //bpm .kevin.com页面,输入用户名和密码后,点击登录失败!
若是加上ip_hash,则登录就ok。
    
[root@kevin-lb vhosts] # cat bpm.kevin.com.conf
upstream os-8080 {
       ip_hash;
       server 192.168.10.20:8080 max_fails=3 fail_timeout=15s;
       server 192.168.10.21:8080 max_fails=3 fail_timeout=15s;
}
               
   server {
       listen      80;
       server_name bpm.kevin.com;
         
       access_log   /data/nginx/logs/bpm .kevin.com-access.log main;
       error_log   /data/nginx/logs/bpm .kevin.com-error.log;
         
  location / {
          proxy_pass http: //os-8080 ;
          proxy_redirect  http: //os-8080/  http: //bpm .kevin.com/;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
}
    
=================缘由分析============================================
当后端节点是动态应用服务器的话,前面的nginx负载层要用ip_hash作session共享,不然nginx负载默认是轮询策略(每一个请求按时间顺序逐一分配到不一样的后端服务器),
就是说当你打开登陆界面,输入用户名和密码,这时候请求是到后端的A机器上;当你点击 "登陆" ,按照轮询策略,此时请求就到了后端的B机器上。
因此会出现登陆无反应的现象。
====================================================================
    
再看下面一例
[root@kevin-lb vhosts] # cat portal.kevin.com.conf
server {
     listen 80;
     server_name  portal.kevin.com;
     access_log   /data/nginx/logs/portal .kevin.com-access.log main;
     error_log   /data/nginx/logs/portal .kevin.com-error.log;
     root   /data/web/webroot ;
      index index.html;
  
     location / {
         if  ($arg_response_for) {
           add_header X-Response-For $arg_response_for;
         }
  
         try_files $uri $uri/  /index .html;
     }
     location ~  /prod  {
         rewrite  /prod/ (.*)$ /$1  break ;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http: //10 .0.54.20:9040;
     }
  
     location  /portal-pc/captcha/login  {
         proxy_pass http: //10 .0.54.20:9040;
     }
     location  /portal-pc/captcha/mobile  {
         proxy_pass http: //10 .0.54.20:9040;
     }
  
     location  /portal-pc/login  {
         rewrite .* /?response_for=unlogined  break ;
     }
  
     location  /portal-pc/user/center  {
         rewrite .* /?response_for=logined  break ;
     }
  
     location  /msdp-file  {
         proxy_pass http: //10 .0.54.20:9020 /msdp-file ;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
     location  /upload  {
         proxy_pass http: //10 .0.54.20:9020 /upload ;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
}
  
[root@kevin-lb vhosts] # ll -d /data/web/webroot
drwxr-xr-x 4 www www 12288 7月  12 21:06  /data/web/webroot
[root@kevin-lb vhosts] # ll /data/web/webroot/index.html
-rw-r--r-- 1 www www 1348 7月  12 21:06  /data/web/webroot/index .html
 
如nginx反向代理后端两台apache应用,涉及到网站后台登陆。若是不使用ip_hash实现session共享,则会出现:
1)使用域名登陆后台,输入用户名和密码,第一次点击 "登陆" 没有反应,闪一下,第二次点击 "登陆" 才会登陆到后台。
2)使用后端apache本身的ip登陆后台,输入用户名和密码,点击 "登陆" 能够成功登陆到后台。
3)这就是由于nginx代理时,没有配置ip_hash形成的,即没有达到session共享效果。
输入用户名和密码的session信息保存到appche01机器上,点击 "登陆" 操做后倒是去apache02机器上拿session。
此时apache02上没有这个session,因此第一次会出现闪一下没有登陆到后台的现象。
第二次点击 "登陆" 后就轮到去apache01机器上拿session,此时正好apache01机器上有这个session,故第二次就能登陆到后台了。
所以,在nginx的代理配置中启用ip_hash,就能够实现session共享,则就不会出现上面这种现象了。

nginx代理配置中几个参数说明web

1
2
3
4
5
proxy_redirect off;                                             # 代理是否支持重定向
proxy_set_header X-Real-IP $remote_addr;                 # 远端真实ip地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    # 反向代理以后转发以前的ip地址
proxy_set_header Host $http_host;                     # http请求的主机域名
proxy_set_header X-NginX-Proxy  true ;                   # nginx代理

d)fair(第三方插件) 
按后端服务器的响应时间来分配请求,响应时间短的优先分配。正则表达式

1
2
3
4
5
upstream resinserver{
server 192.168.1.10:8080;
server 192.168.1.11:8080;
fair;
}

e)url_hash(第三方插件) 
按访问url的hash结果来分配请求,使每一个url定向到同一个后端服务器,后端服务器为缓存服务器时比较有效。 在upstream中加入hash语句,hash_method是使用的hash算法。算法

1
2
3
4
5
6
upstream resinserver{
server 192.168.1.10:8080;
server 192.168.1.11:8080;
hash  $request_uri;
hash_method crc32;
}

设备的状态: 
down            表示单前的server暂时不参与负载 
weight          权重,默认为1。 weight越大,负载的权重就越大。 
max_fails      容许请求失败的次数默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误 
fail_timeout   max_fails次失败后,暂停的时间。 
backup          备用服务器, 其它全部的非backup机器down或者忙的时候,请求backup机器。因此这台机器压力会最轻。apache

下面是负载均衡实现原理拓扑图(多层负载)

Nginx反向代理模块配置方法示例说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
location ~* \.(mp3|mp4)$ {         // 匹配URL以MP3或者MP4结尾的请求
     proxy_pass http: //localhost :8080         // 转发到本机8080端口
}
 
location / {                                     // 匹配任意URL
     proxy_pass http: //localhost :8081   // 转发到本机8081端口
         proxy_set_header    X-Forwarded-For    $remote_addr     // 保留用户真实IP
}
 
location指令能够直接匹配字符串,也能够进行正则表达式匹配
~表示区分大小写,~*表示不区分大小写匹配,=表示精确匹配
proxy_pass指令能够根据location匹配的状况创建先后端的代理映射关系
X-Forwarded-For用于实现定义数据包头,记录用户真实IP

Nginx负载均衡模块配置方法示例说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream backend {
ip_hash;
server web1. test .com weight 1;
server web2. test .com weight 2;
server web3. test .com ;
}
 
server {
listen 80;
server_name web. test .com;
location / {
proxy_pass http: //backend ;
}
}
 
upstream定义后端服务器集合,backend是服务器组名
proxy-pass和fastcgi_pass将请求转发给一组服务器
ip_hash能够根据用户ip地址的 hash 值分配固定的后端服务器

2)Nginx负载均衡案例解析
服务器名称                  网路配置
nginx.test.com           eth0:111.122.155.133
                               eth1:192.168.1.2
web1.test.com           eht0:192.168.1.3
web2.test.com           eth0:192.168.1.4
web3.test.com           eth0:192.168.1.5

3台web机器配置:

在web1 web2 web3上安装httpd并配置网卡。操做步骤以下:(下面是web1的操做,web2和web3的操做步骤相似。注意修改红色部分的参数)
# vim /etc/sysconfig/network-scripts/ifcfg-eth0

1
2
3
4
5
6
7
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.3
NETMASK=255.255.255.0
GATEWAY=192.168.1.2
ONBOOT= yes
TYPE=Ethernet

接着执行下面命令:

1
2
3
4
5
6
7
8
9
10
# service network restart
# yum install -y httpd
# iptables -F
# iptables -X
# service iptables save
# setenforce 0
# sed -i s/enforcing/disabled/g /etc/sysconfig/selinux
# echo "web1 192.168.1.3" > /var/www/html/index.html
# service httpd restart
# chkconfig httpd on

nginx代理服务器配置

# vim /etc/sysconfig/network-scripts/ifcfg-eth0

1
2
3
4
5
6
7
DEVICE=eth0
BOOTPROTO=static
IPADDR=111.122.155.133
NETMASK=255.255.255.0
GATEWAY=111.122.155.0
ONBOOT= yes
TYPE=Ethernet

# vim /etc/sysconfig/network-scripts/ifcfg-eth1

1
2
3
4
5
6
7
DEVICE=eth1
BOOTPROTO=static
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT= yes
TYPE=Ethernet

接着执行下面命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
# service network restart
# iptables -F
# iptables -X
# service iptables save
# setenforce 0
# sed -i s/enforcing/disabled/g /etc/sysconfig/selinux
 
# wget http://nginx.org/download/nginx-1.6.3.tar.gz
# tar zxf nginx-1.6.3.tar.gz -C /usr/src/
# yum install gcc pcre pcre-devel openssl openssl-devel gd gd-devel perl perl-ExtUtils-Embed
# cd /usr/src/nginx-1.6.3/
# ./configure --prefix=/usr/local/nginx --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_gzip_static_module --with-http_perl_module --with-mail_ssl_module
# make && make install

接着修改nginx配置文件
# vim /usr/local/nginx/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
user    nobody;
worker_processes    1;
error_log    logs /error .log    notice;
pid    logs /nginx .pid;
 
events    {
worker_connections    5024;
}
 
http    {
include    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" ' ;
sendfile    on;
tcp_nopush    on;
server_tokens    off;
keepalive_timeout    65;
keepalive_requests    100;
 
gzip     on;                                     // 开启压缩
gzip_min_length    1000;                 // 小于1000B内容不压缩
gzip_buffers    16    32k;                 // 压缩缓存的个数和容量
gzip_types    text /plain     application /xml ;         // 指定压缩文件类型
gzip_comp_level    2;                     // 压缩级别为2,数字越大压缩效果越好
 
client_body_buffer_size    128k;                         // 容许客户端请求缓存大小
client_max_body_size    100m;                             // 容许请求的最大文件容量
large-client_header_buffers    4    8k;                 //
proxy_buffering    on;                                           // 开启代理缓冲功能
proxy_buffer_size    8k;                                         // 第一部分响应数据的缓存大小
proxy_buffers    8    128k;                                     // 响应数据的缓存个数和容量
proxy_cache_path     /usr/local/nginx/cache     levels=1:2    keys_zone=one:100m    inactive=1d    max_size=2G;  
 
// 设置缓存目录,levels设置缓存个数,keys_zone定义缓存名字和容量,inactive定义缓存存活时间,max_size定义硬盘的缓存容量
proxy_connect_timeout    60s;             // 与后端服务器创建TCP链接握手超时时间
upstream servers {
 
//ip_hash ;ip_hash确保相同客户端ip使用相同的后端服务器,不适用就默认轮询
server    192.168.1.3:80    max_fails=3    fail_timeout=30s    weight=2;
server    192.168.1.4:80    max_fails=3    fail_timeout=30s    weight=2;
server    192.168.1.5:80    max_fails=3    fail_timeout=30s    weight=2;
}
 
server {
     listen    80;
     server_name    web. test .com;
     access_log    logs /host .access.log    main;
     location / {
 
proxy_pass http: //servers ;
proxy_cache one;
proxy_set_header X-Forwarded-For $remote_addr;
     }
 
}}

配置完成执行:

1
# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

最后浏览器访问http://192.168.1.2 或者http://111.122.155.133 刷新将分别获得不一样的web页面信息

3)Nginx rewrite规则
nginx的rewrite语法格式和apache很是类似,rewrite regex replacement [flag],其中flag能够被设置为last结束当前指令并从新搜索location匹配、break结束当前rewrite指令、redirect临时重定向30二、permanent永久重定向301。

rewrite地址重写及return应用的语法解析:
根据浏览器标识,访问资源重定向到指定文件目录,下面用IE浏览器示例:

1
2
3
if  ($http_user_agent ~ MSIE ) {
    rewrite ^(.*)$  /msie/ $1  break ;
}

将移动客户端的请求重定向到其余服务器:

1
2
3
if     ($http_user_agent ~*  '(iphone|ipod)'  )  {
       rewrite    ^.+    http: //mobile .site.com$uri;
}

用户使用POST方式请求数据时候,返回405:

1
2
3
if  ($request_method = POST ) {
    return  405;
}

访问admin时候重定向到admin目录:

1
2
3
location  /php_admin  {
      rewrite ^ /php_admin/ .*$  /admin  permanent;
}

-------------------------------------------下面详细说下nginx的rewrite重写指令用法-------------------------------------------
nginx经过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。
该模块须要PCRE支持,所以应在编译nginx时指定PCRE源码目录, nginx安装方法。

1)Nginx Rewrite 基本标记 (flags)

1
2
3
4
last       基本上都用这个Flag。至关于Apache里的[L]标记,表示完成rewrite,再也不匹配后面的规则
break       停止 Rewirte,再也不继续匹配
redirect   返回临时重定向的HTTP状态302
permanent  返回永久重定向的HTTP状态301。原有的url支持正则,重写的url不支持正则

2)正则表达式匹配,其中:

1
2
3
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~* 分别为区分大小写不匹配及不区分大小写不匹配

3)文件及目录匹配,其中:

1
2
3
4
* -f 和!-f 用来判断是否存在文件
* -d 和!-d 用来判断是否存在目录
* -e 和!-e 用来判断是否存在文件或目录
* -x 和!-x 用来判断文件是否可执行

4)Nginx的一些可用的全局变量,可用作条件判断:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri

nginx的rewrite指令执行顺序:

1
2
3
4
5
1)执行server块的rewrite指令(这里的块指的是server关键字后{}包围的区域,其它xx块相似)
2)执行location匹配
3)执行选定的location中的rewrite指令
若是其中某步URI被重写,则从新循环执行1-3,直到找到真实存在的文件
若是循环超过10次,则返回500 Internal Server Error错误

break指令
语法:break;
默认值:无
做用域:server,location,if

中止执行当前虚拟主机的后续rewrite指令集。
break指令实例:

1
2
3
4
if  ($slow) {
     limit_rate 10k;
     break ;
}

if指令
语法:if(condition){...}
默认值:无
做用域:server,location
对给定的条件condition进行判断。若是为真,大括号内的rewrite指令将被执行。
if条件(conditon)能够是以下任何内容:

1
2
3
4
5
6
7
1)一个变量名; false 若是这个变量是空字符串或者以0开始的字符串;
2)使用= ,!= 比较的一个变量和字符串
3)是用~, ~*与正则表达式匹配的变量,若是这个正则表达式中包含},;则整个表达式须要用" 或' 包围
4)使用-f ,!-f 检查一个文件是否存在
5)使用-d, !-d 检查一个目录是否存在
6)使用-e ,!-e 检查一个文件、目录、符号连接是否存在
7)使用-x , !-x 检查一个文件是否可执行

if指令实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if  ($http_user_agent ~ MSIE) {
      rewrite ^(.*)$  /msie/ $1  break ;
  }
 
  if  ($http_cookie ~*  "id=([^;]+)(?:;|$)" ) {
      set  $ id  $1;
  }
 
  if  ($request_method = POST) {
      return  405;
  }
 
  if  ($slow) {
      limit_rate 10k;
  }
 
  if  ($invalid_referer) {
      return  403;
  }

return指令

1
2
3
4
5
6
7
8
9
10
11
12
语法:
return  code;
return  code URL;
return  URL;
 
默认值:无
做用域:server,location, if
 
中止处理并返回指定状态码(code)给客户端。
非标准状态码444,表示关闭链接且不给客户端发响应头。
从0.8.42版本起, return  支持响应URL重定向(对于301,302,303,307),或者文本响应(对于其余状态码).
对于文本或者URL重定向能够包含变量

rewrite指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
语法:rewrite regex replacement [flag];
默认值:无
做用域:server,location, if
若是一个URI匹配指定的正则表达式regex,URI就按照replacement重写。
rewrite按配置文件中出现的顺序执行。flags标志能够中止继续处理。
若是replacement以 "http://" "https://" 开始,将再也不继续处理,这个重定向将返回给客户端。
 
flag能够是以下参数:
last     中止处理后续rewrite指令集,而后对当前重写的新URI在rewrite指令集上从新查找。
break     中止处理后续rewrite指令集,并不在从新查找,可是当前location内剩余非rewrite语句和location外的非rewrite语句能够执行。
redirect 若是replacement不是以http: //  或https: // 开始,返回302临时重定向
permant  返回301永久重定向
 
最终完整的重定向URL包括请求scheme(http: // ,https: // 等),请求的server_name_in_redirect和port_in_redirec三部分 ,说白了也就是http协议、域名、端口三部分组成。

rewrite实例说明:

1
2
3
4
5
6
7
server {
      ...
      rewrite ^( /download/ .*) /media/ (.*)\..*$ $1 /mp3/ $2.mp3 last;
      rewrite ^( /download/ .*) /audio/ (.*)\..*$ $1 /mp3/ $2.ra last;
      return  403;
      ...
  }

若是这些rewrite放到 “/download/” location以下所示, 那么应使用break而不是last , 使用last将循环10次匹配,而后返回500错误:

1
2
3
4
5
location  /download/  {
      rewrite ^( /download/ .*) /media/ (.*)\..*$ $1 /mp3/ $2.mp3  break ;
      rewrite ^( /download/ .*) /audio/ (.*)\..*$ $1 /mp3/ $2.ra  break ;
      return  403;
  }

对于重写后的URL(replacement)包含原请求的请求参数,原URL的?后的内容。若是不想带原请求的参数 ,
能够在replacement后加一个问号。以下,咱们加了一个自定义的参数user=$1,而后在结尾处放了一个问号?,
把原请的参数去掉。

1
rewrite ^ /users/ (.*)$  /show ?user=$1? last;

若是正则表达regex式中包含 “}” 或 “;”, 那么整个表达式须要用双引号或单引号包围.

rewrite_log指令

1
2
3
4
语法:rewrite_log on|off;
默认值:rewrite_log off;
做用域:http,server,location, if
开启或关闭以notice级别打印rewrite处理日志到error log文件。

nginx打开rewrite log的配置:

1
2
3
4
5
rewrite_log on;
error_log logs /xxx .error.log notice;
 
1)打开rewrite on
2)把error log的级别调整到notice

set指令

1
2
3
4
语法: set  variable value;
默认值:none
做用域:server,location, if
定义一个变量并赋值,值能够是文本,变量或者文本变量混合体。

uninitialized_variable_warn指令

1
2
3
4
5
语法:uninitialized_variable_warn on | off;
默认值:uninitialized_variable_warn on
做用域:http,server,location, if
 
控制是否输出为初始化的变量到日志

根据文件类型expires

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Add expires header for static content
location ~*  \.(js|css|jpg|jpeg|gif|png|swf)$ {
if  (-f $request_filename) {
    root  /data/www/wwwroot/bbs ;
    expires 1d;
    break ;
}
}
 
 
# serve static files
location ~  ^/(images|JavaScript|js|css|flash|media|static)/ {
    root  /data/www/wwwroot/down ;
    expires 30d;
}

----------------------------------------Nginx多Server反向代理配置----------------------------------------
Nginx强大的正则表达式支持可使server_name的配置变得很灵活。好比说想要作多用户博客,那么每一个用户都会拥有本身的二级域名,这样的话,能够灵活利用server_name配置也是很容易就实现的。

server_name的匹配顺序
Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1)准确的server_name匹配,例如:
server {
listen 80;
server_name ssdr.info www.ssdr.info;
...
}
 
2)以*通配符开始的字符串:
server {
listen 80;
server_name *.ssdr.info;
...
}
 
3)以*通配符结束的字符串:
server {
listen 80;
server_name www.*;
...
}
 
4)匹配正则表达式:
server {
listen 80;
server_name ~^(?.+)\.howtocn\.org$;
...
}

Nginx将按照上面1),2),3),4)的顺序对server name进行匹配,只要有一项匹配之后就会中止搜索,因此在使用这个指令的时候必定要分清楚它的匹配顺序(相似于location指令)。
server_name指令一项很实用的功能即是能够在使用正则表达式的捕获功能,这样能够尽可能精简配置文件,毕竟太长的配置文件平常维护也很不方便。

下面是2个具体的应用:

1)在一个server块中配置多个站点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server
{
listen 80;
server_name ~^(www\.)?(.+)$;
index index.php index.html;
root  /data/wwwsite/ $2;
}
 
站点的主目录应该相似于这样的结构:
/data/wwwsite/ssdr .info
/data/wwwsite/linuxtone .org
/data/wwwsite/baidu .com
/data/wwwsite/google .com
 
这样就能够只使用一个server块来完成多个站点的配置。

2)在一个server块中为一个站点配置多个二级域名 。
实际网站目录结构中一般会为站点的二级域名独立建立一个目录,一样可使用正则的捕获来实如今一个server块中配置多个二级域名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server
{
listen 80;
server_name ~^(.+)?\.howtocn\.org$;
index index.html;
if  ($host = ssdr.info){
rewrite ^ http: //www .ssdr.info permanent;
}
root  /data/wwwsite/ssdr .info/$1/;
}
 
站点的目录结构应该以下:
/data/wwwsite/ssdr .info /www/
/data/wwwsite/ssdr .info /nginx/
 
这样访问www.ssdr.info时root目录为 /data/wwwsite/ssdr .info /www/ ,nginx.ssdr.info时为 /data/wwwsite/ssdr .info /nginx/ ,以此类推。
后面 if 语句的做用是将ssdr.info的方位重定向到www.ssdr.info,这样既解决了网站的主目录访问,又能够增长seo中对www.ssdr.info的域名权重。

多个正则表达式
若是你在server_name中用了正则,而下面的location字段又使用了正则匹配,这样将没法使用$1,$2这样的引用,解决方法是经过set指令将其赋值给一个命名的变量:

1
2
3
4
5
6
7
8
9
10
11
12
13
server
{
listen 80;
server_name ~^(.+)?\.howtocn\.org$;
set  $www_root $1;
root  /data/wwwsite/ssdr .info/$www_root/;
location ~ .*\.php?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME  /data/wwwsite/ssdr .info/$fastcgi_script_name;
include fastcgi_params;
}
}

Nginx不一样域名反向代理到另外一台服务器 proxy_pass和$host
想让一个VPS专门作另外一个VPS的前端,后端VPS每添加一个域名,前端VPS就要同时添加一个域名来反向代理,做为前端的VPS若是一个一个的添加后端VPS的域名,那么这个事情特别麻烦,能不能让其自动反向代理后端VPS呢,用到proxy_pass和$host就能够轻松实现。
如下例子为了省事,以lnmp为安装环境进行设置,修改前端VPS的nginx.conf文件,修改为如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name $host;
location / {
proxy_pass http: //www .31.gd/;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}

接着修改其余部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
location /.(php|php5)?$
{
fastcgi_pass unix: /tmp/php-cgi .sock;
fastcgi_index index.php;
include fcgi.conf;
}
location  /status  {
stub_status on;
access_log off;
}
location /.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location /.(js|css)?$
{
expires 12h;
}

这样就能够实现了前端VPS能够反向代理任意域名到后端VPS,只要将域名解析到前端VPS,后端VPS进行域名绑定,那么就能够直接访问到了。

一台nginx带多个域名多个tomcat状况的配置
多个域名,其中2个域名需支持泛域名解析:
1)www.abc.com
2)www.bcd.com
3)*.efg.com
4)*.hij.com
其中1),2),3)为一台tomcat,4)为独立tomcat。前端一台nginx,经过配置多个虚拟主机来实现该部署。
进入/usr/local/nginx/conf/vhost目录,全部虚拟主机的配置文件都在该目录下存放。

配置支持泛域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
# A virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen       81;
server_name  *.efg.com;
location / {
proxy_pass http: //localhost :8080;
proxy_set_header   Host    $host;
proxy_set_header   X-Real-IP   $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#
# A virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen       81;
server_name  *.hij.com;
location / {
proxy_pass http: //localhost :8081;
proxy_set_header   Host    $host;
proxy_set_header   X-Real-IP   $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Nginx 多域名配置
nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别创建多个域名配置文件,我通常为了管理方便,每一个域名建一个文件,有些同类域名也可又写在一个总的配置文件里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
1)每一个域名一个文件的写法
首先打开nginx域名配置文件存放目录: /usr/local/nginx/conf/vhost  ,如要绑定域名www.wangshibo.com 则在此目录建一个文件:www.wangshibo.com.conf 而后在此文件中写规则,如:
server
{
listen       80;
server_name www.wangshibo.com;              #绑定域名
index index.htm index.html index.php;       #默认文件
root  /home/www/wangshibo .com;                #网站根目录
include location.conf;                             #调用其余规则,也可去除
}
 
2)一个文件多个域名的写法
一个文件添加多个域名的规则也是同样,只要把上面单个域名重复写下来就ok了,如:
server
{
listen       80;
server_name www.wangshibo.com;              #绑定域名
index index.htm index.html index.php;       #默认文件
root  /home/www/wangshibo .com;                #网站根目录
include location.conf;                             #调用其余规则,也可去除
}
server
{
listen       80;
server_name msn.wangshibo.com;              #绑定域名
index index.htm index.html index.php;       #默认文件
root  /home/www/msn .wangshibo.com;         #网站根目录
include location.conf;                             #调用其余规则,也可去除
}
 
3)不带www的域名加301跳转
若是不带www的域名要加301跳转,那也是和绑定域名同样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:
server
{
listen 80;
server_name wangshibo.com;
rewrite ^/(.*) http: //www .wangshibo.com/$1 permanent;
}
 
4)添加404网页
添加404网页,均可又直接在里面添加,如:
server
{
listen       80;
server_name www.wangshibo.com;              #绑定域名
index index.htm index.html index.php;       #默认文件
root  /home/www/wangshibo .com;                #网站根目录
include location.conf;                             #调用其余规则,也可去除
error_page 404   /404 .html;
}
最后还有一个方法须要注意,可能有须要禁止IP直接访问80端口或者禁止非本站的域名绑定咱们的IP,这样的话应该
以下处理,放到最前一个server上面便可:
server{
listen   80 default;
server_name      _;
return  403;
}
相关文章
相关标签/搜索