下载稳定版本:
nginx-1.4.7.tar.gz
解压:
tar zxvf nginx-1.4.7.tar.gz
安装:
cd nginx-1.4.7
yum -y install pcre-devel openssl openssl-devel
yum install gcc
patch -p1 < /home/xinli/yaozhf/nginx_tcp_proxy_module/tcp.patch
./configure --prefix=/usr/local/nginx --add-module=/home/xinli/yaozhf/nginx_tcp_proxy_module
make
make install
配置文件:
vi /usr/local/nginx/conf/nginx.conf
# 使用的用户和组user www www;# 指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个四核CPU,则总核数为8)worker_processes 8;# 指定错误日志存放的路径,错误日志记录级别可选项为:[ debug | info | notice | warn | error | crit ]error_log /data1/logs/nginx_error.log crit;# 指定 pid 存放的路径pid /usr/local/webserver/nginx/nginx.pid;# 指定文件描述符数量worker_rlimit_nofile 51200;events{ # 使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeBSD系统推荐采用kqueue模型 use epoll; # 允许的连接数 worker_connections 51200;}http{ include mime.types; default_type application/octet-stream; # 设置使用的字符集,如果一个网站有多种字符集,请不要随便设置,应让程序员在HTML代码中通过Meta标签设置 #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; # 设置客户端能够上传的文件大小 client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; # 开启gzip压缩 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 application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; server { listen 80; server_name www.yourdomain.com yourdomain.com; index index.html index.htm index.php; root /data0/htdocs; #limit_conn crawler 20; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /data1/logs/access.log access; }}
启动:
# -c 指定了配置文件的路径
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止:
# 查找主进程号
ps -ef | grep nginx
# 从容停止
kill -QUIT nginx主进程号
# 快速停止
kill -TERM nginx主进程号
kill -INT nginx主进程号
# 强制停止
kill -9 nginx主进程号
# 强制停止所有nginx
pkill -9 nginx
killall nginx
nginx主进程号可以使用
`cat /usr/local/nginx/logs/nginx.pid` 或 $( cat /usr/local/nginx/logs/nginx.pid )
平滑重启:
# 判断配置文件是否正确
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 平滑重启
kill -HUP nginx主进程号
alias用法:
# 一般情况下,在location /中配置root,在location /other中配置alias
location /mydir/ {
alias /var/myuser/mydir/;
# 激活/关闭自动索引 默认为 off
autoindex on;
# 设定索引文件大小的单位(B,KB,MB,GB) 默认为 on
autoindex_exact_size off;
# 开启以本地时间来显示文件时间 默认为 off
autoindex_localtime on;
}
rewrite用法:
# 目录不存在
if ( !-d $request_filename )
{
rewrite "/epg/([0-9]{8})(.*)" /mydir/current/ last;
}
http负载均衡用法:
# 总体格式
http
{
# weight-权重,越高,被分的客户端连接越多。默认权重1
# max_fails-在参数 fail_timeout 指定时间内 请求失败的次数。默认为1,设置为0关闭此检查
# down-永久离线状态
# backup-仅仅在非backup服务器全部宕机或繁忙时启用
upstream epg_server_pool {
server 192.168.3.227:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.3.228:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.3.229:80 down
server 192.168.3.243:80 backup
}
server {
listen 80;
server_name localhost;
location / {
#ip_hash
proxy_pass http://epg_server_pool
}
}
}
tcp负载均衡用法
# 总体格式
tcp {
upstream auth_pool {
# the upstream server will be dispatched by ip_hash.
# ip_hash
server 192.168.3.242:5203;
server 192.168.3.243:5203;
# interval-the check request's interval time.
# rise-After rise_count check success, the server is marked up.
# fall-After rise_count check success, the server is marked up.
# timeout-the check request's timeout.
# type-the check protocol type.
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
listen 5203;
proxy_pass auth_pool;
# set the timeout value with clients. default: 60000 milliseconds
timeout 60000;
}
# set the timeout value of connection to backends.default: 60000 milliseconds
#proxy_connect_timeout 60000
# set the timeout value of reading from backends.default: 60000 milliseconds
#proxy_read_timeout 60000
# set the timeout value of sending to backends.default: 60000 milliseconds
#proxy_send_timeout 60000
}
切割Nginx日志脚本:
#1-编辑脚本
vi /usr/local/nginx/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
#2-设置crontab
crontab -e
00 00 * * * /usr/local/nginx/sbin/cut_nginx_log.sh