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;
        }

访问http://blog.suosuoli.cn/status
在这里插入图片描述
说明

# 对齐一下是下面的样子
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 模块。
echo-github

[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  # 指定第三方模块的源码路径

[root@node1 nginx-1.16.1]# make -f 4 && make install

[root@node1 src]# /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.16.1]# vim /apps/nginx/conf/nginx.conf
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 /main {
   
                index index.html;
                default_type text/html;
                echo "Hello Nginx echo...";
                echo_reset_timer;
                echo_location /echo1;
                echo_location /echo2;
                echo "It took $echo_timer_elapsed secs to echo these words.";
        }


        location /echo1 {
   
                echo_sleep 1;
                echo This is echo1;
        }

        location /echo2 {
   
                echo_sleep 1;
                echo This is echo2;
        }
}

root@ubuntu-suosuoli-node1:/etc/nginx/conf.d# curl 172.20.2.37/echo1
This is echo1
root@ubuntu-suosuoli-node1:/etc/nginx/conf.d# curl 172.20.2.37/echo2
This is echo2

root@ubuntu-suosuoli-node1:/etc/nginx/conf.d# curl 172.20.2.37/main
Hello Nginx echo...
This is echo1
This is echo2
It took 2.002 secs to echo these words.`

访问http://172.20.2.37/main
在这里插入图片描述

三.Nginx 变量

nginx 的变量可以在配置⽂件中引⽤,作为功能判断或者⽇志等场景使⽤,变量可以分为
内置变量和⾃定义变量,内置变量是由 nginx 模块⾃带,通过变量可以获取到众多的与
客⼾端访问相关的值。

变量 含义
$remote_addr; 存放了客⼾端的地址,注意是客⼾端的公⽹ IP,也就是⼀家⼈访问⼀个⽹站,则会显⽰为路由器的公⽹ IP。
$args 变量中存放了 URL 中的指令,例如http://blog.suosuoli.cn/main/index.do?id=20190221&partner=search 中的 id=20190221&partner=search
$document_root 保存了针对当前资源的请求的系统根⽬录,如/apps/nginx/html。
$document_uri 保存了当前请求中不包含指令的 URI,注意是不包含请求的指令,⽐如http://blog.suosuoli.cn/main/index.do? id=20190221&partner=search 会被定义为/main/index.do
$host 存放了请求的 host 名称。
$http_user_agent 客⼾端浏览器的详细信息
$http_cookie 客⼾端的 cookie 信息。
limit_rate 10240; echo $limit_rate; 如果 nginx 服务器使⽤ limit_rate 配置了显⽰⽹络速率,则会显⽰,如果没有设置, 则显⽰ 0。
$remote_port 客⼾端请求 Nginx 服务器时随机打开的端⼝,这是每个客⼾端⾃⼰的端⼝。
$remote_user 已经经过 Auth Basic Module 验证的⽤⼾名。
$request_body_file 做反向代理时发给后端服务器的本地资源的名称。
$request_method 请求资源的⽅式,GET/PUT/DELETE 等
$request_filename 当前请求的资源⽂件的路径名称,由 root 或 alias 指令与 URI 请求⽣成的⽂件绝对路径, 如/apps/nginx/html/main/index.html
$request_uri 包含请求参数的原始 URI,不包含主机名,如:/main/index.do?id=20190221&partner=search。
$scheme 请求的协议,如 ftp,https,http 等。
$server_protocol 保存了客⼾端请求资源使⽤的协议的版本,如 HTTP/1.0,HTTP/1.1,HTTP/2.0 等。
$server_addr 保存了服务器的 IP 地址。
$server_name 请求的服务器的主机名。
$server_port 请求的服务器的端⼝号。

例子:

[root@node1 nginx-1.16.1]# vim /apps/nginx/conf/nginx.conf
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 /variables {
   
                index index.html;
                default_type text/html;

                echo "remote_addr is   : $remote_addr";
                echo "args in URL are  : $args";
                echo "document root is : $document_root";
                echo "document uri is  : $document_uri";
                echo "requested host is: $host";
                echo "user agent is    : $http_user_agent";
                echo "cookies in agent : $http_cookie";
                echo "the network speed: limit_rate";
                echo "user agent random port: $remote_port";
                echo "authed user is   : $remote_user";
                echo "to backend file  : $request_body_file";
                echo "request method   : $request_method";
                echo "requset file path: $request_filename"; # 如/apps/nginx/html/main/index.html
                echo "not include host : $request_uri";
                echo "protocol used    : $scheme";
                echo "spec protocol user agent used : $server_protocol";
                echo "server address   : $server_addr";
                echo "server hostname  : $server_name";
                echo "requested server port : $server_port";

        }
}

# 使用curl测试
root@ubuntu-suosuoli-node1:/etc/nginx/conf.d# curl 172.20.2.37/variables
remote_addr is   : 172.20.2.189
args in URL are  :
document root is : /apps/nginx/html
document uri is  : /variables
requested host is: 172.20.2.37
user agent is    : curl/7.58.0
cookies in agent :
the network speed: limit_rate
user agent random port: 60513
authed user is   :
to backend file  :
request method   : GET
requset file path: /apps/nginx/html/variables
not include host : /variables
protocol used    : http
spec protocol user agent used : HTTP/1.1
server address   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值