用Nginx实现图片服务器,只需要映射对应路径。
如果使用Docker运行nginx容器,因为容器和容器间是相互独立,Docker要再一次实现映射。
为此踩坑无数,遂写下文章记录。


  1. 搜索nginx镜像
docker search nginx
  1. 拉取nginx镜像
docker pull nginx
  1. 创建容器,设置端口映射、目录映射
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
vim 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;

    include /etc/nginx/conf.d/*.conf;
}

以上是常规拉取nginx的方法

image-20211211201734559

image-20211211201756703

现在我们要实现nginx映射到/root/image

实现访问/root/image中的图片

image-20211211201924952

这是我们的nginx.conf

意思是访问101.34.23.97/picture/路径

就能访问图片

root /image/是映射路径(即在容器中图片存放的路径是image/picture)

因此我们在run nginx容器时要做映射: -v root/image(这是我的实际地址):image/picture(容器中的地址)

docker run -id --name=c_nginx -p 80:80 -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx -v $PWD/html:/usr/share/nginx/html -v /root/image:/image/picture nginx

pwd是当前路径的意思,比如我现在在root/nginx目录,那么pwd就是root/nginx

Last modification:December 11th, 2021 at 08:26 pm
如果觉得我的文章对你有用,请随意赞赏