nginx 高级配置

nginx状态页

1 ngx_http_auth_basic_module
2 location /nginx_status {
3 stub_status;
4 allow 192.168.0.0/16;
5 allow 127.0.0.1;
6 deny all;
7 }
curl 127.0.0.1/status
Active connections: 1 
server accepts handled requests
1 1 1 
Reading: 0 Writing: 1 Waiting: 0

上⾯三个数字分别对应accepts,handled,requests三个值
Active connections: 当前处于活动状态的客⼾端连接数,包括连接等待空闲连接数。
accepts:统计总值,Nginx⾃启动后已经接受的客⼾端请求的总数。
handled:统计总值,Nginx⾃启动后已经处理完成的客⼾端请求的总数,通常等于accepts,除⾮有因 worker_connections限制等被拒绝的连接。
requests:统计总值,Nginx⾃启动后客⼾端发来的总的请求数。
Reading:当前状态,正在读取客⼾端请求报⽂⾸部的连接的连接数。
Writing:当前状态,正在向客⼾端发送响应报⽂过程中的连接数。
Waiting:当前状态,正在等待客⼾端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active – (reading+writing),

第三方模块

nginx功能模块扩展(定制开发或开源)需重新编译
--add-module=PATH(模块路径)

内置变量

 1 $remote_addr    存放了客户端的地址,公网IP
 2 $args    存放了URL中的指令, http://www.baidu.com/main/index.do?id=20932849023894&partner=search 中id=20932849023894&partner=search
 3 $document_root    保存了针对当前资源的请求的系统根目录,如 /apps/nginx/html
 4 $document_url    保存了当前请求中不包含指令的URI 如 http://www.admin.net/main/index.do?id=20190221&partner=search 会被定义为/main/index.do 
 5 $host    存放了请求的host名称
 6 $http_user_agent    客户端浏览器的详细信息
 7 $http_cookie    客户端的cookie信息
 8 limit_rate_10240
 9 echo $limit_rate 如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置,则显示0
10 $remote_port    客户端请求nginx服务器时随机打开的端口,是每个客户端自己的端口
11 $remote_user    已经经过Auth Basic Module验证的用户名
12 $request_body_file    做反向代理时发给后端服务器的本地资源名称
13 $request_method    请求资源的方式,GET/PUT/DELETE等
14 $request_filename 当前请求的资源文件的路径名称,由root或alias指令与URI请求生成文件绝对路径,如 /app/nginx/html/main/index.html
15 $request_uri 包含请求参数的原始URI,不包含主机名,如: /main/index.do?id=3424234&partner=search
16 $scheme    请求的协议,如 ftp,https,http等
17 $server_protocol 保存了客户端请求资源使用的协议的版本,如 HTTP/1.0,HTTP/1.1,HTTP/2.0等
18 $server_addr 保存了服务器的IP地址
19 $server_name 请求的服务器的主机名
20 $server_port 请求的服务器的端口号

自定义变量
指令 set $variable value

1 set $name magedu
2 echo $name
3 set $my_port $server_port
4 echo $my_port 
5 echo $server_name:$server_port

自定义访问日志

 1 log_format access_json '{"@timestamp": "$time_iso8601",'
 2 '"host": "$server_addr",'
 3 '"clintip": "$remote_addr",'
 4 '"size":"$body_bytes_sent",'
 5 '"responsetime":"$request_time",'
 6 '"upstreamtime":"$upstream_response_time",'
 7 '"upstreamhost":"$upstream_addr",'
 8 '"http_host":"$host",'
 9 '"uri":"$uri",
