nginx的try_files指令学习

背景

公司一个前端项目用的vue写的,在登陆页面放了几张图片,项目build后部署到服务器,无法显示图片,但是图片的请求却是正常的200状态。我的第一反应是公司加密软件的问题,但是我打开图片发现没问题(正常情况下如果被加密会有一个锁的小图标),此处吐槽一下,就是辣鸡加密软件的问题,明明被加密了显示没加密,让我排查好久。 还以为是nginx缓存或者是nginx静态资源配置问题。我在排查过程中发现了一个其他的问题,请求链接是https://elink.sonoscape.com/img/LOGO.a1913efd.png,而我在nginx没有配置静态资源映射,那么它是怎么访问到的图片?
在这里插入图片描述

nginx配置
server {
    listen 443  default ssl;
    server_name xxxx.com;
    #charset koi8-r;
    root   /www/sono_consultation_view/consultation_system/dist;

    access_log  /var/log/nginx/443.access.log;
    error_log  /var/log/nginx/nginx.user_view_80.error.log  warn;

    #error_page  404              /404.html;

    ssl_certificate /ssl/consultation/4379831__sonoscape.com.pem;
    ssl_certificate_key /ssl/consultation/4379831__sonoscape.com.key;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api {
        proxy_pass   http://127.0.0.1:8080;
        proxy_redirect     off;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


    location /socket.io {
        proxy_pass http://php:2021;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    }

    location ~* /uploads/ {
        proxy_pass http://127.0.0.1:8080;
    }

}

nginx中try_files的作用
try_files写法

格式1:try_files file … uri; 格式2:try_files file … =code;

可应用的上下文:server,location段

语法说明
  1. 按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理
  2. 查找路径是按照给定的root或alias为根路径来查找的
  3. 如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配
  4. 如果是格式2,如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码\
本次的场景说明
location / {
       try_files $uri $uri/ /index.html;
   }

能找到静态资源图片的原因就是上边的这个配置,$uri代表查找文件,从root指定的路径查起, $uri/ 代表的目录

参考

参考资料1
参考资料2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值