nginx

Syntax:	secure_link expression;
Default:	—
Context:	http, server, location

Syntax:	secure_link_md5 expression;
Default:	—
Context:	http, server, location
Syntax:	rewrite regex replacement [flag];
Default:	—
Context:	server, location, if
. 匹配除换行符以外的任意字符0
重复0次或1次
+ 重复1次或更多次
* 最少链接数,哪个机器连接数少就分发
\d 匹配数字
^ 匹配字符串的开始
$ 匹配字符串的结尾
{n} 重复n次
{n,} 重复n次或更多次
[c] 匹配单个字符c
[a-z] 匹配任意一个小写字母
\ 转义字符
( ) 用于匹配括号之间的内容,通过$1、$2调用

rewrite index\.php$ /pages/maintain.html break;

if ($http_user_agent ~ MSIE){
    rewrite ^(.*)$ /mise/$1 break;
}
last 停止rewrite检测
break 停止rewrite检测(与last有很大区别)
redirect 返回302临时重定向,地址栏显示跳转后的地址
permanent 返回301永久重定向,地址栏显示跳转后的地址
server {
    listen 80 default_server;
    server_name blogs.uscwifi.cn;

    access_log  /var/log/nginx/host.access.log  main;
     
    root /opt/app/code3; 
    location ~ ^/break {
        rewrite ^/break /test/ break;
    } 
 
    location ~ ^/last {
         rewrite ^/last /test/ last;
    }    
 
    location /test/ {
       default_type application/json;
       return 200 '{"status":"success"}';
    }
}
location /images/ {
        root "/app/webroot"
}

访问: http://www.test.com/images/a.jpg  相当于文件系统路径 /app/webroot/images/a.jpg 

location  /images/ {
        alias  "/www/pictures/"; 
}
访问: http://www.test.com/images/a.jpg  相当于文件系统路径/www/pictures/a.jpg

编译安装nginx

