Windows下nginx作为静态资源服务器测试

本文详细介绍了如何通过修改Nginx配置实现静态资源的跨域访问,并解释了autoindex指令的作用,展示了如何控制目录列表的显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、修改nginx配置,添加以下配置

 

#url  http://localhost/image/file/22.jpg 对应文件 E:/file/image/file/22.jpg
		#url  http://localhost/image/33.jpg   对应文件  E:/file/image/33.jpg
		 location /image/ {
            root   E:/file/;
			autoindex on;

#添加跨域配置
           add_header 'Access-Control-Allow-Origin' $http_origin;
           add_header 'Access-Control-Allow-Credentials' 'true';
           add_header 'Access-Control-Allow-Methods' 'GET,PUT,POST,DELETE,OPTIONS';
           add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Ca
che-Control,Content-Type,Range,token,sign,timestamp';
           add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
          
        }
#http://192.168.2.41/file/11.jpg   对应 /dit/uploads/file/11.jpg
location /file/ {

             root   /dit/uploads/;
#添加跨域配置
           add_header 'Access-Control-Allow-Origin' $http_origin;
           add_header 'Access-Control-Allow-Credentials' 'true';
           add_header 'Access-Control-Allow-Methods' 'GET,PUT,POST,DELETE,OPTIONS';
           add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Ca
che-Control,Content-Type,Range,token,sign,timestamp';
           add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';


        }

2、配置

autoindex off;

表示不能通过目录访问到文件列表,必须精确访问到文件地址。

autoindex on;的时候访问文件目录会显示该目录下的所有文件。

如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值