最全Nginx系列教程之四:Nginx常用变量汇总及测试(1),软件测试自学

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

1.–add-module
 该选项是nginx的一个编译参数,用来添加额外的第三方模块。因此可知,对任何第三方模块都是使用该方法来添加进nginx中
2.以上的安装可见没有make install这一步,这是因为我已经安装了nginx,我只需要更新我的可执行程序,而其他的文件我不需要更新,这也是nginx升级的原理。如果没有安装过nginx,则需
要make install这一步

附:ngx_echo 模块的下载地址

https://github.com/openresty/echo-nginx-module/releases

测试echo指令是否可用:
#vim /usr/local/nginx/conf/nginx.conf
修改配置文件,添加一个server段做测试,配置如下:
 server {
        listen 80;
        server_name c.lxm.com;
        access_log /data/logs/access.log main;
        location / {
                root /data;
                index index.html index.htm;
        }
        location /test {
            set $foo “hello world”;
            echo “foo: $foo”;
        }
 由上面的信息可知,这里我使用了一个/test接口来测试echo指令是否可用。

注:有的人可能会问,什么叫接口啊?说白了接口就是一个访问路径,访问入口而已,通过这个入口可以进行交互的作用。。其次这里的set是设置变量的指令,更多信息请看官方文档,这个不是该文档要讨论的。。。

[root@test1 conf]#echo “10.0.10.11  c.lxm.com” >> /etc/hosts
[root@test1 conf]# service nginx configtest
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test1 conf]# service nginx restart
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@test1 conf]# curl http://c.lxm.com/test
foo: hello world    //成功的输出了信息
[root@test1 conf]#
  
到此,可以确定第三方模块添加成功,echo指令也可以使用了。。。。

测试变量的含义:
第一组:
修改配置文件nginx.conf,将test接口改为如下配置:
  location /test {
            echo “args: $args”;
            echo “name: $arg_name”;
            echo “sex : $arg_sex”;
            echo “is_args: $is_args”;
        }
  
访问测试:
[root@test1 conf]# curl ‘http://c.lxm.com:80/test?name=lxm&sex=man’    //注意:这里的测试url要加上引号,表示加上参数一起是一个完成的url
args: name=lxm&sex=man
name: lxm
sex : man
is_args: ?
[root@test1 conf]#

由上面的测试信息,来解释下面几个变量:
$args :该变量用来获取url后面的所有参数 ,?表示参数的起始处
a r g _ P A R A M E T E R :这是参数的一个匹配模式 , P A R A M E T E R 为具体的参数名 , arg\_PARAMETER :这是参数的一个匹配模式,PARAMETER为具体的参数名, arg_PARAMETER:这是参数的一个匹配模式,PARAMETER为具体的参数名,arg_PARAMETER就表示获取具体的参数值,例如上面的$arg_name就是获取url中name的值
$is_args  :判断url是否带参数,如果带,则返回一个?,否则返回一个空字符串

第二组:
$http_HEADER    //使用该方式来获取http请求头部中的值,HEADER是一个匹配模式,匹配请求头部中key:value中的可以,返回的是value
$sent_http_HEADER  //使用该方式来获取http响应头部中的值,HEADER是一个匹配模式,匹配响应头部中key:value中的可以,返回的是value

由于这个涉及到http请求流的问题,这里采用日志的形式展示效果,如果用echo来返回,有点东西测试不出来。。

新建一日志格式:
 log_format  test  ‘$http_user_agent $sent_http_content_type $sent_http_content_length’;

