nginx优化

一、nginx日志文件配置

1.log_format 日志格式语法

log_format name [format ....]

2.默认配置文件的日志格式

[root@ping ~]# sed -n '18,20p' /etc/nginx/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"';
main:是日志格式的名字
$remote_addr:是客户端访问nginx服务的IP地址
$remote_user:是客户端用户的名称
$time_local:是客户端访问的nginx服务的时间和时区
$request:是客户端访问的nginx服务的URL地址和HTTP协议
$status:是客户端访问的nginx服务请求状态码如200,404;
$body_bytes_sent:是nginx服务发送给客户端的文件大小;
$http_referer:是客户端访问的nginx服务,从那个网站的页面访问
$http_user_agent:是客户端的浏览器相关信息
$http_x_forwarded_for:客户端IP地址

3.查看日志文件

[root@ping ~]# tail -1 /var/log/nginx/access.log 
192.168.100.1 - - [08/Mar/2018:19:53:03 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "http://192.168.100.6/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "-"

二、日志切割

1.将nginx访问日志重命名

[root@ping ~]# mv /var/log/nginx/access.log /var/log/nginx/access.log-`date +%F`-v1

2.查看nginx访问日志的大小

[root@ping ~]# ls -lh /var/log/nginx/
-rw-r--r-- 1 nginx root 0 3月 8 20:09 access.log
-rw-r----- 1 nginx adm 415 3月 8 19:53 access.log-2018-03-08-v1
-rw-r----- 1 nginx adm 549 3月 8 19:53 error.log

3.查看nginx主进程和工作进程

[root@ping ~]# ps -ef | grep nginx
root 1939 1 0 19:50 ? 00:00:00 nginx: master process nginx
nginx 1940 1939 0 19:50 ? 00:00:00 nginx: worker process
root 2074 1763 0 20:08 pts/0 00:00:00 grep --color=auto nginx

4.让nginx重新生成一个新的日志文件

[root@ping ~]# kill -USR1 1939

5.查看nginx访问日志是否切割成功

[root@ping ~]# ls -lh /var/log/nginx/
-rw-r--r-- 1 nginx root 950 3月 8 20:09 access.log
-rw-r----- 1 nginx adm 415 3月 8 19:53 access.log-2018-03-08-v1
-rw-r----- 1 nginx adm 549 3月 8 19:53 error.log

  

三、隐藏nginx的版本

1.查看http header相关信息

[root@ping ~]# curl -I http://192.168.100.6
HTTP/1.1 200 OK
Server: nginx/1.13.8
Date: Thu, 08 Mar 2018 12:21:54 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 03 Jan 2018 18:15:30 GMT
Connection: keep-alive
ETag: "5a4d1dc2-264"
Accept-Ranges: bytes  

2.查看nginx配置文件是否关闭隐藏版本号

[root@ping ~]# sed -n '15p' /etc/nginx/nginx.conf 
server_tokens off; 

3.nginx重新读取配置文件

[root@ping ~]# nginx -s reload

4.再次查看http header相关信息

[root@ping ~]# curl -I http://192.168.100.6
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 08 Mar 2018 12:23:44 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 03 Jan 2018 18:15:30 GMT
Connection: keep-alive
ETag: "5a4d1dc2-264"
Accept-Ranges: bytes

  

原文地址:https://www.cnblogs.com/pingzhe/p/8530640.html