说明:
此文档初步计划只进行编译安装nginx,基于html的配置nginx虚拟主机及负载均衡。
模块依赖性:nginx的一些模块需要其他第三方库的支持,gzip模块需要zlib库,rewrite模块需要pcre库,ssl模块需要ipenssl库,这些需要我们单独来进行安装。
软件选择:从nginx官网下载最新stable released版本,本文软件包为:nginx-0.8.54.tar.gz
 
 
一、nginx编译安装
 
groupadd www
useradd -g www www
 apt-get  install  libpcre3 libpcre3-dev openssl libssl-dev gcc  automake  make
 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --user=www --group=www
 make
 make install
 
 
二、nginx的常用操作
2.1、nginx启动
/usr/local/nginx/sbin/nginx (-c     /usr/local/nginx/conf/nginx.conf)
   \\ -c 指定nginx.conf文件
 
2.2、nginx停止
查看主进程号
 
 # ps -ef | grep nginx
  
 root      12226     1 0 13:45 ?        00:00:00 nginx: master process ./nginx
                                               \\主进程号
www     12227  12226 0 13:45 ?        00:00:00 nginx: worker process
                                              \\子进程号
root      12462  11827 0 15:43 pts/4    00:00:00 grep nginx
 
l 杀死主进程号
kill -QUIT 主进程号
# kill -QUIT 12226
 
2.3、nginx 配置文件检测
 
# /usr/local/nginx/sbin/nginx –t
 
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
 
2.4、nginx平滑重启
 
kill –HUP 主进程号
# kill -HUP 12226
 
 
  
 
三、nginx配置
3.1、主文件配置
 
user    www www;
worker_processes 8; \\指定工作衍生进程数(一般等于cpu总和数或总和数2倍)
pid         logs/nginx.pid;
worker_rlimit_nofile 65535; \\指定文件描述符数量,与ulimit数值相同
events {
   use epoll; \\使用网络I/O模型,linux推荐采用epool模型
    worker_connections 65535; \\允许的连接数
}
 
 
3.2、负载均衡配置
 
 
 

 
 
http {
    include       mime.types;
    default_type application/octet-stream;
    sendfile        on;
    keepalive_timeout 120;
tcp_nodelay on;
 
## 负载均衡主要模块upstream
    upstream localhost {
      server 10.10.1.231:80;
      server 10.10.1.233:80;
    }
 
    server {
        listen       80;
        server_name localhost;
 
        location / {
             proxy_pass   http://localhost;
             proxy_set_header Host        $host;
             proxy_set_header X-Real-IP   $remote_addr;
             proxy_set_header X_Forwarded-For   $proxy_add_x_forwarded_for;
        }
 
      
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
            root   html;
        }
 
access_log    /data/nginxlogs/balance_access.log;
 error_log     /data/nginxlogs/balance_error.log;
 
 
 
3.3、虚拟主机配置
 
在http的大括号里配置
server {
        listen       80;
        server_name localhost;
 
        charset utf-8;
 
        location / {
            root   /data/nginx;
            index index.html index.htm;
        }
 access_log    /data/nginxlogs/ access.log;
 error_log     /data/nginxlogs/error.log;
 
 }
 
3.4、nginx日志切割
nginx不像apache一样有rotatelogs工具进行切割日志,但是可以使用定时任务将log进行改名重建。脚本见下:
 
#crontab -e
59 23 * * * /tmp/nginx_log.sh >/dev/null 2>&1