nginx負載均衡程序安裝札記
對于Nginx負載均衡的設(shè)定問題我們說過很多,也總結(jié)歸納過很多。在此,我們?yōu)榱遂柟糖懊娴闹R,將其中的安裝要點再為大家重申一下。對于環(huán)境的構(gòu)建,我們不過多的介紹了,前面已經(jīng)說的很清楚,現(xiàn)在我們從安裝開始說起,直接將配置代碼最為重點呈獻給大家。
安裝Nginx
1>安裝
Nginx發(fā)音為[engine x],是由俄羅斯人Igor Sysoev建立的項目,基于BSD許可。據(jù)說他當(dāng)初是F5的成員之一,英文主頁:http://nginx.net。俄羅斯的一些大網(wǎng)站已經(jīng)使用它超過兩年多了,一直表現(xiàn)不凡。Nginx的編譯參數(shù)如下:
- [root@localhost]#./configure --prefix=/usr/local/server/nginx --with-openssl=/usr/include \
- --with-pcre=/usr/include/pcre/ --with-http_stub_status_module --without-http_memcached_module \
- --without-http_fastcgi_module --without-http_rewrite_module --without-http_map_module \
- --without-http_geo_module --without-http_autoindex_module
在這里,需要說明一下,由于Nginx的配置文件中我想用到正則,所以需要pcre模塊的支持。我已經(jīng)安裝了pcre及pcre-devel的 rpm包,但是 Ngxin 并不能正確找到 .h/.so/.a/.la 文件,因此我稍微變通了一下:
- [root@localhost]#mkdir /usr/include/pcre/.libs/
- [root@localhost]#cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
- [root@localhost]#cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
然后,修改objs/Makefile大概在908行的位置上,注釋掉以下內(nèi)容: ./configure --disable-shared
接下來,就可以正常執(zhí)行make及make install了。
2>修改配置文件/usr/local/server/nginx/conf/nginx.conf
以下是我的nginx.conf內(nèi)容,僅供參考:
- #運行用戶
- usernobody nobody;
- #啟動進程
- worker_processes2;
- #全局錯誤日志及PID文件
- error_loglogs/error.log notice;
- pidlogs/nginx.pid;
- #工作模式及連接數(shù)上限
- events {
- use epoll;
- worker_connections1024;}
- #設(shè)定http服務(wù)器,利用它的反向代理功能提供負載均衡支持
- http {
- #設(shè)定mime類型
- include conf/mime.types;
- default_typeapplication/octet-stream;
#p#
- #設(shè)定日志格式
- 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"';
- #設(shè)定請求緩沖
- client_header_buffer_size1k;
- large_client_header_buffers4 4k;
- #開啟gzip模塊
- gzip on;
- gzip_min_length1100;
- gzip_buffers 4 8k;
- gzip_types text/plain;
- output_buffers 1 32k;
- postpone_output1460;
- #設(shè)定access log
- access_loglogs/access.logmain;
- client_header_timeout3m;
- client_body_timeout3m;
- send_timeout 3m;
- sendfileon;
- tcp_nopushon;
- tcp_nodelay on;
- keepalive_timeout65;
- #設(shè)定負載均衡的服務(wù)器列表
- upstream mysvr {
- #weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
- #本機上的Squid開啟3128端口
- server 192.168.8.1:3128 weight=5;
- server 192.168.8.2:80 weight=1;
- server 192.168.8.3:80 weight=6;}
- #設(shè)定虛擬主機
- server {
- listen80;
- server_name 192.168.8.1 www.yejr.com;
- charset gb2312;
- #設(shè)定本虛擬主機的訪問日志
- access_loglogs/www.yejr.com.access.logmain;
- #如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid
- #如果這些文件較多,不推薦這種方式,因為通過squid的緩存效果更好
- location ~ ^/(img|js|css)/{
- root/data3/Html;
- expires 24h;}
- #對 "/" 啟用負載均衡
- location / {
- proxy_passhttp://mysvr;
- proxy_redirectoff;
- proxy_set_headerHost $host;
- proxy_set_headerX-Real-IP $remote_addr;
- proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 90;
- proxy_send_timeout90;
- proxy_read_timeout90;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;}
- #設(shè)定查看Nginx狀態(tài)的地址
- location /NginxStatus {
- stub_status on;
- access_logon;
- auth_basic"NginxStatus";
- auth_basic_user_fileconf/htpasswd;}}}
3>查看 Nginx 運行狀態(tài)
輸入地址 http://192.168.8.1/NginxStatus/,輸入驗證帳號密碼,即可看到類似如下內(nèi)容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
***行表示目前活躍的連接數(shù)
第三行的第三個數(shù)字表示Nginx運行到當(dāng)前時間接受到的總請求數(shù),如果快達到了上限,就需要加大上限值了。