天天看點

docker中使用nginx,實作檔案下載下傳功能Docker容器部署 Nginx服務

Docker容器部署 Nginx服務

首先需要先在自己的Docker中,下載下傳Nginx鏡像。

docker pull nginx
           

配置Nginx

第一步,生成主控端的檔案夾以及主控端的配置檔案

mkdir -p /home/nginx /home/nginx/sharefiles && vim /home/nginx/nginx.conf
           

第二步,修改nginx.conf 配置檔案

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
	
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
	
    include /etc/nginx/conf.d/*.conf;
    server {
        listen 80;
        server_name  _;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        limit_rate 50k;# 限制下載下傳速度
        location / {
            root    /usr/share/nginx/html/download;
        autoindex on;    #開啟索引功能
        autoindex_exact_size off;  #關閉計算檔案确切大小(機關bytes),隻顯示大概大小(機關kb、mb、gb)
        autoindex_localtime on;   #顯示本機時間而非 GMT 時間
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
           

第三步、啟動Nginx的Docker容器

docker run -d --name nginx -p 80:80 -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/sharefiles:/usr/share/nginx/html/download -v /data/nginx/conf.d:/etc/nginx/conf.d nginx
           

參數說明

  • –name:指定容器的名稱,容器名稱不能重複
  • -v:挂載資料卷,主控端路徑:容器路徑
  • -p:指定映射端口,主控端端口:容器端口
  • -d:指定容器背景運作

    運作成功後,通過腳本,檢視自己的容器是否啟動。

docker ps # 檢視運作中的容器
或者
docker ps -a # 檢視所有的容器
           

啟動成功後,隻要輸入對應的ip:80 即可

docker中使用nginx,實作檔案下載下傳功能Docker容器部署 Nginx服務

繼續閱讀