前言
- nginx version: nginx/1.18.0
- CentOS Linux release 7.6.1810 (Core)
- ruoyi 4.6.0
需求
静态资源目录:/data/ruoyi/uploadPath
。
配置静态资源服务,且指向静态资源目录。
配置静态资源服务
# static resources
location /static/ {
alias /data/ruoyi/uploadPath/;
}
通过类似 http://test/static/upload/2021/04/15/6b24c663-1c04-4d0f-b608-8e7ba73c654f.jpg
这样的路径,可以访问到静态资源。
兼容ruoyi
在ruoyi中,上传功能返回两个路径,一个绝对路径(http开头),一个相对路径(/profile开头)。
相对路径为 /profile
+ 上传目录的相对路径
。比如 profile/upload/2021/04/15/6b24c663-1c04-4d0f-b608-8e7ba73c654f.jpg
对应的物理路径为/data/ruoyi/uploadPath/upload/2021/04/15/6b24c663-1c04-4d0f-b608-8e7ba73c654f.jpg
。
为了兼容ruoyi,改为如下配置;
# static resources
location /static/profile/ {
rewrite /static/profile/(.*) /static/$1 last;
}
location /static/ {
alias /data/ruoyi/uploadPath/;
}
http://test/static/upload/2021/04/15/6b24c663-1c04-4d0f-b608-8e7ba73c654f.jpg
、http://test/static/profile/upload/2021/04/15/6b24c663-1c04-4d0f-b608-8e7ba73c654f.jpg
指向同一个物理路径。
另一种兼容ruoyi的方式
# static resources
location ~* ^/static/profile/(.*)$ {
rewrite /static/profile/(.*) /static/$1 last;
}
location /static/ {
alias /data/ruoyi/uploadPath/;
}