nginx 做本地图片js等静态资源映射,转发请求
- server_name:监听请求名称
- root E:\fileServer\uploadFile; 指定本地静态文件路径
- if (!-f $request_filename) 判断请求的文件是否不存在,不存在则转发请求到。
备注$1、$2…”等变量为请求正则中第几个参数,如:
location ~* /(.*)/(\d+)(\d+)(\d+)x(\d+)_([0-9a-xA-X]+).(jpg|png|jpeg|gif)$ { - proxy_store 存为缓存,加快访问速度。proxy_store_access 加上文件的读写权限
- 转发请求:proxy_pass “http://localhost:8080/imgService/thumbnailImgNginx?itemImg=$filepath&width=$width&height=$height&editType=center”;
具体server{}配置:
server {
listen 80;
server_name img.beidouxh.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://img.beidouxh.com:8080/imgService/test;
}
location ~* /(.*)\/(\d+)_(\d+)_(\d+)x(\d+)_([0-9a-xA-X]+)\.(jpg|png|jpeg|gif)$ {
set $filepath "/$1/$2_$3.$7";
set $filename "$2_$3.$7";
set $thumb "$2_$3_$4x$5_$6.$7";
set $width $4;
set $height $5;
set $editType $7;
root E:\fileServer\uploadFile;
if (!-f $request_filename) {
proxy_pass http://localhost:8080/imgService/thumbnailImgNginx?itemImg=$filepath&width=$width&height=$height&editType=center;
break;
}
proxy_store $document_root/$1/$thumb;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
}
location ~* \.(jpg|png|jpeg|gif)$ {
root E:\fileServer\uploadFile;
}