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;
}
|