Nginx第三方模块和变量
1.1 Nginx第三方模块:echo
要安装第三方模块需要对Nginx重新编译
echo模块 https://github.com/openresty/echo-nginx-module
我们先看看未安装模块时的提示是怎么样的
#
location /echo {
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@localhost@conf]->nginx -t
nginx: [emerg] unknown directive "echo" in /apps/nginx/conf/conf.d/pc.conf:55
nginx: configuration file /apps/nginx/conf/nginx.conf test failed
知道报错原因,开始安装echo模块
[root@localhost@conf]->yum install git -y
# 要在github上下载模块先安装个git是吧
#
[root@localhost@conf]->git clone https://github.com/openresty/echo-nginx-module
#
# 下载好后准备重新编译
#
# 先看看原先输入的编译选项
[root@localhost@conf]->nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
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
#
# 找到解压文件路径
[root@localhost@/]->find / -name 'nginx-1.16*'
/usr/local/src/nginx-1.16.1.tar.gz
/usr/local/src/nginx-1.16.1
#
[root@localhost@/]->cd /usr/local/src/nginx-1.16.1/
[root@localhost@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 \
> --add-module=/apps/nginx/mods/echo-nginx-module
# 这里我把这个模块拷贝到了/apps/nginx/mods/
#
[root@localhost@nginx-1.16.1]->make&&make install
#
# 检查语法
[root@localhost@conf]->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
# 通过
#
# 需要重启nginx模块
[root@localhost@conf]->systemctl restart nginx
#
# 测试
当你成功安装好该模块后,我们就可以愉快♂的来学习变量了
1.2 Nginx 变量使用
Nginx中的变量分为内置变量和自定义变量,内置变量是Nginx自带的,通过变量我们可以知道许多有意思的值。
$remote_addr
# 客户端地址,公网的IP
#
$args
# URL地址
#
$document_root
# 当前会话请求的系统目录
#
$document_uri
# 不含指令的URI
#
$host
# 请求的host名称
#
$http_user_agent
# 客户端浏览器详情
#
$http_cookie
# 客户端cookie信息
#
$limit_rate 10240
# 如果设置了limit_rate则会显示网络速率,否则显示0
#
$remote_port
# 客户端发出请求的端口
#
$remote_user
# 经过Auth Basic Module验证过的用户名
#
$request_body_file
# 做反向代理时候发给后端服务器的本地资源名称
#
$request_filename
# 当前请求的资源文件路径名称,实际上是由root和alias生成过后的一个绝对路径
#
$request_method
# 请求资源的方法:GET/PUT/DELETE等
#
$request_uri
# 包含请求参数的原始URI,不包含主机名
#
$scheme
# 请求的协议
#
$server_protocol
# 协议版本
#
$server_addr
# 服务器IP地址
#
$server_name
# 服务器主机名
#
$server_port
# 请求的服务器端口
1.3 自定义变量
方法很简单
set $variable value;
echo $variable
# 一般多使用在server,location,if块中
了解这么多干啥用?
你懂的