Docker 安装 nginx

Docker 安装 nginx

通过命令方式进行安装

拉取镜像

docker pull nginx

创建待挂载的目录

sudo mkdir -p /data/nginx/{conf,conf.d,html,logs}

创建配置文件

#启动临时的nginx
docker run -d --name nginx nginx
#拷贝配置文件
sudo docker cp nginx:/etc/nginx/conf.d/default.conf conf.d/
#删除临时的nginx
docker stop nginx
docker rm nginx

启动nginx

docker run --name nginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d  nginx

启动方式添加 docker-compose 的方式

version: "3"

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
      - 9001:9001
    volumes:                                
      - /data/nginx/html:/usr/share/nginx/html
      - /data/nginx/conf.d:/etc/nginx/conf.d
      - /opt/product:/usr/share/nginx/product
      - /data/nginx/logs:/var/log/nginx
配置文件参考
server {
    listen       80;
    server_name  localhost;

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

#    location / {
#        root   /usr/share/nginx/html;
#        index  index.html index.htm;
#    }

   location /farming {
        root   /usr/share/nginx/html/;
	autoindex on;
        index  index.html index.htm;
    }


    #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;
    #}
}
server {
       listen       9001;    ## 该端口为storage.conf中的http.server_port相同
       server_name  localhost;

       real_ip_header X-Forwarded-For;
       real_ip_recursive on;

       location / {
	  root	/usr/share/nginx/xx;
          index  index.html index.htm;
	  autoindex	on;
       }

    }



原文地址:https://www.cnblogs.com/zhengqun/p/11573831.html