nginx 学习之root 与 alias、正则、proxy_pass

本文深入探讨了Nginx中的root与alias配置的区别,以及如何使用正则表达式进行路径匹配。通过实例说明了root和alias在文件路径解析上的不同行为,同时详细解释了proxy_pass的配置方法。

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

root 与 alias

root

配置:

  location /testroot {
            root D:/nginx-1.16.1/html/3dTransform/;
            index  index.html index.htm;
		 	try_files $uri $uri/ /index.html;
        }

使用 root 指定路径时,访问 url + /testroot。具体路径将会指向 D:/nginx-1.16.1/html/3dTransform/testroot 下的文件。而当 testroot 文件夹不存在时,页面将访问不到,而将会跳转到nginx的index.html 。

alias

配置:

location /testroot {
            alias D:/nginx-1.16.1/html/3dTransform/;
            index  index.html index.htm;
		 	try_files $uri $uri/ /index.html;
        }

alias 与 root 不同,具体路径将会指向 D:/nginx-1.16.1/html/3dTransform 下的文件。

root 与 alias总结

配置:

location /uri {
            alias(root)  path;
       }

root: path + /uri 目录的文件
alias: path 目录的文件

nginx正则

语法规则

Nginx配置中Location的语法规则

location [ = | ~ | ~* | ^~ | !~ | !~* ] /uri/
{ }
  1. = 表示精确匹配
  2. ~ 表示区分大小写正则匹配
  3. ~* 表示不区分大小写正则匹配
  4. ^~ 表示URI以某个常规字符串开头
  5. !~ 表示区分大小写正则不匹配
  6. !~* 表示不区分大小写正则不匹配
  7. / 通用匹配,任何请求都会匹配到
root 使用正则
location ^~ /static/ {
			  root E:\assets;
			}

访问:http://192.168.16.12:8040/static/login_bg.jpg = E:/assets/static/login_bg.jpg
由 以上可知,root的路径,需要在 E:\assets 目录创建一个 static的文件夹

alias使用正则
location ^~ /static/ {
			  alias E:/assets/;
			}

访问:http://192.168.16.12:8040/static/login_bg.jpg = E:/assets/login_bg.jpg
注:在末尾 加上 ‘/’。
alias 使用 ,* 之类的正则。

location  ~ /alert-model/ {
            alias D:/nginx-1.16.1/html/3dTransform/;
            index  index.html index.htm;
		 	try_files $uri $uri/ /index.html;
		
		}

访问:http://192.168.16.12:8040//alert-model/ = D:/nginx-1.16.1/html/3dTransform/alert-model;
这时 需要在3dTransform 目录下创建 alert-model文件夹。

proxy_pass

配置:

location  ^~ /alert-model/ {

			proxy_set_header HOST $host;
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_pass http://127.0.0.1:8041/;
		}

访问: http://192.168.16.12:8040/alert-model/ = http://192.168.16.12:8041/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

今天也想MK代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值