1. 程式人生 > >Nginx安裝upstream-fair模組(第三方模組)

Nginx安裝upstream-fair模組(第三方模組)

在ubuntu或debian上安裝nginx,可以直接採用使用指令安裝
apt-get install nginx

假定工作操作目錄為使用者根目錄即~或者/home/uname(當前你是uname使用者),如果你是root使用者,那麼你的根目錄應該是/root

獲取nginx及安裝依賴包
使用官方源安裝可能不能滿足選擇版本的需求,手動安裝nginx首先安裝依賴包
apt-get指令需要root許可權,確保你使用root使用者或者獲得root許可權
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libpcre3 libpcre3-dev libssl-dev libxslt-dev libgd2-xpm-dev libgeoip-dev


nginx原始碼可以訪問nginx官方網站的下載頁面:http://nginx.org/en/download.html
比如選擇 Stable version nginx-1.2.7
複製下載地址,然後使用命令下載tar包
wget http://nginx.org/download/nginx-1.2.7.tar.gz
解壓縮
tar zxvf nginx-1.2.7.tar.gz

upstream-fair模組
upstream-fair是比內建的負載均衡更加智慧的負載均衡模組,一般google關鍵詞nginx-upstream-fair可以找到相關資源和文章,這裡不詳述了。它採用的不是內建負載均衡使用的輪換的均衡演算法,而是可以根據頁面大小、載入時間長短智慧的進行負載均衡。
可以通過github獲取,地址是https://github.com/gnosek/nginx-upstream-fair
googlecode上也有http://wcoserver.googlecode.com/files/gnosek-nginx-upstream-fair-2131c73.tar.gz
注意:這個版本可能已經過時,推薦使用github上的master zip包
獲取到nginx-upstream-fair-master.zip或者gnosek-nginx-upstream-fair-2131c73.tar.gz後解壓縮
unzip nginx-upstream-fair-master.zip
tar zxvf gnosek-nginx-upstream-fair-2131c73.tar.gz


為了方便起見,把加壓縮後得到資料夾更改一下名字,比如upstream
mv nginx-upstream-fair-master upstream
mv gnosek-nginx-upstream-fair-2131c73 upstream

如果你喜歡用rename也行,具體可以自己操作。

編譯安裝nginx
接下來進入解壓縮之後的目錄
cd nginx-1.2.7/
配置引數
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-ipv6 --with-mail --with-mail_ssl_module --add-module=/home/uname/upstream/


注意最後一個引數–add-module=/home/uname/upstream/的路徑,之前假定的工作目錄為/home/uname(uname為你的使用者根目錄),我們下載的gnosek-nginx-upstream-fair壓縮包在這裡解壓並且更名未upstream,此處填寫絕對路徑。
配置結束應該不會報錯,我在ubuntu12.04和debian6上都做過安裝,並且作為開發和生產環境一直在執行,如果有問題可以聯絡我。
接下來編譯安裝
sudo make
sudo make install

等待編譯結束,安裝完成
執行檔案被安裝在/etc/nginx/sbin/nginx,如果你想安裝到/usr下可以更改前面的配置引數,這裡可以這樣在/usr/sbin中建立軟連結
cd /usr/sbin
sudo ln -s /etc/nginx/sbin/nginx

然後你在任意位置執行
nginx -v
應該能看到nginx版本資訊
nginx version: nginx/1.2.7
檢視詳細配置資訊
nginx -V
顯示詳細資訊
nginx version: nginx/1.2.7
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-ipv6 --with-mail --with-mail_ssl_module --add-module=/home/uname/upstream/

附一個nginx使用upstream-fair的配置檔案示例:

upstream your-site {
server 127.0.0.1:5550;
server 127.0.0.1:5551;
fair;
}
server {
listen 80;
server_name yoursite.com www.yoursite.com;
access_log /var/log/nginx/access.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 60s;
proxy_send_timeout 90s;
proxy_read_timeout 90s;
proxy_buffering off;
proxy_temp_file_write_size 64k;
proxy_pass http://your-site;
proxy_redirect off;
}
}