Nginx解决访问图片显示404,403问题

###### 问题描述 项目中所有的资源放在`/www/jianshu/public/storage`中,希望通过url的方式将其展示出来,如下是配置 ```bash eg: http://demo.test.com/storage/img/1.png ``` ```bash server { listen 80; listen [::]:80; server_name demo.test.com; index index.php index.html in

Nginx解决访问图片显示404,403问题
问题描述

项目中所有的资源放在/www/jianshu/public/storage中,希望通过url的方式将其展示出来,如下是配置

eg: http://demo.test.com/storage/img/1.png
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/;
}
总结

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

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

上一篇:Laravel5多库使用事务失效问题解决 下一篇:PHP面试题之代码设计类

评论

评论(10)

暂无评论