nginx 配置访问非web 目录下的文件

背景: 在某个web 目录下的网站,但图片存储在非web 目录下,想通过浏览器访问非web目录下的资源.

如图所示

web 路径在

server {
        listen       8995;
        server_name   web_ht_virus;
        root   "/web/project/public";
        location / {
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
            }    
            #autoindex  on;  
        }    
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }    
}

需要加入相关配置

alias /data   相关资源如图片、视频等资源存放在/data目录下,则访问http://127.0.0.1/data/me/myvideo.mp4

 完整配置

server {
        listen       8995;
        server_name   web_ht_virus;
        root   "/web/project/public";
        location / {
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
            }    
            #autoindex  on;  
        }    
        location /data {
            alias /data/;
            autoindex on;
        }    
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }    
}

posted @ 2022-01-25 18:05  树下水月  阅读(200)  评论(0编辑  收藏  举报