1. 通用配置文件说明
nginx1.6通用配置文件
| 1 | user nginx nginx; | 
思考1:location默认的root在什么位置?
| 1 | # 请问该server访问的路径是什么地方? | 
2 全局配置
2.1 nginx进程和cpu绑定
| 1 | worker_cpu_affinity 00000001 00000010 00000100 00001000; | 
#将Nginx工作进程绑定到指定的CPU核心上,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占一颗CPU,但是可以保证此进程不会运行到其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升了nginx服务器的性能。
2.2 docker启动nginx前台进程配置
| 1 | daemon off; | 
2.3 开启events的epoll事件驱动
| 1 | events { | 
2.4 关闭“惊群”
| 1 | events { | 
#优化同一时刻只有一个请求,避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒,默认参数为off
全部唤醒的过程也成为”惊群”,因此nginx刚安装完以后要进行适当的优化。
2.5 一个进程可以处理多个请求
| 1 | events { | 
#Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后一个同时接受多个。
3 http配置段
3.1 location配置段的root和alias
root:指定web的家目录,在定义location的时候,文件的绝对路径等于:root+location
比如:1
2
3
4location /aa {
root /webdata/bb;
}
## 访问http://www.mykernel.cn/aa 的时候,对应的文件位置为:/webdata/bb/aa
alias:定义路径别名,会把访问的路径重新定义到其指定的路径
比如:1
2
3
4location /aa {
alias /webdata/bb; #最后的斜杠要和location段的一致
}
## 访问http://www.mykernel.cn/aa 的时候,对应的文件位置为:/webdata/bb
一般情况下使用root较多,而且使用绝对路径。
3.2 location配置使用
语法规则: location [=|~|~*|^~] /uri/ { … }1
2
3
4
5
6
7
8
9
10
11
12
13= #用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止向下匹配并且立即处理请求。 
~ #用于标准uri前,表示包含正则表达式并且区分大小写,并且匹配 
!~#用于标准uri前,表示包含正则表达式并且区分大小写,并且不匹配 
~*#用于标准uri前,表示包含正则表达式并且不区分大小写,并且匹配 
!~*#用于标准uri前,表示包含正则表达式并且不区分大小写,并且不匹配 
^~#用于标准uri前,表示包含正则表达式并且匹配以什么开头 
$ #用于标准uri前,表示包含正则表达式并且匹配以什么结尾 
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?等 
* #用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符
3.3 Nginx 四层访问控制
访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制。一般用于某些检测nginx状态页面的访问权限控制。
| 1 | location /about { | 
3.4 Nginx账户认证功能
| 1 | # 安装密码生产工具,借用httpd的工具 | 
3.5 Nginx作为下载服务器配置
| 1 | server { | 
3.6 Nginx内置状态页
nginx状态页面需要做ip和密码认证1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21server {
        listen 83;
        server_name status.mykernel.cn;
        location /nginx_status {
                stub_status;
                allow 192.168.0.0/16;
                allow 127.0.0.1;
                deny all;
                auth_basic "login password"; 
                auth_basic_user_file /etc/nginx/conf.d/.htpasswd;  # 账号
        }
}
# 显示结果
Active connections: 1  # 活动连接数
server accepts handled requests # 接收的总数 处理完成的总数 客户端发起的总数 
 47 47 61 
Reading: 0 Writing: 1 Waiting: 0 
# Reading: 当前状态,正在读取客户端请求报文首部的连接的连接数。
# Writing:当前状态,正在向客户端发送响应报文过程中的连接数。
# Waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active-(reading+writing)。
3.7 Nginx自定义json访问格式的日志
日志格式使用json,便于使用代码或者工具对日志进行统计和分析1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38# 定义log_format,位置为server外,http段内
   log_format access_json '{"@timestamp":"$time_iso8601",'
                         '"host":"$server_addr",'
                         '"clientip":"$remote_addr",'
                         '"size":$body_bytes_sent,'
                         '"responsetime":$request_time,'
                         '"upstreamtime":"$upstream_response_time",'
                         '"upstreamhost":"$upstream_addr",'
                         '"http_host":"$host",' '"uri":"$uri",'
                         '"domain":"$host",' '"xff":"$http_x_forwarded_for",'
                         '"referer":"$http_referer",'
                         '"tcp_xff":"$proxy_protocol_addr",'
                         '"http_user_agent":"$http_user_agent",'
                         '"status":"$status"}';
                         
# 使用日志格式,位置server段内或者location段内
access_log /data/nginx/html/access_json.log access_json;
root@nginx:/etc/nginx/conf.d# cat log_json.conf 
server {
	listen 84;
	server_name status.mykernel.cn;
	location /log_test { 
		access_log /data/nginx/html/access_json.log access_json;
		root /data/nginx/html;
		index index.html;
	}
}
# 日志样式
root@nginx:/data/nginx/html# tail  -f access_json.log 
{"@timestamp":"2020-10-21T17:56:44+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/nginx_status","domain":"192.168.0.223","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"404"}
{"@timestamp":"2020-10-21T17:56:44+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/favicon.ico","domain":"192.168.0.223","xff":"-","referer":"http://192.168.0.223:84/nginx_status","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"404"}
{"@timestamp":"2020-10-21T17:56:50+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/1.html","domain":"192.168.0.223","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"404"}
{"@timestamp":"2020-10-21T17:57:44+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":5,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/log_test/log.html","domain":"192.168.0.223","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"200"}
3.8 Nginx开启压缩
依赖模块ngx_http_gzip_module
配置指令可以使用的范围:http, server, location1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#启用gzip压缩,默认关闭 
gzip on | off; 
#压缩等级由低到高从1到9,默认为1 
gzip_comp_level level; 
#禁用IE6 gzip功能 
gzip_disable "MSIE [1-6]\."; 
#gzip压缩的最小文件大小,低于设置值的文件将不会压缩 
gzip_min_length 1k; 
#启用压缩功能时,协议的最小版本,默认HTTP/1.1 
gzip_http_version 1.0 | 1.1; 
#指定Nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k; 
gzip_buffers number size; 
#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错 
gzip_types mime-type ...; 
#如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding” 
gzip_vary on | off;
## 
root@nginx:/etc/nginx/conf.d# cat gzip.conf 
server {
	listen 85;
	server_name gzip.mykernel.cn;
	location /gzip {
		root /data/nginx/html;
		index index.html;
		gzip on; 
		gzip_min_length 30k; 
		gzip_comp_level 9;
		gzip_vary off; #未测试出效果
		access_log /data/nginx/html/access_json.log access_json;
	}
}