10 '"domain":"$host",'
11 '"xff":"$http_x_forwarded_for",'
12 '"referer":"$http_referer",'
13 '"tcp_xff":"$proxy_protocol_addr",'
14 '"http_user_agent":"$http_user_agent",'
15 '"status":"$status",';
16 }'
17 access_log /usr/local/nginx/logs/access_json.log access_json;
curl 127.0.0.1/status
{"@timestamp": "2020-12-16T14:18:22+08:00","host": "127.0.0.1","clintip": "127.0.0.1","size":"97","responsetime":"0.000","upstreamtime":"-","upstreamhost":"-","http_host":"127.0.0.1","uri":"/status","domain":"127.0.0.1","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"200",}

json格式的日志访问统计

1 curl 127.0.0.1/status
2 {"@timestamp": "2020-12-16T14:18:22+08:00","host": "127.0.0.1","clintip": "127.0.0.1","size":"97","responsetime":"0.000","upstreamtime":"-","upstreamhost":"-","http_host":"127.0.0.1","uri":"/status","domain":"127.0.0.1","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"200",}

Nginx压缩功能
可以设置压缩比例,降低出口带宽利用率,占用响应的CPU资源
ngx_http_gizp_module

 1 #启用或禁用gzip压缩,默认关闭
 2 gzip on | off;
 3 #压缩比由低到高1到9,默认1
 4 gzip_comp_level level;
 5 #禁用IE6 gzip功能
 6 gzip_disable "MSIE [1-6]."
 7 #gzip压缩的最小文件,小于设置值的文件将不会压缩
 8 gzip_min_length 1k;
 9 #启用压缩功能时,协议的最小版本,默认HTTP/1.1
10 gzip_http_version 1.0 | 1.1
11 #指定nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k
12 gzip_buffers number size;
13 #指明仅对哪些类型的资源执行压缩操作;默认为gzip_type test/html,不用显示指定,否则出错
14 gzip_types mime-type ...;
15 #如果启动压缩,是否在响应报文收不插入"Vary: Accept-Encoding"
16 gzip_vary on | off

https功能
https 有两部分组成: http + SSL/TLS 服务端和客户端的信息传输都会通过TLS进行加密,所以传输逇数据都是加密后的数据

ssl配置参数
ngx_http_ssl_module

 1 #为指定的虚拟主机配置是否启用sll功能,此功能在1.15.0废弃,使用 listen [ssl] 代替
 2 ssl on | off
 3 #当前虚拟主机使用的公钥文件,一般是crt文件
 4 ssl_certificate /path/to/file;
 5 #当前虚拟主机使用的私钥文件,一般是 key 文件
 6 ssl_certificate_key /path/to/file;
 7 #只吃ssl协议版本,早期为sll,现在是TSL,默认为后三个
 8 ssl_protocols [SSLv2] [SSLv3] [TLSv1.1] [TLSv1.2];
 9 #配置sll缓存
10 ssl_session_cache off | none | [builtin[:size]] [shared:name:size]
11 off: 关闭缓存
12 none: 通知客户端支持ssl session cache,但实际不支持
13 builtin[:size]: 使用openSSL内建缓存,为worker进程私有
14 [shared:name:size]: 在各worker之间使用一个共享的缓存,需要定义一个缓存名称和缓存控件大小,一兆可以存储4000个会话信息,多个虚拟主机可以使用相同的缓存名称
15 #客户端连接可以服用ssl session cache中缓存的有效时长 默认5m
16 ssl_seeion_timeout time;

自签名证书

1 自签名CA证书
2 openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt
3 自制key和csr文件
4 openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.magedu.net.key -out www.magedu.net.csr
5 签发证书
6 openssl x509 -req -days 3650 -in www.magedu.net.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.magedu.net.crt
7 验证证书内容
8 openssl x509 -in www.magedu.net.crt -noout -text

nginx证书配置

1 listen 80;
2 listen 443 ssl;
3 ssl_certificate /usr/local/nginx/ssl/www.magedu.net.crt;
4 ssl_certificate_key /usr/local/nginx/ssl/www.magedu.net.key;
5 ssl_session_cache shared:sslcache:20m;
6 ssl_session_timeout 10m;

实现多域名HTTPS
nginx只吃基于单个IP实现多域名的功能,并且还支持单IP多域名的基础之上实现HTTPS,其实是基于nginx的SNI功能实现,
SNI是为了解决一个nginx服务器内使用一个IP绑定多个域名和整数的功能,其具体功能是客户端在连接到服务器建立SSL连
接之前先发送要访问站点的域名 HOSTNAME,服务器再根据这个域名放回给客户端一个合适的证书。

favicon.ico问题
favicon.ico 文件是浏览器收藏网址时显示的图标。当客⼾端使⽤浏览器问⻚⾯时,浏览器会⾃⼰主动发起请求获取
⻚⾯的favicon.ico⽂件,但是当浏览器请求的favicon.ico⽂件不存在时,服务器会记录404⽇志,⽽且浏览器也会
显⽰404报错。
解决办法:
一、服务器不记录访问日志

location = /favicon.ico {
log_not_found off;
access_log off;
}

二、图标保存到指定目录访问

#location - ^/favicon.ico$ {
location = /favicon.ico{
root /data/nginx/html/pc/images;
expires 90d;设置文件过期时间    
}

安全选项

隐藏nginx版本号
更改nginx源码信息并重新编译nginx
vim src/http/ngx_http_header_filter_module.c
#定义响应报文中的server 字段信息
49行: static u_char ngx_http_server_string[] = "Server: linux38" CRLF;

升级OpenSSL版本:
只要 使⽤的是存在缺陷的OpenSSL实例,⽆论是服务器还是客⼾端,都可能因此⽽受到攻击。此问题的原因是在实现
TLS的⼼跳扩展时没有对输⼊进⾏适当验证(缺少边界检查),因此漏洞的名称来源于“⼼跳”(heartbeat)。该程
序错误属于缓冲区过读,即可以读取的数据⽐应该允许读取的还多。

 1 准备OpenSSL源码包: 
 2 # pwd 
 3 /usr/local/src 
 4 # tar xvf openssl-1.1.1d 
 5 编译安装Nginx并制定新版本OpenSSL路径: 
 6 # cd /usr/local/src/nginx-1.16.1/ 
 7 #./configure --prefix=/apps/nginx --user=nginx --group=nginx 
 8 --with-http_ssl_module -- with-http_v2_module --with-http_realip_module 
 9 --with-http_stub_status_module --with- http_gzip_static_module --with-pcre 
10 --with-stream --with-stream_ssl_module --with- stream_realip_module 
11 --with-select_module --with-file-aio --add- module=/usr/local/src/echo-nginx-module 
12 --with-openssl=/usr/local/src/openssl-1.1.1d # make && make install 
13 验证并启动Nginx: 
14 # /apps/nginx/sbin/nginx -t 
15 nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok 
16 nginx: configuration file /apps/nginx/conf/nginx.conf test is successful 
17 # /apps/nginx/sbin/nginx
原文地址:https://www.cnblogs.com/sqbk/p/14178757.html