Nginx (自定义/预定义)变量,alias 虚拟目录

2024-01-25 20:20

本文主要是介绍Nginx (自定义/预定义)变量,alias 虚拟目录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Nginx 变量

  • 1. Nginx 变量简介
  • 2. Nginx 变量的定义和使用
    • 2.1. 自定义变量
    • 2.2. 内置预定义变量
  • 3. Nginx alias 虚拟目录(拓展)

Nginx的配置文件使用语法的就是一门微型的编程语言。既然是编程语言,一般也就少不了“变量”这种东西。

1. Nginx 变量简介

  • 所有的 Nginx变量在 Nginx 配置文件中引用时都须带上 $ 前缀
  • 在 Nginx 配置中,变量只能存放一种类型的值,而且也只存在一种类型,那就是字符串类型
    所有的变量值都可以通过这种方式引用 $变量名

2. Nginx 变量的定义和使用

nginx中的变量分为两种,自定义变量与内置预定义变量

2.1. 自定义变量

1、声明变量可以在sever,http,location等标签中使用set命令声明变量,语法如下
set $变量名 变量值

注意:

  • nginx 中的变量必须都以 $ 开头
  • nginx 的配置文件中所有使用的变量都必须是声明过的,否则 nginx 会无法启动并打印相关异常日志

nginx安装echo模块

//查看已经安装的nginx的版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.24.0//上传或者下载一个相同版本的nginx包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
//下载echo模块的安装包
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/refs/tags/v0.63.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.24.0.tar.gz  v0.63.tar.gz
//解压到相同路径下:
[root@localhost ~]# tar xzvf nginx-1.24.0.tar.gz -C /usr/local/
[root@localhost ~]# tar xzvf v0.63.tar.gz -C /usr/local/
//安装编译工具
[root@localhost ~]# cd /usr/local/
[root@localhost local]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++   zlib zlib-devel gd-devel//添加模块:
[root@localhost local]# cd nginx-1.24.0///添加上原来已经有的参数和新添加的模块:
[root@localhost nginx-1.24.0]# ./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.63//编译,千万不要make install 否则会覆盖原来的文件
[root@localhost nginx-1.24.0]# make
//将原来的nignx二进制文件备份
[root@localhost nginx-1.24.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak 
//拷贝替换新的nignx二进制文件
[root@localhost nginx-1.24.0]# cp objs/nginx /usr/sbin/  
[root@localhost nginx-1.24.0]# systemctl restart nginx    //重启nginx
//查看模块是否添加成功
[root@localhost nginx-1.24.0]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.63

2、配置 $foo=hello

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim echo.conf
server {listen 80;server_name     localhost;location /test {set $foo hello;echo "foo: $foo";}
}
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx
//访问测试
[root@localhost conf.d]# curl  http://localhost/test
foo: hello

Nginx 变量的创建只能发生在 Nginx 配置加载的时候,或者说 Nginx 启动的时候。
而赋值操作则只会发生在请求实际处理的时候。这意味着不创建变量,直接使用变量会导致启动失败。

2.2. 内置预定义变量

内置预定义变量即无需声明就可以使用的变量,通常包括一个 http 请求或响应中一部分内容的值,以下为一些常用的内置预定义变量

变量名定义
$arg_PARAMETERGET请求中变量名PARAMETER参数的值。
$args这个变量等于GET请求中的参数。例如,foo=123&bar=blahblah;这个变量只可以被修改。
$binary_remote_addr二进制码形式的客户端地址。
$body_bytes_sent传送页面的字节数。
$content_length请求头中的Content-length字段。
$content_type请求头中的Content-Type字段。
$cookie_COOKIEcookie COOKIE的值。
$document_root当前请求在root指令中指定的值。
$document_uri与$uri相同。
$host请求中的主机头(Host)字段,如果请求中的主机头不可用或者空,则为处理请求的server名称(处理请求的server的server_name指令的值)。值为小写,不包含端口。
$hostname机器名使用 gethostname系统调用的值
$http_HEADERHTTP请求头中的内容,HEADER为HTTP请求中的内容转为小写,-变为_(破折号变为下划线),例如:$http_user_agent(Uaer-Agent的值)。
$sent_http_HEADERHTTP响应头中的内容,HEADER为HTTP响应中的内容转为小写,-变为_(破折号变为下划线),例如: $sent_http_cache_control, $sent_http_content_type…;
$is_args如果$args设置,值为"?“,否则为”"。
$limit_rate这个变量可以限制连接速率。
$nginx_version当前运行的nginx版本号。
$query_string与$args相同。
$remote_addr客户端的IP地址。
$remote_port客户端的端口。
$remote_user已经经过Auth Basic Module验证的用户名。
$request_filename当前连接请求的文件路径,由root或alias指令与URI请求生成。
$request_body这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。
$request_body_file客户端请求主体信息的临时文件名。
$request_completion如果请求成功,设为"OK";如果请求未完成或者不是一系列请求中最后一部分则设为空。
$request_method这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。
$request_uri这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
$scheme所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect。
$server_addr服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
$server_name服务器名称。
$server_port请求到达服务器的端口号。
$server_protocol请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$uri请求中的当前URI(不带请求参数,参数位于args),不同于浏览器传递的args),不同于浏览器传递的args),不同于浏览器传递的request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html

