nginx
一、nginx的安装
下载地址: http://nginx.org/download/nginx-1.4.2.tar.gz
安装准备: nginx依赖于pcre库,要先安装pcre
1、安装插件库
yum install pcre pcre-devel
2、切换到指定目录下
cd /usr/local/src/
3、下载nginx的软件
wget http://nginx.org/download/nginx-1.4.2.tar.gz
4、解压nginx软件
tar zxvf nginx-1.4.2.tar.gz
5、编译nginx软件
cd nginx-1.4.2
./configure –prefix=/usr/local/nginx
6、进行nginx的安装
make && make install
7、启动nginx
./nginx
备注:
cd /ulsr/local/nginx, 看到如下4个目录
….conf 配置文件
… html 网页文件
…logs 日志文件
…sbin 主要二进制程序
启动常见错误:
[root@localhost nginx]# ./sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
....
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
解决方法:
不能绑定80端口,80端口已经被占用
(有时是自己装了apache,nginx等,还有更多情况是操作系统自带了apache并作为服务启动)
解决: 把占用80端口的软件或服务关闭即可.
安装错误:
安装nginx,执行make时,提示
make: * 没有规则可以创建“default”需要的目标“build”。 停止。
认真看./configure结果
如果错误为:pcre not found,安装需要安装pcre
yum install pcre*
如果错误为:the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using –with-zlib= option.
则需要安装“zlib-devel”即可。
yum install -y zlib-devel
二、nginx信号量的使用
查看进程号 ps aux|grep nginx
Nginx的信号控制
TERM, INT Quick shutdown
QUIT Graceful shutdown 优雅的关闭进程,即等请求结束后再关闭
HUP Configuration reload ,Start the new worker processes with a new configuration Gracefully shutdown the
old worker processes改变配置文件,平滑的重读配置文件
USR1 Reopen the log files 重读日志,在日志按月/日分割时有用
USR2 Upgrade Executable on the fly 平滑的升级
WINCH Gracefully shutdown the worker processes 优雅关闭旧的进程(配合USR2来进行升级)
例如:优雅的关闭nginx kill -QUIT cat logs/nginx.pid
注意: “ 不是单引号
具体语法:
Kill -信号选项 nginx的主进程号
Kill -HUP 4873
Kill -信号控制 `cat /xxx/path/log/nginx.pid`
Kil; -USR1 `cat /xxx/path/log/nginx.pid`
另外,nginx也可以使用其他命令进行操作:
在nginx/sbin目录下输入 ./nginx -h 可以看到指令帮助
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
./nginx -s stop 强制停止
./nginx -s quit 优雅停止
./nginx -s reload 重读配置文件启动
三、nginx的配置
// 全局区
worker_processes 1; // 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数
Event {
// 一般是配置nginx连接的特性
// 如1个word能同时允许多少连接
worker_connections 1024; // 这是指 一个子进程最大允许连1024个连接
}
http { //这是配置http服务器的主要段
Server1 { // 这是虚拟主机段
Location { //定位,把特殊的路径或文件再次定位 ,如image目录单独处理
} /// 如.php单独处理
}
Server2 {
}
}
例子1: 基于域名的虚拟主机
server {
listen 80; #监听端口
server_name a.com; #监听域名
location / {
root /var/www/a.com; #根目录定位
index index.html;
}
}
例子2: 基于端口的虚拟主机配置
server {
listen 8080;
server_name 192.168.1.204;
location / {
root /var/www/html8080;
index index.html;
}
}
实际使用案例:
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream mynginx {
server 45.78.9.159:8094;
#server 45.78.9.159:8094 weight=5;
#server 45.78.9.159:8098 weight=1;
}
server {
listen 80;
server_name www.wtdig.top;
#charset koi8-r;
access_log logs/wtdig.access.log main;
root /usr/tomcat/tomcat1/webapps/wtdig;
index index.html index.htm index.jsp default.jsp index.do default.do;
location ~* .*\.(html|htm|js|css)$ {
expires 1h;
}
location ~* .*\.(jpg|jepg|gif|png|swf|ico)$ {
expires 30d;
}
#location /static {}
location ~ \.(jsp|jspx|do)?$ {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://mynginx;
}
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://mynginx;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
四、日志的管理
我们观察nginx的server段,可以看到如下类似信息
#access_log logs/host.access.log main;
这说明 该server, 它的访问日志的文件是 logs/host.access.log ,使用的格式”main”格式.除了main格式,你可以自定义其他格式.
main格式是什么?
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
main格式是我们定义好一种日志的格式,并起个名字,便于引用.
以上面的例子, main类型的日志,记录的 remote_addr…. http_x_forwarded_for等选项.
1: 日志格式 是指记录哪些选项
默认的日志格式: main
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ’
‘$status $body_bytes_sent “$http_referer” ’
‘”$http_user_agent” “$http_x_forwarded_for”’;
如默认的main日志格式,记录这么几项
远程IP- 远程用户/用户时间 请求方法(如GET/POST) 请求体body长度 referer来源信息
http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP
http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP
2: 声明一个独特的log_format并命名
log_format mylog '$remote_addr- "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
在下面的server/location,我们就可以引用 mylog
在server段中,这样来声明
Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,如lighttp)
access_log logs/access_8080.log mylog;
声明log log位置 log格式;
实际应用: shell+定时任务+nginx信号管理,完成日志按日期存储
分析思路:
凌晨00:00:01,把昨天的日志重命名,放在相应的目录下
再USR1信息号控制nginx重新生成新的日志文件
具体脚本:
!/bin/bash
base_path=’/usr/local/nginx/logs’
log_path=$(date -d yesterday +”%Y%m”)
day=$(date -d yesterday +”%d”)
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
$base_path/$log_path/access_$day.log
kill -USR1 cat /usr/local/nginx/logs/nginx.pid
定时任务
Crontab 编辑定时任务
输入crontab -e,添加一行信息
01 00 * * * /xxx/path/b.sh 每天0时1分(建议在02-04点之间,系统负载小)