Nginx常用功能配置一

Nginx常用功能配置

参数include配置

说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,如果过虚拟主机的数量不多,也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和Nginx的主配置文件nginx.conf分离开即可。

注意:include里包含每个server,所以每个server的conf配置文件需要在include的配置文件下,例如:www.conf 的配置文件在/application/nginx/conf/extra下,而nginx.conf在/application/nginx/conf/下

具体操作步骤

###创建文件放置目录###
mkdir /application/nginx/conf/extra -p

###编辑nginx.conf,把server模块全部删除,然后添加include模块###
cd /application/nginx/conf/
vim nginx.conf
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;
    include extra/bbs.conf;
    include extra/blog.conf;  #(这三个可缩减为include extra/*.conf,此操作执行后,以后新添加的虚拟主机配置,则不必每次进行修改添加)
}

###分别生成bbs.conf,www.conf,blog.conf,三个虚拟主机文件###
cd /application/nginx/conf/extra

vim www.conf

server {
    listen 80;
    server_name www.etiantian.org;
    location / {
    root html/www;
    index index.html index.htm;
  }
}

vim bbs.conf

server {
    listen 80;
    server_name bbs.etiantian.org;
    location / {
    root html/bbs;
    index index.html index.htm;
  }
}

vim blog.conf

server {
    listen 80;
    server_name blog.etiantian.org;
    location / {
    root html/blog;
    index index.html index.htm;
  }
}

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

###在其他客户端上先进行hosts解析###
vim /etc/hosts
172.16.1.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org

###hosts解析完后,然后在客户端上验证是否生效###
curl www.etiantian.org
www
curl bbs.etiantian.org
bbs
curl blog.etiantian.org
blog

配置Nginx status

这个模块主要是记录Nginx的基本访问状态信息,让使用者了解nginx的工作状态

具体操作步骤

###检查编译安装时设置的参数,是否存在--with-http_stub_status_module,这个模块需要在编译Nginx时进行安装###
../sbin/nginx -V

###配置status.conf###
cat >>/application/nginx/conf/extra/status.conf<<EOF
##status
server {
    listen 80;
    server_name status.etiantian.org;
    location / {
    stub_status on;
    access_log off;
    }
}
EOF

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

###需要在window端加一行域名解析###
在C:\windows\System32\drivers\etc下找到hosts文件进行添加域名解析
10.0.0.8 status.etiantian.org

禁止和允许的IP设置

可以用location的方式实现状态信息配置,例如在任意一个虚拟主机里,为server标签增加如下配置:

例如:
location /nginx_status {
    stub_status on;
    access_log off;
    allow 10.0.0.0/24; #(设置允许的IP端访问)
    deny all; #(设置禁止的IP端访问)
}

nginx配置错误日志(error_log)

简介:nginx会把自身运行的故障信息及用户访问的日志信息记录到指定的日志文件里。

关键字error_log不能改变,日志文件可以指定任意存放日志的目录,错误日志基本常见的又【debug|inof|notice|warn|error|crit|alert|emerg】,级别越高记录的信息越少,生产场景一般是warn|error|crit这三个级别之一,注意不要配置info等较低级别,会带来巨大的磁盘I/O消耗。

注意:编辑该配置文件:error_log logs/error.log;#(一般配置这一行就可以)配置在主的nginx.conf

具体操作步骤

cd /application/nginx/conf/
vim nginx.conf
worker_processes 1;
error_log logs/error.log;#(一般配置这一行就可以)
events {
worker_connections 1024;
}
http {
...
}

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

nginx配置访问日志(access_log)

简介:Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户浏览行为;此功能由ngx_http_log_module模块负责,log_format 表示用什么格式记录访问日志,("$http_referer"可以查看从上一级目录从哪里来的地址),对应官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html

具体配置说明:

第一步:在nginx的的主配置文件中加入log_format main格式

编辑该配置文件:/application/nginx/conf/nginx.conf放入:
log_format main '$remote_addr - $remote_user [$time_local] "$requset" '
                           '$status $body_bytes_sent "$http_referer" '
                           '"$http_user_agent" "$http_x_forwarded_for" ';

配合:access_log logs/access.log main;进行使用

第二步:配置对应虚拟机的配置文件

cd /application/nginx/conf/extra/
vim www.conf
server{
      listen 80;
      server_name www.etiantian.org etiantian.org;
      location / {
          root html/www;
          index index.html index.htm;
      }
      access_log logs/www_access.log main;(logs/www_access.log表示记录名,mian表示使用上述log_format的记录格式)
}

具体操作步骤

###配置主配置nginx.conf文件###
cd /application/nginx/conf/
vim nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
     worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

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


include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}

###然后配置每个虚拟主机使用上述格式记录,www.conf,blog.conf,bbs.conf###
cd /application/nginx/conf/extra/
vim www.conf
server{
    listen 80;
    server_name www.etiantian.org ;
    location / {
        root html/www;
        ndex index.html index.htm;
    }
    access_log logs/www_access.log main; #(logs/www_access.log表示记录名,mian表示使用上述log_format的记录格式)
    }

vim blog.conf
server{
    listen 80;
    server_name blog.etiantian.org ;
    location / {
        root html/blog;
        index index.html index.htm;
    }
    access_log logs/blog_access.log main;
    }

vim bbs.conf
server{
    listen 80;
    server_name bbs.etiantian.org ;
    location / {
        root html/bbs;
        index index.html index.htm;
    }
    access_log logs/bbs_access.log main;
    }

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

优化access.log

说明:默认情况Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于分默认情况Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于分析,所有可以对access.log进行轮询切割保存。

具体操作步骤

####首先创建一个存放脚本的文件夹###
mkdir -p /server/scripts
cd /server/scripts

###定义一个Nginx轮询切割脚本###
vim /server/script/cut_nginx_log.sh

#!/bin/sh
Dateformat=`date +%Y%a%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit l
[ -f ${Logname}.log ]||exit l
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

###定时进行轮询操作nginx脚本###
cat >>/var/spool/cron/root <<EOF

#cut nginx access log by oldboy
00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1
EOF

###查看crontab配置###

crontabl -l

猜你喜欢

转载自www.cnblogs.com/LEO00/p/10330770.html