[root@node1 ~]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
[root@node1 ~]# wget https://mirrors.huaweicloud.com/nginx/nginx-1.17.5.tar.gz
[root@node1 ~]# tar xf nginx-1.17.5.tar.gz
[root@node1 ~]# cd nginx-1.17.5/
[root@node1 nginx-1.17.5]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-debug
[root@node1 nginx-1.17.5]# make -j 4 
[root@node1 nginx-1.17.5]# make install
[root@node1 nginx-1.17.5]# /usr/sbin/groupadd -g 995 -r nginx 2> /dev/null
[root@node1 nginx-1.17.5]# /usr/sbin/useradd -c "Nginx web server" -u 997 -g nginx -s /sbin/nologin -r -d /var/lib/nginx nginx 2> /dev/null
[root@node1 nginx-1.17.5]# cat > /usr/lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
[root@node1 nginx-1.17.5]# mkdir /apps/nginx/conf.d 
[root@node1 ~]# mkdir /var/log/nginx
[root@node1 nginx-1.17.5]# cat > /apps/nginx/conf/nginx.conf <<'EOF'
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}
http {
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /apps/nginx/conf/mime.types;
    default_type        application/octet-stream;
    include /apps/nginx/conf.d/*.conf;
    server {
        server_name  _;
        root         /apps/nginx/html;
        location / {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
EOF
[root@node1 nginx-1.17.5]# systemctl daemon-reload 
[root@node1 nginx-1.17.5]# systemctl start nginx
[root@node1 nginx-1.17.5]# curl 127.0.0.1 -I
HTTP/1.1 200 OK

nginx配置选项

上图中各项配置内容介绍:
1. user                           启动 nginx worker 进程的系统用户身份
2. worker_processes               启动 worker 进程的个数
3. error_log                      错误日志的存放路径, 以及错误级别[debug | info | notice | warn | error | crit | alert | emerg]
4. pid                            进程启动后 pid 文件所在的目录
5. events {}                      事件驱动配置段
   worker_connections             单个 worker 进程最大并发连接数

6. http {}                        http 功能配置段
   include                        包含的自配置文件
   log_format                     日志组成格式和内容, 可以给日志格式起一个名称 比如默认叫 main
   access_log                     访问日志存储路径和具体的日志格式,比如使用了上一行 log_format 定义的 main
   send_file                      是否开启 sendfile 功能, 默认是 on
   keepalive_timeout              默认的会话连接超时时间,单位为秒
   gzip                           是否开启 gzip 压缩发送的数据
   server{}                       http 内配置的虚拟主机
        listen                    监听的 ip 地址和端口号
        server_name               虚拟主机名
        access_log                访问指定 server 的日志
        location {}               客户端请求 url 配置条件
                  root            location 中指定的 文件存放系统路径
                  index           默认的 index 页面 后跟 index.html index.php
                  error_page      错误页面
http 代表 nginx 处理 http 请求时的相关配置选项段
7.1. include mime.types; 代表加载 conf 路径下 mime.types 文件
7.2. default_type application/octet-stream; 代表提示下载 mime.types 中不支持的文件类型
7.3. log_format 代表日志格式, 可以自定义一个名称,格式内容为自定义字符和 nginx 内置变量信息的组合
7.4. access_log 代表 http 的访问日志存放在文件系统中的路径,路径后要指明日志格式
7.5. sendfile 代表是否要开启 sendfile 功能,该功能可以减少拷贝内核数据
到用户空间的过程,提高响应数据速度,减少内存开销。
7.6. keepalive_timeout 网络会后连接时长,默认 75 秒,设置为 0 时表示关闭
7.7. gzip on 代表是否开启数据传输压缩。
7.8. server{} 代表 http 虚拟主机配置段
	7.8.1. listen 代表该虚拟主机监听的 主机地址和端口号,比如: listen 192.168.130.132:80;
	7.8.2. server_name 代表主机名,比如:www.magedu.com
7.9. location{} 代表访问路径匹配规则配置段
	7.9.1 root 代表改匹配规则下对应文件在文件系统中的存放路径 
	7.9.2 index 代表默认主页文件,后面根据顺序可以写 index.html index.php 等

nginx进程

#主进程root发起,子进程为nginx
#一个主进程,一个子进程
[root@node1 ~]# ps -ef | grep nginx
root      10473      1  0 20:01 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx     10474  10473  0 20:01 ?        00:00:00 nginx: worker process
root      10478   7037  0 20:07 pts/1    00:00:00 grep --color=auto nginx
#配置文件中
worker_processes auto;
#因此nginx会根据cpu核心的个数来配置
#当前机器CPU核心数为1
[root@node1 ~]# cat /proc/cpuinfo |grep "cpu cores"|uniq|wc -l
1
#当前机器CPU个数
[root@node1 ~]# cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l
1

CPU亲和性

#让nginx进程绑定在指定cpu核心上,减少cpu切换带来的性能问题
#指定nginx两个进程,分别绑定到cpu0和cpu1
worker_processes 2;
worker_cpu_affinity 0010 0001;
#查看CPU运行情况
[root@localhost ~]# ps axo pid,cmd,psr | grep nginx
  7273 nginx: master process /apps   1
  7274 nginx: worker process         1
  7275 nginx: worker process         0
  7277 grep --color=auto nginx       1
#ps -axo pid,cmd,psr  #-a显示所有进程,-x显示没有终端的进程,-o用户自定义显示:pid为进程号,cmd是进程执行的命令,psr是进程当前分配的cpu核心

错误日志:

error_log  logs/error.log  error;
#不写日志记录级别默认为error

nginx实现访问控制

#ngx_http_access_module 
[root@localhost nginx]# cat  /apps/nginx/conf/conf.d/pc.conf
server {
	listen    		80;
	server_name		www.jd.net;
 	location / {
		root 	/data/nginx/html/pc;
		index 	index.html;

		deny 	192.168.130.133;
		allow 	192.168.130.132;
		allow	127.0.0.1;
		deny	all;
	}
}

nginx 中配置用户名密码验证访问控制

#httpd-tools工具
htpasswd
	-c  Create a new file.
	-b  Use the password from the command line rather than prompting for it.
	-m  Force MD5 encryption of the password (default).
	-D  Delete the specified user.
[root@node1 ~]# htpasswd -cm /apps/nginx/conf.d/.pass zhangsan
New password: 
Re-type new password: 
Adding password for user zhangsan
[root@node1 ~]# cat /apps/nginx/conf.d/.pass 
zhangsan:$apr1$Dg9hncK.$qSyfddesBxVXXkqA.6L6J1
[root@node1 ~]# mkdir /var/www/html -p
[root@node1 ~]# echo hello > /var/www/html/index.html
[root@node1 ~]# cat /apps/nginx/conf.d/test.conf 
server{
    listen 80 ;
    server_name jd.com;
    root /var/www/html;
    index index.html;
    location / {
        auth_basic	"passwd:";
        auth_basic_user_file /apps/nginx/conf.d/.pass;
    }

}
[root@node1 ~]# curl -H "jd.com" 127.0.0.1 -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.17.5
Date: Sat, 26 Oct 2019 12:34:30 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
WWW-Authenticate: Basic realm="passwd:"

[root@node1 ~]# curl -H "jd.com"  -u zhangsan:zhangsan 127.0.0.1 -i
HTTP/1.1 200 OK
Server: nginx/1.17.5
Date: Sat, 26 Oct 2019 12:34:35 GMT
Content-Type: text/html
Content-Length: 6
Last-Modified: Sat, 26 Oct 2019 12:29:27 GMT
Connection: keep-alive
ETag: "5db43c27-6"
Accept-Ranges: bytes

hello

配置错误页面

[root@node1 ~]# echo error...no page... > /apps/nginx/html/error.html
[root@node1 ~]# cat  /apps/nginx/conf.d/test.conf 
server{
    listen 80 ;
    server_name jd.com;
    root /var/www/html;
    index index.html;
    error_page		500 502 503 504 404 /error.html;
    location / {
        auth_basic	"passwd:";
        auth_basic_user_file /apps/nginx/conf.d/.pass;
    }
    location = /error.html {
        root /apps/nginx/html;
    }
}
[root@node1 ~]# /apps/nginx/sbin/nginx -s reload
[root@node1 ~]# curl -H "jd.com"  -u zhangsan:zhangsan 127.0.0.1/dasdas 
error...no page...

try_files:当匹配 location 后尝试安装指定的查询文件顺序去指定路径下查找响应文件


配置 nginx 为文件下载服务器

用于配置 nginx 为文件下载服务器的选项目前有4个:
1. autoindex		        on; 开启文件下载功能
2. autoindex_exact_size 	on; 显示详细的文件大小
3. autoindex_format             html; 返回页面的格式,一般使用 html 格式
4. autoindex_localtime          on; 显示服务器时间为当地时间

nginx上传文件

1. client_max_body_size             10m; 上传文件的大小限制,为 0 时,不做限制。
2. client_body_buffer_size          16k; nginx 读取客户端 request 的 body 缓冲空间大小,64位操作系统建议设置为 16k
3. client_body_temp_path            /apps/nginx/temp 1 2 2; 指定 nginx 读取 request 的 body 数据时临时文件存放路径,以及使用 1 2 2 级目录存放这些临时文件

nginx 打开文件缓存功能

1. open_file_cache on;              是否启用缓存功能
   open_file_cache max=100 inactive=20s 最大缓存文件个数和文件缓存过期时间
2. open_file_cache_errors  off;     是否缓存查找错误的文件,默认关闭
3. open_file_cache_min_uses 1;      在缓存过期时间内,文件被访问几次才不会被定义为不活跃文件
4. open_file_cache_valid  60;       检查过期文件的判断周期,每 60 秒检查一遍

隐藏 nginx 服务器版本信息

server_tokens off;  显示或者隐藏服务器信息,比如在错误页面或者头文件中

限制客户端请求方法

开启 nginx 的状态显示功能

#需要http_stub_status_module
[root@node1 html]# /apps/nginx/sbin/nginx -V |& grep status
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-debug
[root@node1 html]# cat /apps/nginx/conf.d/status.conf 
server{
    listen 2333;
    server_name _;
    location / {
        stub_status;
        allow 	127.0.0.1;
        deny all;
    }
}
[root@node1 html]# /apps/nginx/sbin/nginx -s reload
[root@node1 html]# curl 127.0.0.1:2333
Active connections: 1 
server accepts handled requests
 31 31 39 
Reading: 0 Writing: 1 Waiting: 0 
[root@node1 html]# curl 192.168.38.138:2333
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.17.5</center>
</body>
</html>
Active connections:    表示当前处于活动状态的客户端连接数,包括连接等待空闲连接数
accepts:               表示Nginx自启动后已经接受的客户端请求的总数
handled:               表示Nginx自启动后已经处理完成的客户端请求的总数,通常等于accepts,
                        除非有因worker_connections限制等被拒绝的连接
requests:              表示Nginx自启动后客户端发来的总的请求数
Reading:               当前状态,正在读取客户端请求报文首部的连接的连接数。
Writing:               当前状态,正在向客户端发送响应报文过程中的连接数。
Waiting:               当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于
                        active – (reading + writing),

nginx 使用第三方模块echo

#echo模块的用法:https://github.com/openresty/echo-nginx-module
[root@node1 ~]# systemctl stop nginx
[root@node1 ~]# rm -rf nginx-1.17.5
[root@node1 ~]# tar xf nginx-1.17.5.tar.gz 
[root@node1 ~]# yum install git -y -q
[root@node1 ~]# git clone https://github.com/openresty/echo-nginx-module.git
[root@node1 ~]# cd nginx-1.17.5/
[root@node1 nginx-1.17.5]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-debug --add-module=/root/echo-nginx-module
[root@node1 nginx-1.17.5]# make -j 4 
[root@node1 nginx-1.17.5]# make install
[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx -V |& grep echo
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-debug --add-module=/root/echo-nginx-module

[root@node1 nginx-1.17.5]# cat /apps/nginx/conf.d/test.conf 
server{
    listen 80 ;
    server_name jd.com;
    root /var/www/html;
    index index.html;
    error_page		500 502 503 504 404 /error.html;
    access_log  /var/log/nginx/jd.com-access.log;
    error_log   /var/log/nginx/jd.com-error.log; 
    location = /error.html {
        root /apps/nginx/html;
    }
    location /main {
    	index index.html;
    	default_type text/html;
    	echo "hello world,main-->";
    	echo_reset_timer;
    	echo_location /sub1;
    	echo_location /sub2;
    	echo "took $echo_timer_elapsed sec for total.";
    }
    
    location /sub1 {
    	echo_sleep 1;
    	echo sub1;
    }
    
    location /sub2 {
    	echo_sleep 1;
    	echo sub2;
    }
}
[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx
[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx -s reload
[root@node1 nginx-1.17.5]# curl -H "jd.com"  127.0.0.1/main
hello world,main-->
sub1
sub2
took 2.003 sec for total.

nginx内置变量和自定义变量

1. 常用变量
$remote_addr            存放了客户端的地址,注意是客户端的公网IP,也就是一家人访问一个网站,则会显示为路由器的公网IP。
$args                   URL中的指令包含的参数,例如http://www.magedu.net/main/index.do?id=20190221&partner=search 
                        中的id=20190221&partner=search,动态服务器中经常会有这些信息
$document_root          保存了针对当前资源的请求的系统根目录,如/apps/nginx/html
$document_uri           保存了当前请求中不包含指令的URI,注意是不包含请求的指令,比如
                        http://www.magedu.net/main/index.do?id=20190221&partner=search 会被定义为 /main/index.do
$host                   存放了请求的 host 名称
$http_user_agent        客户端使用的浏览器的详细信息
$limit_rate             如果 nginx 服务器使用 limit_rate 配置了显示网络速率,则会显示,如果没有设置, 则显示 0
$remote_port            客户端请求 nginx 服务器时随机打开的端口,这是每个客户端自己的端口。
$request_body_file      做反向代理时发给后端服务器的本地资源的名称
$request_method         请求资源的方式,GET/PUT/DELETE等
$request_filename       当前请求的资源文件的路径名称,由root或alias指令与URI请求生成的文件绝对路径,
                        比如 /apps/nginx/html/main/index.html
$scheme                 请求的协议,如ftp,https,http等
$server_protocol        保存了客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr            保存了服务器的 IP 地址
$server_name            请求的服务器的主机名
$server_port            请求的服务器的端口号

# 打印内置变量和自定义变量
# 自定义变量写法:可以在配置文件的 server location if 中使用 set $varname value 进行设置
[root@node1 nginx-1.17.5]# cat /apps/nginx/conf.d/test.conf 
server{
    listen 80 ;
    server_name jd.com;
    root /var/www/html;
    index index.html;
    error_page		500 502 503 504 404 /error.html;
    access_log  /var/log/nginx/jd.com-access.log;
    error_log   /var/log/nginx/jd.com-error.log; 
    set $valid_time 30s;
    location = /error.html {
        root /apps/nginx/html;
    }
    location /main {
        index index.html;
        echo "来者何人:" $remote_addr;
        echo "访问者端口号:" $remote_port;
        echo "访问主机:" $host;
        echo "访问域名" $server_name;
        echo "访问端口:" $server_port;
        echo "自定义变量:" $valid_time;
    }
}

[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx -s reload
[root@node1 nginx-1.17.5]# curl -H "jd.com"  127.0.0.1/main
来者何人: 127.0.0.1
访问者端口号: 40982
访问主机: 127.0.0.1
访问域名 jd.com
访问端口: 80
自定义变量: 30s

自定义nginx访问日志格式

#可以去官方文档看看改
nginx.conf中
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
......
}

自定义日志格式为json格式,方便日后elk处理

nginx.conf中增加一个日志格式:
	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"}';

[root@node1 nginx-1.17.5]# cat /apps/nginx/conf.d/test.conf 
server{
    listen 80 ;
    server_name jd.com;
    root /var/www/html;
    index index.html;
    error_page		500 502 503 504 404 /error.html;
    access_log  /var/log/nginx/jd.com-access.log access_json;
    error_log   /var/log/nginx/jd.com-error.log; 
    set $valid_time 30s;
    location = /error.html {
        root /apps/nginx/html;
    }
    location /main {
        index index.html;
        echo "来者何人:" $remote_addr;
        echo "访问者端口号:" $remote_port;
        echo "访问主机:" $host;
        echo "访问域名" $server_name;
        echo "访问端口:" $server_port;
        echo "自定义变量:" $valid_time;
    }
}

[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@node1 nginx-1.17.5]# /apps/nginx/sbin/nginx -s reload
[root@node1 nginx-1.17.5]# curl -A "Chrome80"  -H "jd.com"  127.0.0.1/main
来者何人: 127.0.0.1
访问者端口号: 40984
访问主机: 127.0.0.1
访问域名 jd.com
访问端口: 80
自定义变量: 30s

[root@node1 ~]# tail -n1 /var/log/nginx/jd.com-access.log  
{"@timestamp":"2019-10-26T21:43:05+08:00","host":"127.0.0.1","clientip":"127.0.0.1","size":173,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"127.0.0.1","uri":"/main","domain":"127.0.0.1","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Chrome80","status":"200"}

python脚本处理日志

#保存日志文件到指定路径并进测试:
[root@node1 ~]# cat log-analysis.py
#!/usr/bin/env python
#coding:utf-8
status_200= []
status_404= []
f = open("access_json.1og")
for line in f.readlines():
    line = eval(line)
    if line.get("status") == "200":
        status_200.append(line.get)
    elif line.get("status") == "404":
        status_404.append(line.get)
    else:
        print("状态码ERROR")
f.close()
print "状态码200的有--:",len(status_200)
print "状态码200的有--:",len(status_404)
[root@node1 ~]# python log-analysis.py
状态码200的有--: 292
状态码200的有--: 176

nginx 开启传输文件压缩功能

gzip on | off;                      开启或者关闭 gzip 压缩功能
gzip_comp_level 2;                  指定压缩级别 1到9, 9的压缩率最高
gzip_disable "MSIE [1-6]\.";        指定特定浏览器不使用压缩功能
gzip_min_length 1k;                 启用压缩功能文件的阈值,即大于等于 1k 的文件才压缩
gzip_http_version 1.0 | 1.1;        支持压缩的 http 协议版本
gzip_buffers 32 16k;                指定 gzip 使用的缓冲空间个数和大小
gzip_types text/html;               指明可以使用压缩功能的文件类型(mime 类型)
gzip_vary on | off;                 压缩文件后是否在 http 头部添加信息 Vary: Accept-Encoding 

开启 https 功能

server {
    listen              443 ssl;
    server_name         www.example.com;
    ssl_certificate     www.example.com.crt;
    ssl_certificate_key www.example.com.key;
    ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    ssl_session_cache off;                          #配置 https 是否支持加密会话缓存功能,还有其他几个选项:
                                                none: 通知客户端支持ssl session cache,但实际不支持
                                                builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有
                                                [shared:name:size]:在各worker之间使用一个共享的缓存,需
                                                要定义一个缓存名称和缓存空间大小,一兆可以存储4000个会话信
                                                息,多个虚拟主机可以使用相同的缓存名称。
ssl_session_timeout time;                       客户端连接可以复用ssl session cache中缓存的有效时长,默认5m
    ...
}

配置 nginx 服务器 favicon.ico 小图标

[root@node1 ~]# wget -O /var/www/html/favicon.ico  https://www.baidu.com/favicon.ico

nginx状态监控

[root@imooc-nginx conf.d]# cat 2333.conf 
server {
    listen 127.0.0.1:2333;
    server_name _;
    location /status {
        stub_status on;
        access_log off;
    }
}
[root@imooc-nginx conf.d]# curl 127.0.0.1:2333/status
Active connections: 1 
server accepts handled requests
 209 209 921 
Reading: 0 Writing: 1 Waiting: 0 

[root@imooc-nginx conf.d]# cat 2333.conf 
server {
    listen 2333;
    server_name _;
    location /status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}
[root@imooc-nginx conf.d]# curl 127.0.0.1:2333/status
Active connections: 1 
server accepts handled requests
 211 211 923 
Reading: 0 Writing: 1 Waiting: 0 
[root@imooc-nginx conf.d]# curl 192.168.38.204:2333/status
curl: (7) Failed connect to 192.168.38.204:2333; 拒绝连接

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