3. Nginx alias 虚拟目录(拓展)

root:为 location 指定网站的根目录。location 里定义的子路径是跟这个根目录相对的。
alias:为 location 指定服务的具体文件路径。location 里定义的子路径会被忽略
使用alias标签的目录块中不能使用 rewrite 的 break 。

server {listen 80;server_name localhost;location /test {root /var/www/html;index index.html;}location /qfedu {alias /var/www/nginx; #访问http://x.x.x.x/qfedu时实际上访问是/var/www/nginx/index.htmlindex index.html;}
}//创建location匹配的目录和文件
[root@localhost ~]# mkdir /var/www/html/test
[root@localhost ~]# echo "location /test" >> /var/www/html/test/index.html//创建alias匹配的目录和文件
[root@localhost ~]# mkdir /var/www/nginx
[root@localhost ~]# echo "alias /qfedu" >> /var/www/nginx/index.html[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx

root 和 alias 的主要区别是:

  • 使用root,实际的路径就是:root值 + location值。root会将完整的url映射进文件路径,root可以看成是一个相对路径,访问url网址时,http://www.baidu.com/test,这个test必须要在/usr/share/nginx/html下有这个目录,目录中必须要这个文件才行。
  • 使用alias,实际的路径就是:alias值。alias只会将localhost后的url映射到文件路径。
    而使用alias时,访问http://www.baidu.com/test,在/usr/share/nginx/html不需要有这个test目录即可。
  • alias只能位于location块中,root可以放在http,server,location块中

这篇关于Nginx (自定义/预定义)变量,alias 虚拟目录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/644501

相关文章

Nginx如何进行流量按比例转发

《Nginx如何进行流量按比例转发》Nginx可以借助split_clients指令或通过weight参数以及Lua脚本实现流量按比例转发,下面小编就为大家介绍一下两种方式具体的操作步骤吧... 目录方式一:借助split_clients指令1. 配置split_clients2. 配置后端服务器组3. 配

Nginx实现前端灰度发布

《Nginx实现前端灰度发布》灰度发布是一种重要的策略,它允许我们在不影响所有用户的情况下,逐步推出新功能或更新,通过灰度发布,我们可以测试新版本的稳定性和性能,下面就来介绍一下前端灰度发布的使用,感... 目录前言一、基于权重的流量分配二、基于 Cookie 的分流三、基于请求头的分流四、基于请求参数的分

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

一文详解Nginx的强缓存和协商缓存

《一文详解Nginx的强缓存和协商缓存》这篇文章主要为大家详细介绍了Nginx中强缓存和协商缓存的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、强缓存(Strong Cache)1. 定义2. 响应头3. Nginx 配置示例4. 行为5. 适用场景二、协商缓存(协

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

Ubuntu 22.04 服务器安装部署(nginx+postgresql)

《Ubuntu22.04服务器安装部署(nginx+postgresql)》Ubuntu22.04LTS是迄今为止最好的Ubuntu版本之一,很多linux的应用服务器都是选择的这个版本... 目录是什么让 Ubuntu 22.04 LTS 变得安全?更新了安全包linux 内核改进一、部署环境二、安装系统