Nginx作為一個后起之秀,他的迷人之處已經(jīng)讓很多人都投入了他的懷抱。配置簡單,實現(xiàn)原理簡單。做一個負載平衡的再好不過了。
其原理:
簡單介紹一下他的安裝及配置過程
官方網(wǎng)站
http://wiki.codemongers.com/Main
一、依賴的程序
1. gzip module requires zlib library
2. rewrite module requires pcre library
3. ssl support requires openssl library
二、安裝
./configure
make
make install
默認安裝的路徑是/usr/local/nginx
更多的安裝配置
./configure --prefix=/usr/local/nginx
--with-openssl=/usr/include (啟用ssl)
--with-pcre=/usr/include/pcre/ (啟用正規(guī)表達式)
--with-http_stub_status_module (安裝可以查看nginx狀態(tài)的程序)
--with-http_memcached_module (啟用memcache緩存)
--with-http_rewrite_module (啟用支持url重寫)
三、啟動及重啟
啟動:nginx
重啟:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
測試配置文件:nginx -t
簡單吧,安裝,啟動都比較方便。
四、配置文件
http://wiki.codemongers.com/NginxFullExample
#運行用戶
1
user nobody nobody;
2
#啟動進程
3
worker_processes 5;
4
#全局錯誤日志及PID文件
5
error_log logs/error.log notice;
6
pid logs/nginx.pid;
7
#工作模式及連接數(shù)上限
8
events {
9
#工作模式有:select(標準模式),poll(標準模式),kqueue(高效模式,適用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
10
#epoll(高效模式,本例用的。適用Linux 2.6+,SuSE 8.2,),
#/dev/poll(高效模式,適用Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
11
use epoll;
12
worker_connections 1024;
13
}
14
#設定http服務器,利用它的反向代理功能提供負載均衡支持
15
http {
16
#設定mime類型
17
include conf/mime.types;
18
default_type application/octet-stream;
19
#設定日志格式
20
log_format main '$remote_addr - $remote_user [$time_local] '
21
'"$request" $status $bytes_sent '
22
'"$http_referer" "$http_user_agent" '
23
'"$gzip_ratio"';
24
25
log_format download '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$http_range" "$sent_http_content_range"';
26
#設定請求緩沖
27
client_header_buffer_size 10k;
28
large_client_header_buffers 4 4k;
29
30
#開啟gzip模塊,要求安裝gzip 在運行./config時要指定
31
gzip on;
32
gzip_min_length 1100;
33
gzip_buffers 4 8k;
34
gzip_types text/plain;
35
output_buffers 1 32k;
36
postpone_output 1460;
37
#設定訪問日志
38
access_log logs/access.log main;
39
client_header_timeout 3m;
40
client_body_timeout 3m;
41
send_timeout 3m;
42
sendfile on;
43
tcp_nopush on;
44
tcp_nodelay on;
45
keepalive_timeout 65;
46
47
#設定負載均衡的服務器列表
48
upstream backserver {
49
#weigth參數(shù)表示權值,權值越高被分配到的幾率越大
50
#本例是指在同一臺服務器,多臺服務器改變ip即可
51
server 127.0.0.1:8081 weight=5;
52
server 127.0.0.1:8082;
53
server 127.0.0.1:8083;
54
}
#Deny access to any host other than (www).4535.com
server {
server_name _; #default
return 404;
}
55
#設定虛擬主機,默認為監(jiān)聽80端口,改成其他端口會出現(xiàn)問題
56
server {
57
listen 80;
58
server_name test.com www.test.com;
59
charset utf8;
60
#設定本虛擬主機的訪問日志
61
access_log logs/test.com.log main;
62
#如果訪問 /images/*, /js/*, /css/* 資源,則直接取本地文件,不用轉發(fā)。但如果文件較多效果不是太好。
63
location ~ ^/(images|js|css)/ {
64
root /usr/local/testweb;
65
expires 30m;
66
}
67
#對 "/" 啟用負載均衡
68
location / {
69
proxy_pass http://backserver;
70
proxy_redirect off;
71
proxy_set_header Host $host;
72
proxy_set_header X-Real-IP $remote_addr;
73
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
74
client_max_body_size 10m;
75
client_body_buffer_size 128k;
76
proxy_connect_timeout 90;
77
proxy_send_timeout 90;
78
proxy_read_timeout 90;
79
proxy_buffer_size 4k;
80
proxy_buffers 4 32k;
81
proxy_busy_buffers_size 64k;
82
proxy_temp_file_write_size 64k;}
83
#設定查看Nginx狀態(tài)的地址,在運行./config 要指定,默認是不安裝的。
84
location /NginxStatus {
85
stub_status on;
86
access_log on;
87
auth_basic "NginxStatus";
88
#是否要通過用戶名和密碼訪問,測試時可以不加上。conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產(chǎn)生即可#auth_basic_user_file conf/htpasswd;
89
}
90
}
91
生活是用腳一步一步走出來的.....