binary_remote_addr
hostname
uri
query_string
$realpath_root
proxy_host
proxy_protocol_addr
upstream_cache_status
upstream_http_
upstream_response_time
$upstream_status
测试环境:
OS:CentOS6.4x64
hostname:test1.lxm.com
ip:10.0.10.11
function:nginx前端代理,主要用来测试nginx的各变量
OS:CentOS6.4x64
hostname:test2.lxm.com
ip:10.0.10.12
function:LNMP后端服务器,用来提供一个简单的测试页面而已(主要是为了测试前端nginx反向代理到后端的变量值)
为了展示测试的效果,此次测试需要安装一个nginx的第三方模块:echo-nginx-module-0.55.tar.gz. 借助于该模块中的一个指令:echo 可以将个变量的值输出到终端上,给各位一个只管的感受。避免盲目的文字介绍,搞的头晕脑胀,也省的自己记错了。。哈哈。。。
安装echo-nginx-module-0.55.tar.gz:
在我的测试环境中,已经安装好了nginx,此时要再安装第三方模块,则要使用下面的方法:
#cd /root/soft
#tar -zxvf nginx-1.6.1.tar.gz
#cd nginx-1.6.1
#./configure --prefix=/usr/local/nginx/ --with-pcre --with-http_ssl_module --with-openssl=/root/soft/openssl-1.0.1i --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client_body_temp/ --http-proxy-temp-path=/usr/local/nginx/proxy_temp/ --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp/ --http-scgi-temp-path=/usr/local/nginx/scgi_temp/ --add-module=../echo-nginx-module-0.55
#make
#/usr/local/nginx/sbin/nginx -s stop
#cp -p objs/nginx /usr/local/nginx/sbin/nginx //覆盖掉原先的nginx执行文件
说明:
1.--add-module
该选项是nginx的一个编译参数,用来添加额外的第三方模块。因此可知,对任何第三方模块都是使用该方法来添加进nginx中
2.以上的安装可见没有make install这一步,这是因为我已经安装了nginx,我只需要更新我的可执行程序,而其他的文件我不需要更新,这也是nginx升级的原理。如果没有安装过nginx,则需
要make install这一步
附:ngx_echo 模块的下载地址
测试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";
}
由上面的信息可知,这里我使用了一个/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 c.lxm.com/test
foo: hello world //成功的输出了信息
[root@test1 conf]#
到此,可以确定第三方模块添加成功,echo指令也可以使用了。。。。
测试变量的含义:
第一组:
修改配置文件nginx.conf,将test接口改为如下配置:
location /test {
echo "args: arg_name";
echo "sex : is_args";
}
访问测试:
[root@test1 conf]# curl 'c.lxm.com:80/test?name=l…' //注意:这里的测试url要加上引号,表示加上参数一起是一个完成的url
args: name=lxm&sex=man
name: lxm
sex : man
is_args: ?
[root@test1 conf]#
由上面的测试信息,来解释下面几个变量:
arg_PARAMETER :这是参数的一个匹配模式,PARAMETER为具体的参数名,arg_name就是获取url中name的值
$is_args :判断url是否带参数,如果带,则返回一个?,否则返回一个空字符串
第二组:
sent_http_HEADER //使用该方式来获取http响应头部中的值,HEADER是一个匹配模式,匹配响应头部中key:value中的可以,返回的是value
由于这个涉及到http请求流的问题,这里采用日志的形式展示效果,如果用echo来返回,有点东西测试不出来。。
新建一日志格式:
log_format test '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 c.lxm.com/test/index.…
welcome to my variables test
[root@test1 conf]#curl -I //获取的是响应头中的信息 HTTP/1.1 200 OK Server: nginx Date: Mon, 01 Sep 2014 06:56:18 GMT Content-Type: text/html Content-Length: 38 Last-Modified: Mon, 01 Sep 2014 06:43:54 GMT Connection: keep-alive ETag: "540415aa-26" X-Cache-TEST: - Accept-Ranges: bytes [root@test1 logs]# tail -f access.log Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0 text/html 38 curl/7.19.7 (x86\_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 text/html 38 由上面的访问测试和日志显示,变量成功获取到了值。。 $http\_user\_agent: //获取的是客户端访问代理的类型,请求头中的信息 $sent\_http\_content\_type : //获取的是http响应头中content\_type的值 $sent\_http\_content\_length: //获取的是http响应头重的content\_length的值注意:在变量中的字符串一律是小写,虽然在请求头和响应头中的字符串可能有大写,但是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: request_uri";
echo "request_method: request_filename";
echo "content_length: content_type";
echo "document_root: document_uri";
echo "server_addr: server_name";
echo "server_port: server_protocol";
echo "host: hostname";
echo "remote_addr: remote_user";
echo "remote_port: $remote_port";
}
}
#service nginx configtest
#service nginx restart
[root@test1 conf]# curl 'c.lxm.com:80/test/index.…'
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_method //该表示获取的是http请求的方法
uri //获取的是当前请求的uri,不包括参数
content_type //获取的是http请求头中的Content-Type字段,不过这里也没显示。。。
document_uri //当前请求的uri,从上面的信息来看,和uri的效果是一样的
remote_port //获取客户端的访问端口,这个端口是随机的
server_protocol //表示服务器端想客户端发送响应的协议
server_name //客户端访问服务端的域名,即url中的域名
binary_remote_addr //显示二进制的客户端地址
hostname //表示服务器端的主机名
第四组:
对于这一组使用日志的形式来展示。。
修改配置文件nginx.conf:
log_format testproxy 'proxy_host proxy_protocol_addr upstream_cache_status 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 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;
}
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
开源项目:docs.qq.com/doc/DSlVlZExWQ0FRSE9H