当前位置: 首页 > news >正文

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配置了显示网络速率,则会显示,如果没有设置, 则显示0echo $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 

http://www.lryc.cn/news/427077.html

相关文章:

  • Windows采用VS2019实现Open3D的C++应用
  • 冒泡排序、选择排序、插入排序,三种简单排序算法的区别?
  • Docker 日志管理
  • JavaScript初级——基础知识
  • 0817(持久层框架:JDBC,MyBatis)
  • 在亚马逊云科技上安全、合规地创建AI大模型训练基础设施并开发AI应用服务
  • 无人机模拟训练室技术详解
  • 【Spring框架】
  • uniapp 日常业务 随便写写 源码
  • 【软件测试】单元测试20套练习题
  • 8.16 day bug
  • 《Nginx核心技术》第11章:实现MySQL数据库的负载均衡
  • 使用 Gnosis Safe 创建多签名钱包
  • LeetCode 算法:前 K 个高频元素 c++
  • MySQL的SQL语句更新某个字段的值在原来值的基础上随机增加100~600
  • LeetCode --- 410周赛
  • 最佳的iPhone解锁软件和应用程序
  • 初等函数和它的表达式
  • Android 12系统源码_多屏幕(二)模拟辅助设备功能开关实现原理
  • 【Go语言初探】(二)、项目文件结构和GOPATH设置
  • 三种简单排序:插入排序、冒泡排序与选择排序 【算法 05】
  • Python -- GUI图形界面编程—GUI编程实例 博主也在持续学习中[ 持续更新中!!! 欢迎白嫖 也求粉啊啊啊~ ]
  • Vue2和Vue3中的diff算法
  • springboot使用aop或Jackson进行数据脱敏
  • 【Solidity】基础介绍
  • 【SpringBoot3】双向实时通讯 websocket
  • 搭建内网开发环境(一)|基于docker快速部署开发环境
  • MATLAB R2023b配置Fortran编译器
  • 2024新型数字政府综合解决方案(七)
  • 搭建高可用k8s集群