优化nginx配置文件

一、利用include功能优化nginx配置文件

为了让nginx.conf配置文件看起来简洁,可以把每个server标签模块化管理。在http标签里加上一个include替代server标签,也就是把server标签的内容放到include指定的目录下。具体操作如下。
原来的配置文件:

			worker_processes  1;
events {
worker_connections  1024;
	}
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
server {
    listen       80;
    server_name  www.ceishi.com;
    location / {
        root   html/www;
        index  index.html index.htm;
    }
}
server {
    listen       80;
    server_name  bbs.ceishi.com;
    location / {
        root   html/bbs;
        index  index.html index.htm;
    }
}
server {
    listen       80;
    server_name  blog.ceishi.com;
    location / { 
        root   html/blog;
        index  index.html index.htm;
    }   
}   

}

更改之前先备份原来的配置文件

 cp  /home/wode/nginx/conf/nginx.conf /home/wode/nginx/conf/nginx.conf.ceishi.1

更改为:

 worker_processes  1;
events {
worker_connections  1024;
}
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
#nginx vhosts config #在这个位置加上include,并指定各个虚拟主机server标签位置
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}

创建include指定目录

 mkdir /home/wode/nginx/conf/extra -p

向include分别写入server标签内容

 cd /home/shitan/nginx/conf/ 
 mkdir /home/shitan/nginx/conf/extra -p
 sed -n "10,17p" nginx.conf.ceishi.1 > ./extra/www.conf#写入www的server标签
 cat extra/www.conf #查看
sed -n "18,25p" nginx.conf.ceishi.1 > ./extra/bbs.conf#写入bbs的server标签
cat extra/bbs.conf #查看有没有错误
sed -n "26,33p" nginx.conf.ceishi.1 > ./extra/blog.conf#写入blog的server标签
 cat ./extra/blog.conf 

重启nginx
/home/wode/nginx/sbin/nginx -t#检查语法
/home/wode/nginx/sbin/nginx -s reload #重启
测试http服务

[root@localhost conf]#curl www.ceishi.com
www.ceshi.com
[root@localhost conf]#curl bbs.ceishi.com
bbs.ceshi.com
[root@localhost conf]#curl blog.ceishi.com
bbs.ceshi.com

二、nginx虚拟主机别名的配置

别名意思就是比喻你在浏览器上访问 www.sohu.com 和你直接输入 sohu.com 最终都访问 www.sohu.com。多域名访问同一个站点目录这就是别名。有两种方法实现,第一种:叫别名,第二种用rewrite规则实现

2.1第一种实现方法(别名)

现在以www域名的虚拟主机为例,实现别名功能。

 vim ./extra/www.conf 

修改后

    #cat ./extra/www.conf      
server {
    listen       80;
    server_name  www.ceishi.com ceishi.com;#在server_name后加上别名,空格隔开
    location / {
        root   html/www;
        index  index.html index.htm;
    }
}
  • DNS解析加入的别名

       vim  /etc/hosts
    
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     ::1         localhost localhost.localdomain localhost6 				localhost6.localdomain6
     	192.168.31.230 www.ceishi.com  bbs.ceishi.com   blog.ceishi.com  	ceishi.com 
     	#加上别名,空格隔开
    
  • 测试别名

     #curl ceishi.com
      www.ceshi.com
      curl -I ceishi.com
     HTTP/1.1 200 OK#标志为200
     Server: nginx/1.10.3
     Date: Wed, 12 Jun 2019 04:15:46 GMT
     Content-Type: text/html
     Content-Length: 14
     Last-Modified: Tue, 11 Jun 2019 07:20:43 GMT
     Connection: keep-alive
     ETag: "5cff564b-e"
     Accept-Ranges: bytes
    

2.2 第二种利用rewrite 301跳转实现别名

以www域名为例:

  • 更改/extra/www配置

    #vim ./extra/www.conf 
    server {
      listen      80;
      server_name ceishi.com;
      rewrite ^/(.*) http://www.ceishi.com/$1 permanent;#使用rewrite模块匹配,permanent代表永久跳转
     }
    server {
      listen       80;
     server_name  www.ceishi.com;#删除别名,以免报错
     location / {
         root   html/www;
         index  index.html index.htm;
     }
    }
    
  • 测试

    #curl -I ceishi.com 
     HTTP/1.1 301 Moved Permanently#301跳转标志,如果是别名就是200
     Server: nginx/1.10.3
     Date: Wed, 12 Jun 2019 04:05:13 GMT
     Content-Type: text/html
     Content-Length: 185
     Connection: keep-alive
     Location: http://www.ceishi.com/
    

三、nginx状态信息配置

nginx软件的功能模块中有个ngx_http_stub_status_mode模块,这个模块的功能是记录基本的访问信息,让使用者了解nginx的工作状态。
检查编译安装时有没有安装ngx_http_stub_status_mode模块。

