小白学习docker搭建nginx代理图片服务器和文件服务器

369 阅读2分钟

1、拉取nginx

docker pull nginx
docker tag nginx nginx:v1

2、创建并运行容器

docker run -p 9093:80 --name nginx -d nginx:v1

3、进入容器

docker exec -it nginx /bin/bash

4、在容器的var目录下,并创建images目录和files目录

mkdir images/html
mkdir files/html

5、基于该容器打包新的nginx镜像

docker stop nginx
docker commit nginx nginx:v2 // docker commit 容器名 新的镜像名

6、在主机的var目录下创建nginx、images、files目录,并创建对应文件

mkdir images/html/pics
mkdir iamges/html/videos
mkdir files/html/txt
mkdir files/html/pdf
mkdir files/html/office
mkdir nginx/conf/conf.d
cd conf
touch nginx.conf
cd conf.d
touch default.conf
mkdir nginx/log

default.conf:下面的注释方式不对,创建的时候去掉

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    // 这边已经将容器中的/var/images/html目录挂载到了主机中的/var/images/html目录中
    // location /pics 意思是还需要在主机中的/var/images/html目录下创建pics目录
    // 并且将图片存放在pics目录下
    // location的意思就是root代表的目录下的某个目录
    location /pics {
        root   /var/images/html;
    }
    
    // 文件服务器
    location /txt {
        root /var/files/html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

nginx.conf:

user  root;
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;
}

7、使用新镜像创建nginx容器,并将nginx容器的日志和配置文件挂载到主机中var/nginx目录下

// nginx容器代理配置文件/etc/nginx/conf.d/default.conf
// nginx容器配置文件/etc/nginx/nginx.conf
docker run -p 9093:80 --name nginx -v /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /var/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /var/nginx/log:/var/log/nginx -v /var/images/html:/var/images/html -v /var/files/html:/var/files/html -d nginx:v2

配置成功后,在浏览器访问:http://192.168.244.134:9093/pics/1.jpeg,可以看到:

image.png 访问http://192.168.244.134:9093/txt/1.txt, 出现了乱码

image.png 乱码解决办法,在/var/nginx/conf/conf.d/default.conf里加上如下代码:

// 添加在location /txt中
charset 'utf-8';

8、配置新的文件服务器(比如视频资源),只需要在本机的var目录下nginx的default.conf进行配置新的资源地址,停止容器,再重启,就可以了。

location /videos {
    root /var/images/html;
}