Docker 中安装 Nginx

  1. 在/data目录下新建一个html目录,用于网站的根目录
    mkdir -p /data/html
  2. 为了配置方便,我们把docker中的nginx配置文件映射出来
    在本地创建一个nginx.conf文件
    mkdir -p /data/nginx
    vim /data/nginx/nginx.conf
  3. 输入简单的配置
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  1800;

    gzip            on;
    gzip_min_length 1000;
    gzip_proxied    off;
    gzip_types      text/plain text/css text/js application/javascript;

    server {
       # 监听80端口
        listen       80;
        server_name  localhost;
        index   index.html;

        location / {
            # 根目录为docker中的目录,这个在启动时配置参数映射到主机上
            root   /usr/local/html/;
            add_header 'Cache-Control' 'no-store';
        }
    }
}
  1. 在docker中启动nginx
    docker run --name nginx -v /data/html:/usr/local/html -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf:ro --net=host -d nginx:1.10.1-alpine
  2. 查看状态
    docker ps
    CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
    4f7e11811ae6        nginx:1.10.1-alpine   "nginx -g 'daemon of…"   48 seconds ago      Up 47 seconds                           nginx
  3. 等待下载完成后,设置随docker启动而启动
    docker update --restart=always nginx
0%