1.跨域配置 在server模块下
访问如:http://127.0.0.1:8080/static/a.txt
#跨域server {listen 8080;server_name localhost;#允许跨域请求的域, *代表所有add_header 'Access-Control-Allow-Origin' *;#允许带上cookie请求add_header 'Access-Control-Allow-Credentials' 'true';#允许请求的方法,比如 GET/POST/PUT/DELETEadd_header 'Access-Control-Allow-Methods' *;#允许请求的headeradd_header 'Access-Control-Allow-Headers' *;#http://127.0.0.1:8080/static/a.txt 可访问到 /home/images目录下的a.txt文件location /static {#root /home; #指定根目录alais /home/images;}}
2.压缩文件传输 在http块下
#开启压缩功能,提高传输率gzip on;#限制最小压缩,即小于1字节的文件不会被压缩gzip_min_length 1;#定义压缩的级别(压缩比越大,压缩越狠,同时cup使用更多)gzip_comp_level 3;#定义压缩文件的类型gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/json;
3.文件下载
server {listen 8080;server_name localhost;# /download 映射到服务器 /home/data 目录location /download {alias /home/data;#资源放此文件夹下autoindex on;autoindex_exact_size off;}}
4.静态文件直接访问
http://127.0.0.1:8080/static/xx.jpg 即可访问到服务器 /home/images/xx.jpg
server {listen 8080;server_name localhost;location /static {#root /home; #指定根目录alais /home/images;}}
5.防盗链 server模块下
server {listen 8080;server_name localhost;#对源站点验证 查看请求是不是这个源站请求过来的valid_referers *.xxx.com; #如果不是源站访问过来的,则返回404阻止访问if ($valid_referers) {return 404;}location /static {#root /home; #指定根目录alais /home/images;}}