参考
nginx+docker搭建静态资源服务器
基于springboot搭建图片服务器
问题的抛出
就拿优快云来说,通常我们将本地的的照片托进去就会返回一个属于这个图片的链接,然而其他人可以通过这个链接访问到我从本地拖进去的图片。

经典案例
头像的上传,从本地上传的图片,那么下次登录也能访问到。
Nginx+Docker 搭建静态资源服务器
安装docker
yum -y install docker
拉取nginx镜像
docker pull nginx
准备工作
# 创建一个存放静态资源的目录,比如我是使用了 /home/ftpadmin/health/images( 建议不要使用 root 权限,不好使用 xftp 上传文件 )
mkdir /home/ftpadmin/health/images
# 创建并编辑 nginx.conf,我的 nginx.conf 放在 /home/ftpadmin/health 路径底下
vi nginx.conf
可以在Xftp下创建,最后可以看到是这样的

修改nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /images;
index index.html index.htm;
}
}
include /etc/nginx/conf.d/*.conf;
}
执行docker实例创建命令
# --name 实例名
# -p 是端口映射,80是 nginx 容器的默认端口,映射到主机的 4545 端口,也就是访问宿主机的 4545 端口,就会自动跳转到 nginx 容器的 80 端口,也就是 nginx 的入口
# -v 是挂载目录,它会让容器和宿主机共享目录,我们把静态资源放在 /home/ftpadmin/health/images,容器内的路径 /images 也会更新静态资源,记得改成自己的真实路径
# 由于容器内没有 vim 和 vi ,甚至没有 yum ,所以我们无法在容器内修改,那么可以选择 将外部的 nginx 配置文件覆盖容器内的配置文件
挂载命令
docker run --name images_nginx -p 4545:80 -v /home/ftpadmin/health/images:/images -v /home/ftpadmin/health/nginx.conf:/etc/nginx/nginx.conf -d nginx
可以用Xftp上传几个图片

可以测试一下,图片是否可以访问啦
http://ip:4545/image.png

基于Springboot搭建图片服务器
nginx已经准备完成啦,那么现在我们要开始实现资源的上传
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



