Ubuntu/Debian安装Nginx和upstream-fair

在ubuntu或debian上安装nginx,可以直接采用使用指令安装

1
apt-get install nginx

假定工作操作目录为用户根目录即~或者/home/uname(当前你是uname用户),如果你是root用户,那么你的根目录应该是/root

获取nginx及安装依赖包
使用官方源安装可能不能满足选择版本的需求,手动安装nginx首先安装依赖包
apt-get指令需要root权限,确保你使用root用户或者获得root权限

1
2
3
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包

1
wget http://nginx.org/download/nginx-1.2.7.tar.gz

解压缩

1
tar zxvf nginx-1.2.7.tar.gz

upstream-fair模块
upstream-fair是比内建的负载均衡更加智能的负载均衡模块,一般google关键词nginx-upstream-fair可以找到相关资源和文章,这里不详述了。它采用的不是内建负载均衡使用的轮换的均衡算法,而是可以根据页面大小、加载时间长短智能的进行负载均衡。
可以通过github获取,地址是https://github.com/gnosek/nginx-upstream-fair
googlecc上也有http://wcoserver.googlecc.com/files/gnosek-nginx-upstream-fair-2131c73.tar.gz
注意:这个版本可能已经过时,推荐使用github上的master zip包
获取到nginx-upstream-fair-master.zip或者gnosek-nginx-upstream-fair-2131c73.tar.gz后解压缩

1
2
unzip nginx-upstream-fair-master.zip
tar zxvf gnosek-nginx-upstream-fair-2131c73.tar.gz

为了方便起见,把加压缩后得到文件夹更改一下名字,比如upstream

1
2
mv nginx-upstream-fair-master upstream
mv gnosek-nginx-upstream-fair-2131c73 upstream

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

编译安装nginx
接下来进入解压缩之后的目录

1
cd nginx-1.2.7/

配置参数

1
./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上都做过安装,并且作为开发和生产环境一直在运行,如果有问题可以联系我。
接下来编译安装

1
2
sudo make
sudo make install

等待编译结束,安装完成
执行文件被安装在/etc/nginx/sbin/nginx,如果你想安装到/usr下可以更改前面的配置参数,这里可以这样在/usr/sbin中建立软链接

1
2
cd /usr/sbin
sudo ln -s /etc/nginx/sbin/nginx

然后你在任意位置执行

1
nginx -v

应该能看到nginx版本信息

1
nginx version: nginx/1.2.7

查看详细配置信息

1
nginx -V

显示详细信息

1
2
3
4
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的配置文件示例:

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