NGINX 常用内置变量

目录

$remote_addr 变量

$args 变量

$is_args 变量

$document_root 变量

$document_uri 变量

$host 变量

$limit_rate 变量

$remote_port  变量

$remote_port --显示客户端端口

$request_method 变量  --返回请求方式

$request_filename 变量 --返回请求实际路径

$request_uri;  变量 --返回document_uri 与 args

$server_protocol  变量 -- 服务端协议

$server_addr  变量  -- 服务器地址

$server_name -- 虚拟主机名称

$server_port  -- 访问主机端口

$http_cookie  -- 返回cookie值

$cookie_<name>

多变量组合成URL


变量描述
$remote_addr
存放了客户端的地址,注意是客户端的公网 IP
$args

URL 中的所有查询参数。

例如:https://example.com/search?query=test&enc=utf-8

返回 query=test&enc=utf-8

$is_args如果 URL 中有查询参数,则为 ?,否则为空。
$document_root当前请求的系统根目录。例如:/webdata/nginx/timinglee.org/lee
$document_uri当前请求中不包含查询参数的 URI。例如:/var
$host请求的 Host 名称。
$limit_rate如果设置了 limit_rate,则显示限制的网络速率,否则为 0
$remote_port客户端请求 Nginx 服务器时使用的端口。
$remote_user经过 Auth Basic Module 验证的用户名。
$request_body_file反向代理时发给后端服务器的本地资源文件名。
$request_method请求的方法(例如:GET, PUT, DELETE 等)。
$request_filename当前请求的资源文件的磁盘路径。
$request_uri包含查询参数的原始 URI,不包含主机名。例如:/main/index.do?id=20190221&partner=search
$scheme请求的协议(例如:http, https, ftp 等)。
$server_protocol客户端请求资源使用的协议版本(例如:HTTP/1.0, HTTP/1.1, HTTP/2.0 等)。
$server_addr服务器的 IP 地址。
$server_name虚拟主机的主机名。
$server_port虚拟主机的端口号。
$http_user_agent客户端浏览器的详细信息。
$http_cookie客户端的所有 Cookie 信息。
$cookie_<name>请求报文中特定 Cookie 的值,<name> 替换为 Cookie 的名称。
$http_<name>记录请求报文的首部字段,<name> 替换为首部字段的小写形式,横线替换为下划线。

示例

$remote_addr 变量

[root@RHEL-9 conf.d]# vim /usr/local/nginx/conf.d/var.conf 
server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;

    location /var {
        default_type text/html;
        echo $remote_addr;
    }

[root@RHEL-9 conf.d]# systemctl restart nginx


# 客户端访问
[root@docker-rhel ~]# curl var.shuyan.com/var
192.168.239.50

$args 变量

[root@RHEL-9 conf.d]# vim var.conf 
server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;

    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
    }
[root@RHEL-9 conf.d]# nginx -s reload

# 客户端测试 $args 匹配? 后面的值
[root@docker-rhel ~]# curl var.shuyan.com/var?name=111www=123
192.168.239.50
name=111www=123

$is_args 变量

$document_root 变量

[root@RHEL-9 conf.d]# vim var.conf 
server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;

    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
    }

[root@RHEL-9 conf.d]# nginx -s reload

实现效果

$document_uri 变量

[root@RHEL-9 conf.d]# vim var.conf 
server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;

    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
    }

[root@RHEL-9 conf.d]# nginx -s reload

测试效果

实现效果

$host 变量

[root@RHEL-9 conf.d]# vim var.conf
server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;

    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $document_root$document_uri;
        echo $host;
    }

[root@RHEL-9 conf.d]# nginx -s reload

$limit_rate 变量

server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;
    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $document_root$document_uri;
        echo $host;
        # 限速打印
        #如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
        echo $limit_rate;
    }

$remote_port  变量

server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;
    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $document_root$document_uri;
        echo $host;
        echo $limit_rate;

        #客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口
        echo $remote_port;
    }

$remote_port --显示客户端端口

server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;
    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $document_root$document_uri;
        echo $host;
        echo $limit_rate;
        echo $remote_port;

        # 已经经过Auth Basic Module验证的用户名
        echo $remote_user;
    }


# 客户端测试
[root@docker-rhel ~]# curl -u shuyan:123456 var.shuyan.com/var?name=111www=123
192.168.239.50
name=111www=123
?
/data/web/html
/var
/data/web/html/var
var.shuyan.com
0
33194
shuyan

$request_method 变量  --返回请求方式

server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;
    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $document_root$document_uri;
        echo $host;
        echo $limit_rate;
        echo $remote_port;
        echo $remote_user;
        # 返回请求资源的方式  GET/PUT/DELETE等
        echo $request_method;
    }

$request_filename 变量 --返回请求实际路径

server {
    listen 80;
    server_name var.shuyan.com;
    root /data/web/html;
    index index.html;
    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $document_root$document_uri;
        echo $host;
        echo $limit_rate;
        echo $remote_port;
        echo $remote_user;
        echo $request_method;
        # 当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径
        echo $request_filename;
    }

[root@docker-rhel ~]# curl -u shuyan:123456 var.shuyan.com/var?name=111www=123
192.168.239.50
name=111www=123
?
/data/web/html
/var
/data/web/html/var
var.shuyan.com
0
33206
shuyan
GET
/data/web/html/var

$request_uri;  变量 --返回document_uri 与 args

# 包含请求参数的原始 URI ,不包含主机名,相当于 :$document_uri?$args,

$scheme 变量

$server_protocol  变量 -- 服务端协议

# 保存了客户端请求资源使用的协议的版本,例如 :HTTP/1.0 HTTP/1.1 HTTP/2.0

$server_addr  变量  -- 服务器地址

# 保存了服务器的 IP 地址

$server_name -- 虚拟主机名称

# 虚拟主机的主机名

$server_port  -- 访问主机端口

$http_user_agent
# 客户端浏览器的详细信息

$http_cookie  -- 返回cookie值

# 客户端的所有 cookie 信息

$cookie_<name>

#name 为任意请求报文首部字部 cookie key

$http_<name>

# name 为任意请求报文首部字段 , 表示记录请求报文的首部字段,n ame 的对应的首部字段名需要为小写,如果有 横线需要替换为下划线

多变量组合成URL

$scheme、$host、$document_uri、$args 

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妍妍的宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值