【四月更文打卡】Nginx 高级配置

122 阅读3分钟

Nginx 高级配置

一.状态页

Nginx 状态页类似于 apache 和 php 使用的状态页面,基于ngx_http_auth_basic_module 实现,在编译安装 nginx 的时候需要添加编译参数--with-http_stub_status_module, 否则配置完成之后监测会是提⽰语法错误。

server {
        server_name blog.suosuoli.cn;
        keepalive_requests 5;
        keepalive_timeout 65 66;

        location /status {
                stub_status;
                allow 172.20.1.1;
                allow 127.0.0.1;
                deny all;
        }

说明

# 对齐一下是下面的样子
Active connections: 2
server accepts handled requests
       404     404     458
Reading: 0 Writing: 1 Waiting: 1

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

二.第三方模块使用

Nginx 支持扩展第三方模块,第三⽅模块需要在编译安装 Nginx 的时候使⽤ 参数--add-module=PATH指定路径添加,PATH是第三方模块的源码路径。 有的模块是由公司的开发⼈员针对业务需求定制开发的,有的模块是开源爱好者 开发好之后上传到 github 进⾏开源的模块,nginx ⽀持第三⽅模块需要从源码 重新编译⽀持,⽐如开源的 echo 模块。

[root@node1 data]# cd /usr/local/src
[root@node1 src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 3015 (delta 8), reused 11 (delta 5), pack-reused 2997
Receiving objects: 100% (3015/3015), 1.15 MiB | 16.00 KiB/s, done.
Resolving deltas: 100% (1619/1619), done.

[root@node1 src]# vim /apps/nginx/conf/nginx.conf
server {
     server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /status {
        stub_status;
        allow 172.20.1.1;
        allow 127.0.0.1;
        deny all;
        }

        location /echo1 {
                echo _sleep 1;
                echo This is echo1!!;
        }

        location /echo2 {
                echo _sleep 1;
                echo This is echo2!!;
        }
}

[root@node1 src]# nginx -s stop
# 五echo模块,报错,无法关闭nginx
nginx: [emerg] unknown directive "echo" in /etc/nginx/conf.d/blog.conf:31

# 注释掉echo配置
[root@node1 src]# nginx -s stop

[root@node1 src]# ll echo-nginx-module/
total 104
drwxr-xr-x 6 root root  4096 Jan  5 16:01 ./
drwxr-xr-x 3 root root  4096 Jan  5 16:00 ../
-rw-r--r-- 1 root root  3184 Jan  5 16:01 config
drwxr-xr-x 8 root root  4096 Jan  5 16:01 .git/
-rw-r--r-- 1 root root    27 Jan  5 16:01 .gitattributes
-rw-r--r-- 1 root root   618 Jan  5 16:01 .gitignore
-rw-r--r-- 1 root root  1345 Jan  5 16:01 LICENSE
-rw-r--r-- 1 root root 54503 Jan  5 16:01 README.markdown
drwxr-xr-x 2 root root  4096 Jan  5 16:01 src/
drwxr-xr-x 2 root root  4096 Jan  5 16:01 t/
-rw-r--r-- 1 root root  2216 Jan  5 16:01 .travis.yml
drwxr-xr-x 2 root root  4096 Jan  5 16:01 util/
-rw-r--r-- 1 root root   986 Jan  5 16:01 valgrind.suppress

# 编译安装
[root@node1 src]# cd nginx-1.16.1
[root@node1 nginx-1.16.1]# cd nginx-1.16.1
./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-http_perl_module \
--add-module=/usr/local/src/echo-nginx-module  # 指定第三方模块的源码路径