如何解决Nginx访问图片显示404,403错误

问题描述

项目中所有的资源存放如下目录

/www/jianshu/public/storage

希望通过url的方式将其展示出来,如下是配置:

server
{
    listen 80;
    listen [::]:80;
    server_name demo.test.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/test/public;
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-74.conf;
    #PHP-INFO-END
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /server/vhost/rewrite/demo.test.com.conf;
    #REWRITE-END
    if (!-e $request_filename) {
       rewrite  ^(.*)$  /index.php?s=/$1  last;
       break;
    }
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
    }
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null; 
    }
    access_log  /wwwlogs/demo.test.com.log;
    error_log  /wwwlogs/demo.test.com.error.log;
}

在按照上面的配置访问时,如果返回404解决方案如下:

新增资源访问配置

location ^~ /storage/ {
    alias  /www/test/public/storage/;
}

总结

1.root响应的路径:配置的路径(root指向的路径)+完整访问路径(location的路径)+静态文件

2.alias响应的路径:配置路径+静态文件(去除location中配置的路径)


评论(0条)

刀客源码 游客评论