安装echo模块调试Nginx变量
问题来源
(1)看到nginx.conf里面的变量头都大了,因为我完全不知道那个变量表示的到底是什么,一脸懵逼。。。
(2)有的时候我们需要根据不同的 if 条件自定义变量,并对这些自定义的变量进行调试
解决方法
令人感动到哭的解决方法就是 echo-nginx-module 模块来调试变量。然而这个模块默认情况下并不存在于nginx的正式发行包中,因此我们需要在编译安装nginx的时候手动添加
echo-nginx-module模块的github地址是:
https://github.com/openresty/echo-nginx-module
安装方法如下:
$ wget 'http://nginx.org/download/nginx-1.11.2.tar.gz'
$ tar -xzvf nginx-1.11.2.tar.gz
$ cd nginx-1.11.2/
# Here we assume you would install you nginx under /opt/nginx/.
$ ./configure --prefix=/opt/nginx \
--add-module=/path/to/echo-nginx-module
$ make -j2
$ make install
调试案例
为了输出nginx中的各种变量,我通过在/etc/hosts文件中添加了如下记录:
server{
listen 80;
server_name www.test.com;
location / {
echo 'query_string: $query_string';
echo 'request_method: $request_method';
echo 'content_type: $content_type';
echo 'content_length: $content_length';
echo 'fastcgi_script_name: $fastcgi_script_name';
echo 'request_uri: $request_uri';
echo 'document_uri: $document_uri';
echo 'document_root: $document_root';
echo 'server_protocol: $server_protocol';
echo 'https: $https';
echo 'nginx_version: $nginx_version';
echo 'remote_addr: $remote_addr';
echo 'remote_port: $remote_port';
echo 'server_addr: $server_addr';
echo 'server_port: $server_port';
echo 'server_name: $server_name';
echo 'uri: $uri';
}
}
然后使用curl命令向www.test.com进行请求:
curl -L www.test.com/index.php?from=siguoya --header "content-type:text/html;" -H "content-length:200"
即达到了调试各种变量的效果:
query_string: from=siguoya
request_method: GET
content_type: text/html;
content_length: 200
fastcgi_script_name: /index.php
request_uri: /index.php?from=siguoya
document_uri: /index.php
document_root: /usr/local/nginx/1.10.1/html
server_protocol: HTTP/1.1
https:
nginx_version: 1.10.1
remote_addr: 127.0.0.1
remote_port: 61376
server_addr: 127.0.0.1
server_port: 80
server_name: www.test.com
uri: /index.php