作者:diege
整理时间:2012-04-12
一、        安装操作系统
根据《FreeBSD标准安装配置》安装FreeBSD8.2
二、        更新Port系统
1、设置Port更新源
SERVERNAME=portsnap.FreeBSD.org
2、更新
第一次更新
#portsnap fetch extract
而在以后只须要运行:
portsnap fetch update
三、        安装配置nginx
1、安装
# cd /usr/ports/www/nginx && make install clean
安装配置:一般情况下默认即可
默认配置包括:
[x[X] IPV6                      Enable IPv6
[x[X] HTTP_MODULE               Enable HTTP module
x[X] HTTP_CACHE_MODULE         Enable http_cache module
x[X] HTTP_REWRITE_MODULE       Enable http_rewrite module
x[X] HTTP_STATUS_MODULE        Enable http_stub_status module
x[X] WWW                       Enable html sample files
2、配置
硬件为双C8核8G内存机器
全局配置文件
# nginx.conf
user                    www;
worker_processes        8;
error_log               /data/logs/nginx_error.log;
pid                     /var/run/nginx.pid;
events {
        worker_connections      10000;
}
http {
        access_log              off;
 
charset  utf-8;
 
        include                 mime.types;
        default_type            application/octet-stream;
        sendfile                on;
        tcp_nopush              on;
        keepalive_timeout       10;
        tcp_nodelay             on;
        server_tokens           off;
        gzip                    off;
        gzip_types              text/plain text/xml;
        client_max_body_size    8M;
        proxy_read_timeout      300;
        fastcgi_connect_timeout 10;
        fastcgi_send_timeout    300;
        fastcgi_read_timeout    300;
        fastcgi_buffer_size     8k;
        fastcgi_buffers         16 8k;
        server_names_hash_bucket_size   64;
        large_client_header_buffers     64 8k;
#       fastcgi_temp_path       /data/tmp/nginx_fastcgi_temp;
#       client_body_temp_path   /data/tmp/nginx_client_body_temp;
#                proxy_temp_path        /data/tmp/nginx_proxy_temp;
 
        index                   index.htm index.html;
                   #set_real_ip_from        10.10.10.11;
        #set_real_ip_from        10.10.10.12;
        #real_ip_header          X-Real-IP;
                    #定义错误页面
              error_page 404 http://www.www.skylog.cn/static/error.html;
                    error_page 403 http://www.www.skylog.cn/static/error.html;
      
        server {
                listen          80;
                server_name     122.226.66.20;
                root    /data/vhosts/default;
 
                location /nginx_status {
                stub_status on;
                allow 127.0.0.1;
                deny all;
                }
        #       access_log      /data/logs/default_web.log;
        #       error_page      404 http://$host/;
        }
 
        include vhosts/*.conf;
 
}
虚拟站点配置文件
server {
        listen  80;
        server_name  hd.test.com;
        root  /data/vhosts/hd.test.com/public;
        access_log      /data/logs/hd.test.com-access_log;
 
        location / {
                alias  /data/vhosts/hd.test.com/public/;
                index index.html index.php;
 
                # if file exists return it right away
                if (-f $request_filename) {
                        break;
                }
 
                # otherwise rewrite the fucker
                if (!-e $request_filename) {
                        rewrite ^(.+)$ /index.php last;
                        break;
                }
        }
 
        location /static {
                alias  /data/vhosts/hd.test.com/view/static/;
                if (-f $request_filename) {
                        expires max;
                        break;
                }
        }
        location /upload {
                alias /data/vhosts/hd.test.com/upload/;
        }
 
        # if the request starts with our frontcontroller, pass it on to fastcgi
 
        location ~ \.php$
        {
                #fastcgi_pass php-fpm;
                fastcgi_index   index.php;
                fastcgi_pass    127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include fastcgi_params;
        }
 
# IP Filter
#       allow 10.0.0.1;
#       deny all;
}
3、测试启动
1)测试
# nginx –t
# echo 'nginx_enable="YES"' >> /etc/rc.conf
# /usr/local/etc/rc.d/nginx start
2)查看版本
# nginx -v
nginx: nginx version: nginx/1.0.6
3)查看编译参数
# nginx –V
4、关于LOG配置
1)普通格式
#log_format 有一个默认的,无需设置的combined日志格式设置
#其格式为
log_format  combined  '$remote_addr - $remote_user [$time_local]' '$request "$status" $body_bytes_sent ' '"$http_referer" "$http_user_agent" '
#其他自定义的LOG
log_format  main  '$remote_addr - $remote_user [$time_local] $request '
              '"$status" $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for"';
 
log_format download  '$remote_addr - $remote_user [$time_local] '
                         '"$request" $status $bytes_sent '
                         '"$http_referer" "$http_user_agent" '
                         '"$http_range" "$sent_http_content_range
2)、LOG 使用
                            #access_log  logs/$servername-access.log main buffer=32k;
                            #access_log  logs/$servername-access.log main;缓冲不会被使用
            access_log  logs/$servername.cn.access.log  main;
3)、抓时间日志配置
$request_time: nginx处理请求的时间
$upstream_response_time: php-cgi的响应时间
完整的
log_format  timelog  '$remote_addr - $remote_user [$time_local] $request '
              '"$status" $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for"'
                              '$request_time $upstream_response_time';
 
四、        httpGzip模块配置
支持在线实时压缩输出数据流
默认开启,--with-http_gzip_static_module
在http{}块中
gzip                    on;
gzip_min_length                      1k;
gzip_buffers                 4      16k;
gzip_http_version           1.1;
gzip_comp_level               2;
gzip_types       text/plain application/x-javascript text/css text/javascript application/xml;
gzip_vary          on;
                 
说明
gzip on; #打开gzip压缩
gzip_min_length  1k; #最小压缩文件大小
gzip_buffers     4 16k; #压缩缓冲区 申请4个单位16K的内存作为压缩结果流缓存。
gzip_http_version 1.1; #压缩版本(默认1.1,前端为squid2.5使用1.0)
gzip_comp_level; 压缩等级,1压缩比最小,处理速度最快。9压缩比最大,传输速度快,但处理最慢,也比较耗CPU资源。
gzip_types       text/plain application/x-javascript text/css text/html text/javascript application/xml;
#压缩类型,默认就已经包含text/html 所以上面就不用再写了,当然写上去的话,也不会有问题,但是会有一个warn。
gzip_vary 可以让前端的缓存服务器缓存经过gzip压缩的页面。例如用squid缓存经过Nginx压缩的数据。
 
五、        使用stub_status模块查看nginx状态和Nginx ×××验证
模块默认开启
x[X] HTTP_STATUS_MODULE        Enable http_stub_status module
可为该功能单独设置访问LOG,访问用户名密码以及可以访问的IP
放在住配置的站点下
server_name             192.168.1.200;
location /nginx_status {
 stub_status on;
}
增加,访问LOG,用户密码密码。以及访问控制
1、使用apache的htpasswd产生用户名密码
# htpasswd -c /usr/local/etc/nginx/nginx.htpasswd diege
2、修改配置文件
               location /nginx_status {
                stub_status on;
                access_log /data/logs/nginx_status.log;
                auth_basic "nginx_status";
                auth_basic_user_file /usr/local/etc/nginx/nginx.htpasswd;
                allow 192.168.1.1;
                allow 127.0.0.1;
                deny all;
                }
3、访问测试
# tail -f /data/logs/nginx_status.log
192.168.1.1 - diege [12/Apr/2012:21:36:22 +0800] "GET /nginx_status HTTP/1.1" 200 98 "-" "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
4、状态说明
Active connections 当前活跃的链接数
8 8 18 三个数分别为Nginx当前总共处理的连接个数,成功创建的握手次数,总共处理的请求数。
Reading:表示读取到客户端的Header信息数
Writing:表示Nginx返回给客户端的Header信息数
Waiting:表示Nginx已处理完,正在等候下一次请求指令时的驻留链接数
六、        使用ngx_http_access_module限制ip访问
这个模块是默认模块,默认开启
使用deny和allow 对IP进行访问控制,一般放在location下
deny和allow放在前面的覆盖后面的,放在前面的优先,需要例外时放在前面。所以,一般我们允许访问的IP,然后拒绝所有IP。
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
七、        nginx 日志分隔
原理:向Nginx进程传递USR1信号产生新日志文件
Nginx几个常用的进程信号
HUP:重新加载配置文件
USR1:日志切换,重新打开一个日志文件
USER2:用于平滑升级可执行程序
1、  服务器只有单个虚拟站点脚本
#!/bin/sh
# Name :            gzip_hd.test.com_log.sh
# Date created :    2012-04-16
# Whom :            diege<diege@icee.cn>
# Version :         V1.0,2012-04-16
# Description :     处理hd.test.com log
vhost=hd.test.com
yesterday=`/bin/date -v -1d +%Y-%m-%d`
logbak_dir=/data/backup/httplogs
httplog_dir=/data/logs                              
src_file=${httplog_dir}/${vhost}-access_log         
dst_file=${logbak_dir}/${vhost}/${vhost}_${yesterday}_access.log
gzip_httplog (){                                                     
         /bin/mv  $src_file $dst_file                       
         kill -USR1 `cat  /var/run/nginx.pid`
         cd ${logbak_dir}/${vhost}/ && gzip ${vhost}_${yesterday}_access.log
}
 
if [ -d ${logbak_dir}/${vhost} ];then
         gzip_httplog
else
         mkdir -p ${logbak_dir}/${vhost}
         gzip_httplog
fi
2、  一台服务器多个虚拟站点
#!/bin/sh
# Name :            gzip_hd.test.com_log.sh
# Date created :    2012-04-16
# Whom :            diege<diege@icee.cn>
# Version :         V1.0,2012-04-16
# Description :     处理hd.test.com log
vhost=hd.test.com
yesterday=`/bin/date -v -1d +%Y-%m-%d`
logbak_dir=/data/backup/httplogs
httplog_dir=/data/logs                              
src_file=${httplog_dir}/${vhost}-access_log         
dst_file=${logbak_dir}/${vhost}/${vhost}_${yesterday}_access.log
gzip_httplog (){                                                     
         /bin/mv  $src_file $dst_file                       
         kill -USR1 `cat  /var/run/nginx.pid`
         cd ${logbak_dir}/${vhost}/ && gzip ${vhost}_${yesterday}_access.log
}
 
if [ -d ${logbak_dir}/${vhost} ];then
         gzip_httplog
else
         mkdir -p ${logbak_dir}/${vhost}
         gzip_httplog
fi
3、  执行时间 每天凌晨0点
#crontab –l
0       0       *       *       *       /root/script/gzip_hd.test.com_log.sh >/dev/null 2>&1