nginx配置多个静态网页,无法访问相对路径下图片资源?

环境:一个腾云服务器,装宝塔linux面包板,在面板软件中装了个nginx1.22.0
目标:
地址/test1加载一个静态网页,资源文件都在/www/server/my/test内
地址/test2加载另一个静态网页,资源文件都在/www/server/my/test2内
遇见问题:test2.html内代码的,bgGif无法访问/www/server/my/test/images内的图片。
是相对路径没填对?还是location设置有问题?或者用绝对路径应该怎么填?

nginx的location设置,如下
server
{
listen 80;
server_name localhost;

    #error_page   404   /404.html;
    include enable-php.conf;
    
    location /{
      root /wwwrver/my;
      index home1.html;
    }
    
    location /test{
      root /wwwrver/my;
      index test.html;
    }
    
    location /test2{
      root /wwwrver/my;
      index test2.html;
    }
    location /images{
      root /wwwrver/my/test2;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /\.
    {
        deny all;
    }
    access_log  /www/wwwlogs/access.log;
}

include /wwwrver/panel7host inx/*.conf;
}

文件路径,如下两图

img

img

html代码

img

代码为网上下载,的一个表白网页源码
这里遇见问题,"./images/图片"资源无法访问


1我配置文件location应该如何写,才能相对路径下访问图片资源?
2 test2.html内<images src=,应该如何写绝对路径才能访问图片资源?
问题图片

img

问题补充1
我用这样的链接可以访问到txt文本
地址/test2/images/imgtxt2.txt
但是用
地址/test2/images/001.png
显示这样

img

是被nginx的默认配置里面的某项拒绝了访问图片么?
server配置内容在前面

问题补充2
注释掉了这3个location之后就可以了

img


问这3句话有什么用呀?

一步步排查吧!

  1. 在你每个root目录下放个test.txt文件,然后通过对应的url访问,确保location配置的是正确的
  2. 再单独访问你images中的图片能不能正常访问
  3. 使用chrome浏览器,F12,看控制台network,看你无法打开图片的URL与步骤2中的地址有啥区别

说实话,有点复杂

如果你要test2访问test目录下的图片要用绝对链接,就是http://www.test.html/images/cat.gif 这样的
test访问test2目录下的图片用绝对链接http://www.test2.html/images/cat.gif

nginx 搭建图片服务器 然后用 ip地址+文件路径进行访问肯定是可以的

你网页打开后,F12看一下图片的加载路径,看下路径中是否包含/test 或者 test2 ,如果路径中没有指明test还是test2,是获取不到,因为根据你的配置,根目录时是指向“my”这里文件夹,而这个文件夹下没有你要的图片,所以加载失败