#./sbin/nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17)  (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/home/shitan/nginx-1.10.1 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
#存在ngx_http_stub_status_mode这个模块
  • 配置过程如下
    在/extra目录下创建一个监控状态的server标签,名字为status.conf。

    cat >> /home/shitan/nginx/conf/extra/status.conf << eof
     #status
    server{
    listen 80;
    server_name status.www.ceishi.com;
    location / {
    stub_status on;
    access_log   off;
    }
     }
    eof
    

    然后在nginx.conf配置文件中加上一个include指定状态目录

    #cat ../nginx.conf
     worker_processes  1;
     events {
    worker_connections  1024;
     }
     http {
        include       mime.types;
        default_type  application/octet-stream;
     	sendfile        on;
         keepalive_timeout  65;
         #nginx vhosts config
         include extra/www.conf;
     	 include extra/bbs.conf;
     	 include extra/blog.conf;    
          include extra/status.conf; #包含状态目录
     }
    
  • 重启nginx让配置生效

    #./sbin/nginx -t
    nginx: the configuration file /home/shitan/nginx-1.10.1/conf/nginx.conf syntax is ok
    nginx: configuration file /home/shitan/nginx-1.10.1/conf/nginx.conf test is successful
    #./sbin/nginx -s reload

  • 查看链接状态

    #curl status.www.ceishi.com;   
    Active connections: 1 #表示当前的连接数
    server accepts handled requests
    48 48 40 
    #第一个server表示nginx启动到现在共处理了48个链接
    #第二个accpets表示nginx启动到现在共成功创建了48次握手。请求丢失书=(握手数-连接数)
    #第三个handled requests,表示总共处理了40次请求
    Reading: 0 Writing: 1 Waiting: 0 
    #Reading:  nginx读取到客户端的header信息数
    # Writing: nginx返回给客户端的header信息数
    # Waiting:nginx已经处理完正在等候下一次请求指令的驻留链接,在开启keep-alive的情况下,这个值等于active-(reading+writing)
    

    四、nginx错误日志

    nginx的错误信息是调试nginx服务的重要手段,属于核心功能模块(ngx_core_module)的参数,该参数名字为error_log,可以放在main区块中配置,也可以放置在不同的虚拟主机中单独记录虚拟主机错误信息。
    error_log语法格式以及参数说明如下:

    error_log        file             level 
    #关键字       日志文件       错误日志等级
    

    其中关键字error_log不能改变,日志文件可以指定任意存放日志的目录。错误等级可以分为debug,info,notice,warn,error,crit,alert,emerg等,一般场景设置为warn,error,crit这三个级别。
    error_log的默认值为:
    error_log logs/error.log error;
    可以放置的标签段
    main ,http,server,location

  • 修改nginx.conf增加记录错误日志

    #cat ./conf/nginx.conf
    worker_processes  1;
     error_log logs/error.log error;#在main区块中添加错误日志,记录所有虚拟主机日志信息。记录位置相对于nginx安装目录
     events {
     worker_connections  1024;
     }
     http {
      include       mime.types;
       default_type  application/octet-stream;
      sendfile        on;
     keepalive_timeout  65;
     #nginx vhosts config
     include extra/www.conf;
      include extra/bbs.conf;
      include extra/blog.conf;    
      include extra/status.conf; 
     }
    

重启nginx使配置生效

./sbin/nginx -t
./sbin/nginx -s reload
cat logs/error.log #查看日志信息

五、nginx访问日志

Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站分析用户的浏览行为。此功能由ngx_http_log_module模块负责。ngx_http_log_module模块介绍

  • 访问日志参数
    nginx的访问日志参数主要有下面这两个参数控制。

    log_format:用来定义日志的格式(可以定义多种日志格式,取不同的名字即可)
    access_log :用来指定日志文件的路径以及使用何种日志格式记录日志,一般放在虚拟主机上,便于区分。
    nginx默认访问日志的配置如下:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '      #$remote_addr表示远程地址
       '$status $body_bytes_sent "$http_referer" '
       '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  logs/access.log  main;#main表示用main这种格式记录
    
  • 访问日志的log_format一般放在nginx.conf主配置文件里,放在http标签里server标签外。下面是一个例子。

      #cat nginx.conf
    worker_processes  1;
    error_log logs/error.log error;
    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"';#添加日志格式
    
     #nginx vhosts config
     include extra/www.conf;
     include extra/bbs.conf;
     include extra/blog.conf;    
     include extra/status.conf; 
     }
    

log_format参数说明:
在这里插入图片描述
在这里插入图片描述

  • 访问日志的 access_log一般放在虚拟主机里,即/extra/目录下,下面以域名www为例。
    access_log放在server标签的location下面。

    #cat extra/www.conf 
    server {
     listen       80;
     server_name  www.ceishi.com ceishi.com;
     location / {
         root   html/www;
         index  index.html index.htm;
     }
      access_log  logs/access_www.log  main;#放在此位置
    }
    
  • 重启nginx使配置生效

    ../sbin/nginx -t#检查语法
    
     ../sbin/nginx -s reload#重新加载
    
  • 查看访问日志
    用手机和电脑访问192.168.31.230看到的结果
    tail -f …/logs/access_www.log
    192.168.31.92 - - [13/Jun/2019:16:10:07 +0800] “GET / HTTP/1.1” 200 14 “-” “Mozilla/5.0 (Linux; U; Android 9;zh-cn; ) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.1 Mobile Safari/537.36” "-"手机
    192.168.31.196 - - [13/Jun/2019:16:11:05 +0800] “GET /favicon.ico HTTP/1.1” 404 571 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)” "-"电脑

  • 实现nginx访问日志轮询
    默认情况下nginx会把所有的访问日志生成到一个指定的访问日志文件access.log中,时间长了会导致日志内容太多不利于分析和处理日志,因此有必要按天对nginx访问日志切割。nginx不带轮询工具,需要自己写一个脚本。下面是一个按天的脚本。

    #!/bin/sh
     Dateformate=`date +%Y%m%d -d -1day`#前一天,因为0点备份
     Basedir="/home/shitan/nginx"
     Nginxlogdir="$Basedir/logs"
     Logname="access_www"
     [ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
     [ -f ${Logname}.log ]||exit 1
     /bin/mv ${Logname}.log ${Dateformate}_${Logname}.log
     $Basedir/sbin/nginx -s reload
     #添加定时任务
     crontab -e
     00 00 * * *  /bin/sh /home/script/cut_nginx_log.sh >/dev/null 2>&1
    

猜你喜欢

转载自blog.csdn.net/weixin_44596822/article/details/91489463