Nginx中root和alias的区别
nginx中的root的使用方式
- 定义:
root
指令用于设置服务器上的根目录,从这个根目录开始,Nginx 会根据请求的 URI 拼接路径来寻找文件。 - 使用方式:
root
指令通常在server
块或location
块中使用。 - 路径:
root
所指定的路径会与location
块中的 URI 一起构成文件系统路径。
server {
listen 80;
server_name example.com;
location /images/ {
root /var/www/data;
}
}
在上面的配置中,请求 http://example.com/images/example.jpg
会映射到 /var/www/data/images/example.jpg
。
---------------------------------------------------------------------------------------------------------------------------------------------------------
拾壹博客前端前台的网页发起请求连接如下
这就是适合使用root的情况,具体代码如下图
location / {
root c:/install/blog/dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
nginx中的alias的使用方式
- 定义:
alias
指令用于将某个 URI 直接映射到文件系统中的某个目录。alias
替换的是location
块匹配的整个路径。 - 使用方式:
alias
只能在location
块中使用。 - 路径:
alias
所指定的路径是直接映射到文件系统路径,并不会与location
块中的 URI 进行进一步组合。
server {
listen 80;
server_name example.com;
location /image/ {
alias /var/www/images/;
}
}
在上面的配置中,请求 http://example.com/image/example.jpg
会直接映射到 /var/www/images/example.jpg
,而不是 /var/www/images/image/example.jpg
。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
拾壹博客前端后台的网页发起请求连接如下
这就是适合使用alias的情况,具体代码如下图
location /admin {
alias c:/install/blog/dist2/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签