Nginx:location、proxy_pass中的斜杠/
学习自:
nginx location/区别详解_nginx_脚本之家
nginx的location与proxy_pass指令超详细讲解及其有无斜杠( / )结尾的区别_nginx location 斜杠-CSDN博客
nginx的location、root、alias指令用法和区别 – 蓝队云
Nginx系列-5 root和alias和index和try_files_nginx root index-CSDN博客
nginx root和index是什么 nginx location root配置_mob64ca140c75c7的技术博客_51CTO博客
1、前提
1)明确Server和URL
Server:提供HTTP服务的程序,通常为IP:Port的形式
URL:请求的资源在HTTP服务器上的路径,
http://127.0.0.1:8080/server/
Server:127.0.0.1:8080
URL:/server/
http://127.0.0.1:8080
Server:127.0.0.1:8080
URL:缺省,默认为/
2)location
location为对url的识别和匹配,我们在HTTP上输入的URL并非服务器上的路径(实际上我们也不知道别人服务器的文件组织结构是啥样)。
因此必须要有一个将url映射到服务器路径的机制,这就是nginx的主要工作之一。
这项工作是通过在nginx上配置location项实现的。
location url{
……
}
这样就可以将在1)中所说的url进行匹配,进而1)继续进行转发(转发到具体的微服务工程),通过proxy_pass关键字执行;2)直接在本地路径中找到所需的资源,并把该资源返回,通过root、alias、index关键字执行。
因此需要区分转发、直接返回资源两种情况。
2、proxy_pass:二次转发
写法:
location url1{
proxy_pass server2/url2
}
1)/url2为空
此时只有一个server2,那么转发的地址为server2/url1,是唯一一种会把url1拼到最终URL中的方式;
2)url2为空
此时只有一个单斜杠/,那么最终的转发地址为server2/
3)其他情况
当url2不为空时,实际转发地址的拼接规则均为
1)先从HTTP请求的URL中将server/url1拆出来;
2)剩余部分原封不动的拼到server2/url2之后
例子:
1)/url2为空
访问:www.test.com/api/upload
location /api{
proxy_pass http://127.0.0.1:8080
}
此时不管location后为/api还是/api/,都是转发到http://127.0.0.1:8080/api/upload。
这也是唯一一种会把location匹配的url拼到proxy_pass中的方式。
2)url2为空(与1的区别在于1连前边的斜杠都没有)
访问:www.test.com/api/upload
location /api{
proxy_pass http://127.0.0.1:8080/
}
此时不管location后为/api还是/api/,都是转发到http://127.0.0.1:8080/upload。
3)其他情况:url2不空
访问:www.test.com/api/upload
①location /api{
proxy_pass http://127.0.0.1:8080/server/
}
转发到http://127.0.0.1:8080/server/upload
②location /api/{
proxy_pass http://127.0.0.1:8080/server/
}
转发到http://127.0.0.1:8080/serverupload(这是因为把访问的url中将/api/拆去,剩下的部分只剩一个upload)
4)url末尾无斜杠
访问:www.test.com/api/upload
location /api{
proxy_pass http://127.0.0.1:8080/server
}
转发到http://127.0.0.1:8080/server/api/upload
3、root、alias、index
学习自:
nginx的location、root、alias指令用法和区别 – 蓝队云
Nginx系列-5 root和alias和index和try_files_nginx root index-CSDN博客
如果location中配了proxy_pass,那么请求会被转发。如果是Spring Cloud项目,大多数情况下会直接转给对应的微服务进行处理。
如果location中配了root、alias、index,那么请求就会被这个location接收,并用root所在目录下的某个静态资源文件返回。通常用于实现在ng上返回友好页面、静态图片之类的功能。
root和alias帮助找到静态文件在本地所在的目录。
index帮助找到静态文件在本地的文件名。
用法
root dir_path
index filename
或
alias dir_path
index filename
区别
①拼接location
root会把location后的路径拼接到dir_path后,即root+location+filename;
alias不会拼接,而是直接用将url中的内容剔除location之后,拼到alias之后。这一点和proxy_pass的拼接规则3类似。
②位置
root可以在server块或location块中;
alias只能在location块中。
③结尾斜杠
root结尾斜杠可写可不写;
alias结尾斜杠建议写,否则会像proxy_pass那样,存在url直接拼接的风险:
1 2 3 4 5 | #http://localhost:808/static/abc location /static/ { alias /home/sy ; } #拼接为/home/syabc |
例子
如果访问www.abc.com/t/index.html
1 2 3 4 5 6 7 8 | location /t { root /data/html ; } #实际本地路径为/data/html/t/index.html location /t { alias /data/html ; }<br> #实际本地路径为/data/html/index.html |
扩展
1、301 Moved Permanently
背景:当我们在访问的url中,并没有加斜杠时,此时nginx会先把这个url的末尾一节当做文件处理。
如果此时url能根据root指定的路径找到一个真实存在的目录,那么此时就会报301。
可以理解为系统提示这是个目录,如果要访问具体的内容请把文件路径补全。
2、url末尾有无斜杠(对root、alias规则相同)
url末尾有无斜杠,是区分url最后一节是目录还是文件的标志。
假设NG中的location块为:
1 2 3 | location /static { root /data/html ; } |
1)url:http://localhost:8088/static
Nginx会首先认为static是个文件:
①/data/html/static文件存在,则直接返回该文件,状态码200;
②/data/html/static/目录存在,则返回状态码301,即需要补全文件名;
③static既不是文件也不是目录,且不存在,则返回404。
2)url:http://localhost:8088/static/
Nginx会首先认为static是个目录:
①/data/html/static/目录不存在,返回404;
②/data/html/static/目录存在,但该目录下的index.html不存在,返回403;
③/data/html/static/index.html存在,将index.html文件返回
4、try_files
try_files用于location、server块中,用于控制文件查找、请求重定向的逻辑。
1)用法
①try_files file1 file2 ... uri;
当file1、file2……都不存在时,重写请求至uri;
②try_files file1 file2 ... =code;
当file1、file2……都不存在时,返回状态码code。
2)例子
①try_files file1 file2 ... uri
1 2 3 4 | location / { root /data/html ; try_files $uri $uri/ /index .html; } |
当$uri、$uri/都不存在时,NG会在内部将请求重写至/index.html。
1 2 3 4 5 6 7 8 | location / { root /data/html try_files $uri $uri/ @redirect_to_baidu; } location @redirect_to_baidu { return 301 http: //www .baidu.com; } |
这里的uri是以@开头命名的location。
②try_files file1 file2 ... =code;
1 2 3 4 | location /{ root /data/html try_files $uri $uri/ =400; } |
当$uri、$uri/指代的文件都不存在时,返回400状态码。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性