Nginx是什么

Nginx是一个web服务器,类似于Apache,但是nginx还具有反向代理负载均衡功能和缓存服务功能,并且部署简单,方便。

Nginx的特点

  • 支持高并发,并且资源消耗少
  • 可以做HTTP反向代理及加速缓存
  • epoll模型,随着连接数的增加,性能基本没有下降
  • 对处理静态小文件优势更大

编译安装Nginx

安装依赖库

yum安装一键搞定,不建议编译安装pcre yum install -y openssl openssl-devel pcre pcre-devel gcc

下载及编译安装

在官方下载最新稳定版nginx,注意如果下载缓慢,可先下载到本地,然后传到nginx服务器上。 wget -q http://nginx.org/download/nginx-1.10.3.tar.gz 创建nginx账户 useradd nginx -s /sbin/nologin -M 解压tar包,并cd到指定目录

tar xf nginx-1.10.3.tar.gz
cd nginx-1.10.3

编译安装

./configure --user=nginx --group=nginx --prefix=/application/nginx-1.10.3/ --with-http_stub_status_module --with-http_ssl_module
make && make install

创建软链接,方便后期版本升级 ln -s /application/nginx-1.10.3 /application/nginx

相关命令

/application/nginx/sbin/nginx -t        #启动检查
/application/nginx/sbin/nginx            #启动
/application/nginx/sbin/nginx -s reload     #重新加载配置
/application/nginx/sbin/nginx -s stop      #关闭

Nginx配置文件

[root@localhost nginx]# egrep -v '#|^$' conf/nginx.conf
worker_processes  1;
error_log  logs/error.log  notice;   #错误日志
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"'; #配置日志格式
    sendfile        on;
    keepalive_timeout  65;
    include conf.d/*.conf;   #将不同主机的配置文件单独放到conf.d目录下
    server {
        listen       80;
        #listen    8080;    基于端口
        #listen 192.168.10.10:80;   基于IP
        server_name  localhost;
        #server_name www.boheyan.cn;    基于域名
        #server_name www.boheyan.cn boheyan.cn; 配置主机别名
        #rewrite ^/(.*) http://www.boheyan.cn/$1 permanent; 301跳转,永久跳转到www.boheyan.cn
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        error_log  logs/boheyan.log  ;   #错误日志
        access_log  logs/boheyan.access.log  main;  #访问日志
    }
}

Nginx status功能

conf.d目录下创建status.conf

[root@localhost conf]# cat conf.d/status.conf
server{
    listen	8080;
    server_name	localhost;
    location / {
	stub_status	on;
	access_log	off;
    }
}

然后reload配置,打开连接http://192.168.124.11:8080/

Active connections: 活动连接数
server:启动到现在共处理了19个连接
accepts:成功创建了19次握手
handled requests:共处理了13次请求
Reading:读取到客户端的Header信息数
Writing:返回给客户端的Header信息数
Waiting:等候请求指令的驻留连接

nginx 访问日志

定义日志格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

启动错误日志和访问日志

 error_log  logs/boheyan.log  ;   #错误日志
 access_log  logs/boheyan.access.log  main;  #访问日志

日志切割脚本

按天切割访问日志,脚本如下:

[root@localhost script]# cat ~/script/cut_nginx_log.sh
#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="boheyan.access"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

然后加入定时任务,每天0点定时执行

[root@localhost script]# cat >> /var/spool/cron/root << EOF
> 0 0 * * * /bin/sh /root/script/cut_nginx_log.sh > /dev/null 2>&1
> EOF
[root@localhost script]# crontab -l
0 0 * * * /bin/sh /root/script/cut_nginx_log.sh > /dev/null 2>&1

Location 路由

  • ~ 用于区分大小写匹配
  • ~* 用于不区分大小写匹配
  • !~/!~* 用于对匹配取反
  • ^~ 进行常规字符串匹配检查,不做正则表达式检查
顺序 特殊字符组合匹配 匹配说明
1 location = / { 精准匹配
2 location ^~ /images/ { 匹配常规字符串,不能正则匹配检查
3 location ~* \.(gif|jpg|jpeg)$ { 正则匹配
4 location /documents/ { 匹配常规字符串,如果有正则,则优先匹配正则
5 location / { 所有location都不能匹配后的默认匹配

rewrite语法

301跳转例子: rewrite ^/(.*) http://www.boheyan.cn/$1 permanent;

flag标记符号 说明
last 本条规则匹配完成后,继续向下匹配新的location URL规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址