nginx-配置文件详解<二>

错误页面配置

官网查看错误界面配置点击此处
在这里插入图片描述

error_page code […] [=code] URI | @name 根据http响应状态码来指明特用的错误页面,在安装nginx后默认是备注释掉的

语法使用例如 :error_page 404 /404_customed.html

指定网站为404错误页面,以下举例演示:

  • 搜索一个网站,下载下来上传到安装路径下的/html/下
[root@nginx html]# ls 
50x.html  index.html  中国社会公益网_files  中国社会公益网.html
[root@nginx html]# mv 中国社会公益网.html 404.html
[root@nginx html]# ls 
404.html  50x.html  index.html  中国社会公益网_files
  • 取消配置文件中的注释
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
error_page  404              /404.html;     //取消掉注释,指定404.html为错误页面
......

[root@nginx ~]# nginx -s reload 
  • 网页访问
    在这里插入图片描述
    此时错误页面就是我们之前下载上传的网页
  • 可以根据需要修改状态码
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
        error_page  404  =200            /404.html;    //添加参数=,修改状态码为200
......

[root@nginx ~]# nginx -s reload

在这里插入图片描述

日志格式

在这里插入图片描述
默认是注释掉的,取消注释开启

  • 取消注释
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
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;
......

[root@nginx ~]# nginx -s reload 
  • 网站访问查看日志
    在这里插入图片描述
//查看日志
[root@nginx ~]# tail -f /usr/local/nginx/logs/access.log 
192.168.218.1 - - [27/Oct/2021:18:44:51 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36" "-"

//参数详解
    $remote_addr     //客户端
    $remote_user      //远程用户,没有做认证的默认为 -
    [$time_local]        //本地时间
    $request            //请求方法,协议版本
    $status           //状态码
    $body_bytes_sent     //发送主体字节数,若是从缓存获取就是0
    $http_referer          //是否跳转,如果不是跳转就是 -
    $http_user_agent    //用户代理,即浏览器
    $http_x_forwarded_for    //从哪里跳转,没有跳转为0

//注意:此处可用变量为nginx各模块内建变量,如果不需要某一变量可以自行删除

平滑升级

为nginx添加新的功能,但是不能影响用户的体验,所以就要掌握平滑升级的方法

平滑升级步骤:不能进行安装操作

  • 获取老版本的编译参数 -V
  • 获取新版本或新功能的软件包
  • 对新功能或新版本的软件包进行编译
  • 备份老程序
  • 停掉老程序并用新程序使用老程序的配置文件进行启动
  • 检验功能,若无问题即用新程序替换老程序

以下演示添加echo功能来演示平滑升级:

  • 获取老版本的编译参数
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
  • 获取新功能的软件包
[root@localhost ~]# ls 
anaconda-ks.cfg  echo-nginx-module-master.zip
nginx-1.20.1.tar.gz
//已提前下载好上传

[root@nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值