ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

十、nginx的变量说明

2020-09-11 13:32:08  阅读:144  来源: 互联网

标签:index 变量 request server 说明 nginx html echo


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

可以通过nginx的官网查看nginx内置的变量信息

 

 

 

 常见的nginx内置变量

$remote_addr;
  存放了客户端的地址,注意是客户端的公网IP,也就是一家人访问一个网站,则会显示为路由器的公网IP。

$args;
  #变量中存放了URL中的指令,例如http://www.ywx.com/seal/index.do?id=20080321&partner=search中的id=20080321&partner=search

$document_root;
  保存了针对当前资源的请求的系统根目录,如/usr/local/nginx/html。


$document_uri;
  保存了当前请求中不包含指令的URI,注意是不包含请求的指令,比如http://www.ywx.com/seal/index.do?id=20080321&partner=search会被定义为/seal/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请求生成的文件绝对路径,如/usr/local/nginx/html/seal/index.html

$request_uri;
  包含请求参数的原始URI,不包含主机名,如:index.do?id=20080321&partner=search 。

$scheme;
  请求的协议,如ftp,https,http等。

$server_protocol;
  保存了客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等。

$server_addr;
  保存了服务器的IP地址。

$server_name;
  请求的服务器的主机名。

$server_port;
  请求的服务器的端口号。

测试实例

在服务端打印nginx的内置变量给客户端

虚拟主机的配置

server {
    listen 80;
    server_name localhost;

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


    location /seal {
    index index.html;
    default_type text/html;
        echo "remote_addr = $remote_addr";
        echo ";";
        echo "args = $args";
        echo ";";
        echo "document_root = $document_root";
        echo ";";
        echo "document_uri = $document_uri";
        echo ";";
        echo "host = $host";
        echo ";";
        echo "http_user_agent = $http_user_agent";
        echo ";";
        echo "http_cookie = $http_cookie"
        echo ";";
        limit_rate 10240;
        echo "limit_rate = $limit_rate";
        echo ";";
        echo "remote_prot = $remote_port";
        echo ";";
        echo "remote_user = $remote_user";
        echo ";";
        echo "request_body_file = $request_body_file";
        echo ";";
        echo "request_method = $request_method";
        echo ";";
        echo "request_filename = $request_filename";
        echo ";";
        echo "request_uri = $request_uri";
        echo ";";
        echo "scheme = $scheme";
        echo ";";
        echo "server_protocol = $server_protocol";
        echo ";";
        echo "server_addr = $server_addr";
        echo ";";
        echo "server_name = $server_name";
        echo ";";
        echo "server_port = $server_port";

    }

}

重新加载配置文件

nginx -t
nginx -s reload

nginx的自定义变量

自定义变量名称和值,使用指令set variable value
格式如下:
set variable value;
支持:server, location, if
示例:
set name magedu;
echo name;
set my_port server_port;
echo my_port;
echo "server_name:$server_port";

测试实例

nginx的虚拟机配置

server {
    listen 80;
    server_name localhost;

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


    location /seal {
    index index.html;
    default_type text/html;
        set $NGX_VER  2020; #使用set自定义一个变量$NGX_VER,值为2020;
        set $NGX_NAME $server_name; #使用set自定义一个变量$NGX_VER,值为nginx内置变量$server_name的值;
        echo "NGX_VER=$NGX_VER";
        echo ";";
        echo "NGX_NAME=$NGX_NAME";
    }

}

重新加载配置文件

nginx -t
nginx -s reload

标签:index,变量,request,server,说明,nginx,html,echo
来源: https://www.cnblogs.com/yaokaka/p/13651226.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有