热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

(三)Nginx配置·续

2019独角兽企业重金招聘Python工程师标准概述前文写了关于Nginx环境配置,但是还没有完,接下来将会继续讲三个相关的配置主要是以下三个1.

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

概述

  • 前文写了关于Nginx环境配置,但是还没有完,接下来将会继续讲三个相关的配置
  • 主要是以下三个 1.Nginx访问日志 2.Nginx日志切割 3.静态文件不记录日志和过期时间

Nginx访问日志

1.先看看日志格式

#日志的路径
[root@centos7mei ~]# vim /usr/local/ngin/conf/nginx.conf
#搜索log_format
#注意下面这段log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"';

名词解释:

名称解释
$remote_addr客户端IP(公网IP)
$http_x_forwarded_for代理服务器的IP
$time_local服务器本地时间
$host访问主机名(域名)
$request_uri访问的url地址
$status状态码
$http_refererreferer
$http_user_agentuser_agent

2.刚才在主配置文件中定义了日志格式,接下来还需要在虚拟主机配置中定义日志的储存路径,最后面指定日志的格式名字

[root@centos7mei ~]# cd /usr/local/nginx/conf/vhost/
[root@centos7mei vhost]# ls
default.conf test.com.conf
#编辑配置文件
[root@centos7mei vhost]# vim test.com.conf
#内容如下
[root@centos7mei vhost]# cat test.com.conf
server
{listen 80;server_name test.com test2.com test3.com;index index.html index.htm index.php;root /data/wwwroot/test.com;if ($host != 'test.com' ) {rewrite ^/(.*)$ http://test.com/$1 permanent;}#加上这一行access_log /tmp/1.log combined_realip;
}
#检查读写并重新加载服务
[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload

3.测试

#访问两次网站
[root@centos7mei vhost]# curl -x127.0.0.1:80 test3.com/index.html/SDFAS -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Fri, 17 Aug 2018 18:24:04 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html/SDFAS[root@centos7mei vhost]# curl -x127.0.0.1:80 test2.com/index.html/SDFAS -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Fri, 17 Aug 2018 18:24:23 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html/SDFAS
#这里的问题是直接抄视频,没看自己写的具体路径
[root@centos7mei vhost]# cat /tmp/test.com.log
cat: /tmp/test.com.log: No such file or directory
#这下面就能看到访问日志了
[root@centos7mei vhost]# cat /tmp/1.log
127.0.0.1 - [18/Aug/2018:02:24:04 +0800] test3.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"
127.0.0.1 - [18/Aug/2018:02:24:23 +0800] test2.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"

Nginx日志切割

  • 日志记录了很多东西,但不是每个都是有用的,所以需要对其做一些操作,去其糟粕取其精华。
  • 切割日志需要用到shell脚本

1.添加脚本

[root@centos7mei vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
#脚本内容
[root@centos7mei vhost]# cat /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
domv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
#执行一下脚本
[root@centos7mei vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180817
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls 1.log
+ for log in '`ls *.log`'
+ mv 1.log 1.log-20180817
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 16489

2,测试

#清理文件
[root@centos7mei vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
#这里报错是因为没有满足条件的文件
rm: missing operand
Try 'rm --help' for more information.
#查找下
[root@centos7mei vhost]# find /tmp/ -name *.log-* -type f
/tmp/1.log-20180817
## 最后添加计划任务,清理文件总不能我们自己每天手动清理吧
[root@centos7mei vhost]# crontab -e
#内容
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

静态文件不记录日志和过期时间

  • 静态文件可以不记录日志,原因是除了占空间,没别的什么用

ps: 不记录日志就是说我们在访客记录中看不到,访客访问静态文件的信息

  • 简单的来说就是添加配置文件,让访问日志不记录静态文件,还有给图片缓存添加过期时间

1.编辑配置文件

#进入虚拟主机配置文件
[root@centos7mei vhost]# vim test.com.conf
#内容
[root@centos7mei vhost]# cat test.com.conf
server
{listen 80;server_name test.com test2.com test3.com;index index.html index.htm index.php;root /data/wwwroot/test.com;if ($host != 'test.com' ) {rewrite ^/(.*)$ http://test.com/$1 permanent;}#主要是这里到access_log前面的括号完location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${#过期时间expires 7d;#表示 不记录访问日志access_log off;}location ~ .*\.(js|css)${expires 12h;access_log off;}access_log /tmp/1.log combined_realip;
}
#检查读写和重新加载服务
[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload

2.测试

#这里我们创建两个图片文件,就是图片格式结尾的文件,然后去访问,最后查看访问日志
[root@centos7mei vhost]# cd /data/wwwroot/test.com/
[root@centos7mei test.com]# ls
index.html
#这里马虎的出现了一个符号错误
[root@centos7mei test.com]# vim 1,gif
[root@centos7mei test.com]# vim 2.gs
#导致访问出现404
[root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/1.gif



404 Not Found



nginx/1.8.0



#原来是点写成了逗号,根本没那个文件,所以报错了
[root@centos7mei test.com]# ls
1,gif 2.gs index.html
[root@centos7mei test.com]# rm -f 1,gif
[root@centos7mei test.com]# ls
2.gs index.html
[root@centos7mei test.com]# vim 1.gif
[root@centos7mei test.com]# ls
1.gif 2.gs index.html
#连接下
[root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/1.gif
fdsafs:
[root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/2.gs
dasdfsfdadf
[root@centos7mei test.com]# curl -I -x127.0.0.1:80 test.com/1.GIF
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Sat, 18 Aug 2018 01:21:14 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
#添加-I选项能看到更加详细的信息
[root@centos7mei test.com]# curl -I -x127.0.0.1:80 test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sat, 18 Aug 2018 01:21:21 GMT
Content-Type: image/gif
Content-Length: 8
Last-Modified: Sat, 18 Aug 2018 01:04:52 GMT
Connection: keep-alive
ETag: "5b7770b4-8"
Expires: Sat, 25 Aug 2018 01:21:21 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
#这里能看到我们访问的1.gif(小写的)就没有记录到日志里面了
[root@centos7mei test.com]# cat /tmp/1.log
127.0.0.1 - [18/Aug/2018:03:35:40 +0800] test2.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"
127.0.0.1 - [18/Aug/2018:09:21:14 +0800] test.com "/1.GIF" 404 "-" "curl/7.29.0"

转:https://my.oschina.net/u/3707523/blog/1929898



推荐阅读
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文总结了Java中日期格式化的常用方法,并给出了示例代码。通过使用SimpleDateFormat类和jstl fmt标签库,可以实现日期的格式化和显示。在页面中添加相应的标签库引用后,可以使用不同的日期格式化样式来显示当前年份和月份。该文提供了详细的代码示例和说明。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • switch语句的一些用法及注意事项
    本文介绍了使用switch语句时的一些用法和注意事项,包括如何实现"fall through"、default语句的作用、在case语句中定义变量时可能出现的问题以及解决方法。同时也提到了C#严格控制switch分支不允许贯穿的规定。通过本文的介绍,读者可以更好地理解和使用switch语句。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
author-avatar
丁丽君coolboy
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有