1.安装Nginx所需要的pcre库和openssl #pcre兼容正则表达 openssl在使用https会用到此模块不装会报错
yum install -y pcre-devel openssl-devel
2.下载Nginx源码包 到官网http://nginx.org/下载 企业应用建议下载半年以前的稳定版 #wget 可以加-q参数不显示过程
wget http://nginx.org/download/nginx-1.14.0.tar.gz
3.创建nginx程序用户
useradd -s /sbin/nologin -M www
4.解压源码包编译安装 并做软连接启动 #停止nginx -s stop 重启nginx -s reload 检测配置文件nginx -t 查看状态信息nginx -V
tar xf nginx-1.14.0.tar.gz -C /usr/src/ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module;make;make install ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ /usr/local/nginx/sbin/nginx
5.初步了解nginx主配置文件 #在/usr/local/nginx/conf/nginx.conf nginx的优化主要在于修改nginx的主配文件
egrep -v "#|^$" nginx.conf
worker_processes 1;           #worker进程的数量
error_log  logs/error.log;          #错误日志(默认没开)
pid        logs/nginx.pid;             #进程号(默认没开)
events {                    #事件区块开始
    worker_connections  1024;         #每个worker进程支持的最大连接数
}                    #事件区块结束
http {                 #http区块开始
    include       mime.types;             #Nginx支持的媒体类型库文件包含
    default_type  application/octet-stream;    #默认的媒体类型
    sendfile        on;                #开启高效传输模式
    keepalive_timeout  65;            #连接超时。
    server {                      #网站配置区域(第一个server第一个虚拟主机站点)
        listen       80;                #提供服务的端口,默认80
        server_name  www.wk.org;       #提供服务的域名主机名
        location / {                    #第一个Location区块开始
            root   html;                  #站点的根目录(相对于nginx安装路径)
            index  index.html index.htm;          #默认的首页文件,多个用空格分开
        }
        error_page 500 502 503 504  /50x.html;      #出现对应的http状态码时,使用50x.html回应客户
        location = /50x.html {              #Location区块开始,访问50x.html
            root   html;                       #指定对应的站点目录为html
        }
    }
    server {                        #网站配置区域(第二个server第二个虚拟主机站点)
        listen       80;                   #提供服务的端口,默认80
        server_name  bbs.wk.org;         #提供服务的域名主机名
        location / {                  #服务区块
            root   html;                   #相对路径(nginx安装路径)
            index  index.html index.htm;
        }
        location = /50x.html {             #发生错误访问的页面
            root   html;
        }
    }
}


worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf; #虚拟网站配置信息统一放在了当前的extra目录下
include extra/mail.conf;
include extra/status.conf;
}
cat extra/www.conf
server {
listen 80;
server_name www.wk.com;
location / {
root /var/www/html/wwwcom;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
}
/usr/local/nginx/sbin/nginx -V #查看是否编译了信息状态模块
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module   #标红为信息状态模块
cat /usr/local/nginx/conf/extra/status.conf
server{
 listen 80;
    server_name  status.wk.com;
    location / {
       stub_status  on;     #开启信息功能模块    
           access_log   off;     #不做日志访问记录
         }
}
在主配文件 加入error_log logs/error.log;

mkdir -p /server/scripts/
cd /server/scripts/
vim cut_nginx.log.sh
#!/bin
#日志切割脚本
Date=`date +%Y%m%d`
Bdir="/usr/local/nginx"
Nginxlogdir="$Bdir/logs"
Logname="access"
[ -d $Nginxlogdir ] && cd $Nginxlogdir ||exit 2
[ -f "$Logname".log ] ||exit 3
/bin/mv "$Logname".log "$Date"_"$Logname".log
$Bdir/sbin/nginx -s reload
find "$Nginxlogdir" -name "$Logname*" -type f -mtime +7|xargs /bin/rm
放入定时任务
或
#!/bin/bash
#日志切割脚本
log_path=/var/log/nginx/
log_file=/var/log/nginx/`date +%Y`/`date +%m`
pid_file=‘/usr/local/nginx/logs/nginx.pid‘
old_access_log=‘/usr/local/nginx/logs/www.amber.com.access.log‘
access_log=`echo $old_access_log |awk -F‘/‘ ‘{print $NF}‘`
err_log=‘/usr/local/nginx/logs/error.log‘
if [ -f $pid_file ];then
    [ -d $log_file ] || mkdir -p
$log_file
    mv $old_access_log
${log_file}/${access_log}-$(date +%d)
    kill -USR1 `cat $pid_file`
    find $log_path -mtime +30 |xargs rm
-rf
else
    echo "nginx未运行!" |tee
-a $err_log
fi
二者都可用


原文:https://www.cnblogs.com/ywrj/p/9374700.html