Centos7使用docker快速安装nginx及快速配置

一、拉取镜像及创建挂载文件

#拉取nginx镜像
docker pull registry.docker-cn.com/library/nginx
#用于挂载nginx web文件
mkdir -p /home/nginx/www
#用于挂载nginx配置文件
mkdir -p /home/nginx/conf
#用于挂载zookeeper日志文件
mkdir -p /home/nginx/logs
#修改image的tgs
docker tag registry.docker-cn.com/library/nginx nginx

二、启动容器

#先启动
docker run -p 9999:80 --name mynginx -d nginx
#拷贝配置文件
docker cp -a mynginx:/etc/nginx/ /home/nginx/conf
#修改配置文件
vi /home/nginx/conf/nginx/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;
    keepalive_timeout  65;
    upstream www_server_pools {
		server 47.105.108.195:15672 weight=1;
		server 47.105.108.195:15672 weight=1;
	}
	upstream bbs_server_pools {
		server 47.105.108.195:15672 weight=1;
		server 47.105.108.195:15672 weight=1;
    }

    include /etc/nginx/conf.d/*.conf;
}

vi /home/nginx/conf/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://www_server_pools;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://bbs_server_pools;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

#再启动
docker run -p 9999:80 --name mynginx -v /home/nginx/www:/www -v /home/nginx/conf/nginx:/etc/nginx -v /home/nginx/logs:/wwwlogs -d nginx
#-p 9999:80:将容器的80端口映射到主机的9999端口
#--name nginx:将容器命名为mynginx
#-v /home/nginx/www:/www:将主机中当前目录下的www挂载到容器的/www
#-v /home/nginx/conf:/etc/nginx:将主机中当前目录下的nginx.conf挂载到容的/etc/nginx/nginx.conf
#-v /home/nginx/logs:/wwwlogs:将主机中当前目录下的logs挂载到容器的/wwwlogs

三、防火墙设置

#编辑防火墙配置文件
vi /etc/sysconfig/iptables
-A IN_public_allow -p tcp -m tcp --dport 9999 -m conntrack --ctstate NEW -j ACCEPT
#重启防火墙
systemctl restart iptables

##如果是云服务需到云端配置安全组规则

你可能感兴趣的:(Docker)