修改test接口的server部分:
   server {
        listen 80;
        server_name c.lxm.com;
        access_log /data/logs/access.log test;
        location / {
                root html;
                index index.html index.htm;

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

}
 
#service nginx configtest
#service nginx restart

[root@test1 conf]# curl http://c.lxm.com/test/index.html

welcome to my variables test

[root@test1 conf]#curl -I

注意:在变量中的字符串一律是小写,虽然在请求头和响应头中的字符串可能有大写,但是nginx会一律将其转换为小写之后进行匹配。。。

此外,要想知道http请求头和响应头中都有些什么内容,建议使用火狐浏览器的F12调试功能中的网络功能,来获取请求头和响应头的信息。。。。

还要扒拉一句的是:虽然这两个变量可以获取头部中的信息值,但是经过我的测试,并不是对所有的都有效,起码在响应头中的date和server,本人没测试成功,总是返回空值。或许你该注意。。

第三组:
配置文件如下:
 server {
        listen 80;
        server_name c.lxm.com;
        access_log /data/logs/access.log test;
        location / {
                root html;
                index index.html index.htm;

}
        location /test {
        proxy_pass http://backend;
        proxy_redirect off;
        echo “uri: $uri”;
        echo “request_uri: $request_uri”;
        echo “request_method: $request_method”;
        echo “request_filename: $request_filename”;
        echo “content_length: $content_length”;
        echo “content_type: $content_type”;
        echo “document_root: $document_root”;
        echo “document_uri: $document_uri”;
        echo “server_addr: $server_addr”;
        echo “server_name: $server_name”;
        echo “server_port: $server_port”;
        echo “server_protocol: $server_protocol”;
        echo “host: $host”;
        echo “hostname: $hostname”;
        echo “remote_addr: $remote_addr”;
        echo “remote_user: $remote_user”;
        echo “remote_port: $remote_port”;
  }
 }
 
#service nginx configtest
#service nginx restart

[root@test1 conf]# curl ‘http://c.lxm.com:80/test/index.html?name=lxm&sex=man’
uri: /test/index.html
request_uri: /test/index.html?name=lxm&sex=man
request_method: GET
request_filename: /usr/local/nginx//html/test/index.html
content_length:
content_type:
document_root: /usr/local/nginx//html
document_uri: /test/index.html
server_addr: 10.0.10.11
server_name: c.lxm.com
server_port: 80
server_protocol: HTTP/1.1
host: c.lxm.com
hostname: test1.lxm.com
remote_addr: 10.0.10.11
remote_user:
remote_port: 57292

根据上面的输出信息,来解释一下下面的变量:
$request_filename  //该变量获取的是请求的文件在linux服务器上的完整的绝对路径
$request_method  //该表示获取的是http请求的方法
$request_uri  //该变量表示的原始请求的uri,包括参数。所谓原始请求就是即使在内部做了重定向之后也不会变化
$uri  //获取的是当前请求的uri,不包括参数
$content_length  //获取的是http请求头中Content-Length的值,不过这里似乎没显示,抱歉这里的测试页面中没有该字段
$content_type   //获取的是http请求头中的Content-Type字段,不过这里也没显示。。。
$document_root   //获取的是请求url的文件所在的目录路径
$document_uri   //当前请求的uri,从上面的信息来看,和uri的效果是一样的
$remote_addr  //获取的是客户端的ip地址,这里为什么是10.0.10.11呢,因为我是在本机上用curl测试的,即使客户端也是服务器
$remote_port  //获取客户端的访问端口,这个端口是随机的
$remote_user  //获取客户端的认证用户信息,这里因为没有用认证,所谓显示为空
$server_protocol  //表示服务器端想客户端发送响应的协议
$server_addr  //服务器的地址
$server_name  //客户端访问服务端的域名,即url中的域名
$server_port //服务器端做出响应的端口号
$binary_remote_addr  //显示二进制的客户端地址
$host  //和server_name一样,表示的是域名
$hostname  //表示服务器端的主机名

第四组:
 对于这一组使用日志的形式来展示。。
 
修改配置文件nginx.conf:
log_format   testproxy  ‘$proxy_add_x_forwarded_for $proxy_host $proxy_port $proxy_protocol_addr $upstream_addr $upstream_cache_status $upstream_response_length $upstream_response_time $upstream_status’;

server {
       listen      80;
        server_name  a.lxm.com b.lxm.com c.liwei.com;

#charset koi8-r;

access_log  logs/host.access.log  testproxy;

#location / {
        #    root   html;
        #    index  index.html index.htm;
        #    auth_basic “relam authentication”;
        #    auth_basic_user_file /usr/local/nginx/htpasswd;
        #}
        location / {
                root html;
                proxy_redirect off;
                proxy_pass   http://backend;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
                proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
                proxy_cache cache_one;
                proxy_cache_valid 200 302 1h;
                proxy_cache_valid 301 1d;
                proxy_cache_valid any 1m;
                expires 30d;
        }

}

注意:上面的内容是nginx配置文件的一个片段,只是跟我们的测试内容相关而已
  
#service nginx configtest
#service nginx restart

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

外链图片转存中…(img-2YtoDdDk-1715385407683)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值