Nginx作為一個后起之秀,他的迷人之處已經讓很多人都投入了他的懷抱。配置簡單,實現原理簡單。做一個負載平衡的再好不過了。
其原理:
簡單介紹一下他的安裝及配置過程
官方網站
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/ (啟用正規表達式)
--with-http_stub_status_module (安裝可以查看nginx狀態的程序)
--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
#運行用戶
user nobody nobody;
#啟動進程
worker_processes 5;
#全局錯誤日志及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及連接數上限
events {
#工作模式有:select(標準模式),poll(標準模式),kqueue(高效模式,適用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
#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+)
use epoll;
worker_connections 1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型
include conf/mime.types;
default_type application/octet-stream;
#設定日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#設定請求緩沖
client_header_buffer_size 10k;
large_client_header_buffers 4 4k;
#開啟gzip模塊,要求安裝gzip 在運行./config時要指定
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#設定訪問日志
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#設定負載均衡的服務器列表
upstream backserver {
#weigth參數表示權值,權值越高被分配到的幾率越大
#本例是指在同一臺服務器,多臺服務器改變ip即可
server 127.0.0.1:8081 weight=5;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
}
#設定虛擬主機,默認為監聽80端口,改成其他端口會出現問題
server {
listen 80;
server_name test.com www.test.com;
charset utf8;
#設定本虛擬主機的訪問日志
access_log logs/test.com.log main;
#如果訪問 /images/*, /js/*, /css/* 資源,則直接取本地文件,不用轉發。但如果文件較多效果不是太好。
location ~ ^/(images|js|css)/ {
root /usr/local/testweb;
expires 30m;
}
#對 "/" 啟用負載均衡
location / {
proxy_pass http://backserver;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#設定查看Nginx狀態的地址,在運行./config 要指定,默認是不安裝的。
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
#是否要通過用戶名和密碼訪問,測試時可以不加上。conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生即可
#auth_basic_user_file conf/htpasswd;
}
}