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

本文详细介绍了Nginx中的变量类型、自定义变量的声明与使用、内置预定义变量的列举,以及alias在创建虚拟目录时的区别。通过实例展示了如何在配置文件中使用这些概念,包括模块的安装和配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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.html
                index 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块中
<think>好的,我现在要分析用户提供的这个Nginx配置文件。首先,我需要了解每个部分的作用,确保理解正确。然后根据用户的要求,用中文详细解释配置的结构和各个指令的功能,同时注意行内数学表达式和独立公式的格式要求。不过看起来这个配置主要是Nginx的server块设置,可能不涉及数学公式,但需要确认是否有需要用到的地方。如果有变量或路径中的特殊符号,可能需要用$...$格式。比如路径中的变量或者正则表达式。 首先,用户提供的配置是一个Nginx的server块,监听9100端口,服务器名是192.168.1.127。接下来有很多location块,每个处理不同的路径。需要逐一解释这些location的作用,比如alias、proxy_pass等指令的功能。 注意用户提到的系统级指令,比如所有行内数学表达式需要用$...$,独立公式用$$...$$。但在这个配置中,可能主要涉及的是代码块和路径,数学表达式可能较少。但如果有像变量$host、$remote_addr等,可能需要用$...$包裹,比如$host$。需要检查是否有需要这样处理的地方。 接下来,按照用户要求的结构,分步骤解释配置的每个部分。确保结构清晰,帮助用户理解每个配置项的作用。可能需要分段讲解server块的基本设置,然后每个location的作用,包括静态文件服务、反向代理等。 需要确认是否存在潜在问题,比如使用alias和proxy_pass时的路径是否正确,是否有斜杠问题。例如,location /admin 使用alias到目录dist,这时候访问/admin/时,Nginx会从该目录下寻找文件。而proxy_pass的地址是否带有结尾的斜杠,会影响URL的转发方式。例如,location /oauth2 的proxy_pass后面有/oauth2,是否正确?可能需要指出这些细节。 另外,用户配置中的absolute_redirect off; 是为了防止Nginx在重定向时使用绝对路径,可能会影响某些情况下的重定向行为,需要解释这一点。 在解释过程中,要确保使用中文,语句通顺,结构清晰,分点说明各个部分的功能,指出可能的注意事项或常见问题。比如,使用alias时,目录路径是否正确,权限是否足够,以及proxy_pass的目标地址是否可达等。 最后,检查是否有语法错误,比如括号是否匹配,分号是否正确,虽然用户可能已经确认过,但指出潜在的错误点也是有帮助的。比如,location /govern/ 的proxy_pass后面是否有斜杠导致URL替换的问题。 总结来说,我需要将整个配置文件分解,逐一解释每个指令和块的作用,注意事项,确保用户能够理解配置的结构和每个部分的功能,同时指出可能的配置陷阱或最佳实践。</think>以下是对您提供的Nginx配置文件的逐步解析: --- ### 一、基础服务器配置 ```nginx server { listen 9100; server_name 192.168.1.127; absolute_redirect off; access_log /var/log/nginx/threecenter_access.log; ``` - **核心功能**:定义一个监听端口为$9100$、域名为$192.168.1.127$的虚拟主机 - **关键指令**: - `absolute_redirect off`:禁用绝对路径重定向(避免因域名变化导致重定向异常) - `access_log`:指定访问日志存储路径为$/var/log/nginx/threecenter\_access.log$ --- ### 二、静态资源路由配置 #### 1. 后台管理页面 ```nginx location /admin { alias C:/Work/luan/frontend/tc_admin_web/dist; index index.html index.htm; } ``` - **路径映射**:将URL路径$/admin$映射到本地目录$C:/Work/luan/frontend/tc\_admin\_web/dist$ - **示例**:访问$http://192.168.1.127:9100/admin/index.html$会返回该目录下的$index.html$ #### 2. 登录页面 ```nginx location /login { alias C:/Work/luan/frontend/tc_oauth2_web/dist; index index.html index.htm; } ``` - 逻辑同上,处理登录相关静态资源 #### 3. 二维码资源目录 ```nginx location /qr { alias C:/Work/luan/frontend/qr-img; index index.html index.htm; } ``` - 专用于提供二维码图片等静态资源 --- ### 三、反向代理配置 #### 1. OAuth2服务代理 ```nginx location /oauth2 { proxy_pass http://192.168.1.127:7070/oauth2; } ``` - **流量转发**:将$/oauth2$请求转发到后端服务$http://192.168.1.127:7070/oauth2$ - **注意**:由于proxy_pass末尾包含路径$/_oauth2$,实际URL会进行路径替换(如$/oauth2/token$ → $http://后端/oauth2/token$) #### 2. 后台API代理 ```nginx location /api/admin { proxy_pass http://192.168.1.127:7071/admin; # 其他proxy_set_header配置... } ``` - **Header处理**:透传客户端真实IP($X-Real-IP$)、域名等信息 - **超时控制**:设置连接超时时间为$15$秒 #### 3. 文件服务代理 ```nginx location /file { proxy_pass http://192.168.1.127:7071/admin; } ``` - 特别说明:此配置将$/file$请求全部转发到后台的$/admin$接口 --- ### 四、特殊配置项 #### 1. 跨系统服务代理 ```nginx location /govern/ { proxy_pass http://10.90.3.47:7080/; } ``` - **路径处理**:由于proxy_pass以斜杠$/$结尾,请求路径会完全替换(如$/govern/test$ → $http://10.90.3.47:7080/test$) #### 2. 错误页面配置 ```nginx error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } ``` - 自定义5xx错误页面,从$/usr/share/nginx/html$目录读取$50x.html$ --- ### 五、潜在优化点 1. **路径安全性**:Windows风格路径$C:/Work/...$在Linux环境下需检查文件系统挂载配置 2. **斜杠处理**:`location /govern/`与`proxy_pass`后的斜杠需严格匹配,避免路径拼接错误 3. **日志管理**:建议增加$error\_log$配置以便问题排查 通过此配置,Nginx实现了静态资源托管、多后端服务代理、错误处理等核心功能,构建了完整的请求分发体系。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TA548464

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

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

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

打赏作者

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

抵扣说明:

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

余额充值