[图片上传中。。。(1)]> docker 版本 :
[root@repositories]# docker -v
Docker version 1.12.1, build 23cf638
目的:通过nginx访问图片地址
制作镜像的目录:
Dockerfile内容:
FROM centos
MAINTAINER chances-gaoyx
VOLUME [ "/sys/fs/cgroup" ]
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN /bin/echo -e "ZONE="Asia/Shanghai"\nUTC=false\nRTC=false" > /etc/sysconfig/clock
RUN yum -y install epel-release; yum clean all; yum makecache ; yum -y install nginx
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
ADD ./default.jpg /usr/share/nginx/html/
ADD ./nginx.conf /etc/nginx/
RUN mkdir -p /usr/share/nginx/logs
VOLUME [ "/opt/product/data/" ]
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
nginx.conf 配置:
##user nobody;
worker_processes 12;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
http{
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
access_log off;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 25;
types_hash_max_size 2048;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
open_file_cache max=10000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
limit_rate_after 2m;
limit_rate 512k;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index /default.jpg;
location /pics {
alias /opt/product/data/cms-picture;
#autoindex on;
proxy_redirect off;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_buffer_size 40k;
proxy_buffers 40 320k;
proxy_busy_buffers_size 640k;
proxy_temp_file_write_size 640k;
}
location /apk {
root /data/ncyd/;
#alias ;
autoindex on;
proxy_redirect off;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_connect_timeout 10;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffer_size 40k;
proxy_buffers 40 320k;
proxy_busy_buffers_size 640k;
proxy_temp_file_write_size 640k;
}
error_page 404 500 502 503 504 /default.jpg;
location /nginx_status {
stub_status on;
access_log off;
}
}
}
制作镜像:
docker build -t nginx-gaoyx:1.0 ./
启动镜像:
docker run --name=$HOSTNAME-nginx --restart=always -d -ti -p 60001:80 -v /opt/test/gaoyx/data:/opt/product/data c8af
访问图片:
前提: nginx中配置了图片地址是/opt/product/data/cms-picture ,我们把本地目录 /opt/test/gaoyx/data 挂载到了 /opt/product/data 中,所以我们要把图片文件夹 cms-picture 放在本地目录 /opt/test/gaoyx/data 下面,路径是这样的: /opt/test/gaoyx/data/cms-picture/20170405/a.png
启动镜像后,在浏览器访问图片地址:
主页我们也改了默认图片,见配置index /default.jpg;
,所以访问主页是显示这样的:
ok,截止到目前 Nginx 图片服务器相关的就结束了。
加一个有关nginx配置的信息:
nginx 的文件路径名 和 location 块名配置找到理论依据啦,用root 定义路径的话是拼接 /块名 为整个路径去访问资源;用alias 定义路径的时候,会把/块名 丢弃 再拼接后面的地址 为访问路径。所以当整个资源的路径不包含块名的时候用alias 而不是用 root 定义。