1
mikeguan 2019-08-18 10:12:30 +08:00 via Android
root 和 rewrite 或许可以满足你的需求
|
2
xiri 2019-08-18 10:24:12 +08:00
如果你是想做到访问 mydomain.com/123/a.jpg ,结果到 /var/local/img/123/a.jpg 找文件,把 root 目录设为 /var/local/img 就够了
如果你是想做到访问 mydomain.com/123/a.jpg ,结果到 /var/local/img/a.jpg (没有 123 目录)找文件,在对应的 location 里指定 alias 到 /var/local/img 就行了 |
3
redbelt OP |
5
xiri 2019-08-18 10:27:30 +08:00
|
7
redbelt OP 按照 @xiri 大佬的指导配置了一下 ,还是不行 ,我补充下配置和日志:
测试地址: http://note.tianyongwei.com/%E7%AC%94%E8%AE%B0/0%E9%80%9A%E8%AF%86%E6%8A%80%E6%9C%AF/image-20190727104421457.png/ 配置文件: server { listen 80; server_name note.tianyongwei.com; # root /usr/share/nginx/Note/note_by_js; # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; location ~ \.(jpg|jpeg|png|gif)$ { alias /usr/share/nginx/Note/assets/; } location / { root /usr/share/nginx/Note/note_by_js; index index.html; } access_log /var/log/nginx/note.log; } nginx error.log 2019/08/18 10:31:45 [error] 16320#0: *147710 "/usr/share/nginx/Note/note_by_js/笔记 /0 通识技术 /image-20190727104421457.png/index.html" is not found (2: No such file or directory), client: 219.143.150.68, server: note.tianyongwei.com, request: "GET /%E7%AC%94%E8%AE%B0/0%E9%80%9A%E8%AF%86%E6%8A%80%E6%9C%AF/image-20190727104421457.png/ HTTP/1.1", host: "note.tianyongwei.com" |
9
zw1one 2019-08-18 11:45:03 +08:00
location ~ .*\.(jpg|jpeg|png|gif)$ {
alias /usr/share/nginx/Note/assets/; } |
10
redbelt OP 算是弄出来了 , 奉上配置。
思路是重写 ,但最后用了 try_files , 其中区别暂不求甚解。 难点在匹配文件的正则 location ~ /([^/]*)\.(jpg|jpeg|png|gif)$ { try_files $uri $uri/ /assets/$1.$2; } ``` server { listen 80; server_name your.domain.com; # 这个正则的作用 # 1. 匹配图片文件,四种文件格式结尾 # 2. 第一个小括号确定能拿到文件名 location ~ /([^/]*)\.(jpg|jpeg|png|gif)$ { try_files $uri $uri/ /assets/$1.$2; } # 全站的图片文件都在「/usr/share/nginx/Note/assets 」这个文件夹中,没有子文件夹 location ^~ /assets/ { root /usr/share/nginx/Note; if (-f $request_filename) { expires 1h; break; } } location / { root /usr/share/nginx/Note/note_by_js; index index.html; } location ~ \.md$ { root /usr/share/nginx/Note/note_by_js; } access_log /var/log/nginx/note.log; } ``` |