使用 nginx+ docker搭建靜態資源服務器

nginx docker容器 作爲靜態資源服務器

安裝 docker

yum -y install docker

拉取 nginx 鏡像

docker pull nginx

準備工作

# 創建一個存放靜態資源的目錄,比如我是使用了 /home/zyy/images( 建議不要使用 root 權限,不好使用 xftp 上傳文件 )
mkdir /home/zyy/images
# 創建並編輯 nginx.conf,我的 nginx.conf 放在 /home/zyy 路徑底下
vi nginx.conf

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/zyy/images,容器內的路徑 /images 也會更新靜態資源,記得改成自己的真實路徑
# 由於容器內沒有 vim 和 vi ,甚至沒有 yum ,所以我們無法在容器內修改,那麼可以選擇 將外部的 nginx 配置文件覆蓋容器內的配置文件
docker run --name images_nginx -p 4545:80 -v /home/zyy/images:/images -v /home/zyy/nginx.conf:/etc/nginx/nginx.conf -d nginx

最後通過 url 可以訪問你的靜態資源

http://ip:4545/image.png
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章