docker部署nginx

拉取Nginx进行
docker pull nginx:latest 

拉取完成查看:docker images

准备工作
创建docker目录环境

先在主机创建工作文件夹,为了挂载配置和静态文件的访问使用

#启动一个容器

docker run -d --name nginx nginx
# 查看 容器 获取容器ID 或直接使用名字
docker container ls
# 在当前目录下创建目录:conf 
sudo mkdir -p /docker/test/nginx/conf
sudo chown $USER.$USER -R /docker/test/nginx
# 拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录($PWD 当前全路径)
docker cp nginx:/etc/nginx/nginx.conf /docker/test/nginx/conf
docker cp nginx:/etc/nginx/conf.d /docker/test/nginx/conf
rm -rf /docker/test/nginx/conf/conf.d/*
# 停止容器
docker container stop nginx
# 删除容器
docker container rm nginx

# 在当前目录下创建目录:html 放静态文件
mkdir -p /docker/test/nginx/html
nginx配置

nginx default配置

vim /docker/test/nginx/conf/nginx.conf

配置文件

#user  nobody;
worker_processes  2;

#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  65;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    send_timeout 200;
    #gzip  on;

    map $http_x_forwarded_for $clientRealIp {
      "" $remote_addr;
      ~^(?P[0-9\.]+),?.*$ $firstAddr;
    }


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include conf.d/*.conf;
}
域名 nginx配置
vim /docker/test/nginx/conf/conf.d/test.conf

conf文件

upstream www.test.com { 
#      hash $clientRealIp;
      server 192.168.0.111:9999 weight=10;
}

server {
        listen 80;
        server_name 192.168.0.111;
        location / {
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://www.test.com;
            client_max_body_size 1024M;
            client_body_buffer_size 1024M;
            proxy_connect_timeout 200;
            proxy_send_timeout 200;
            proxy_read_timeout 200;
            proxy_buffer_size 4k;
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
        }

}
部署容器
docker stop nginx
docker rm nginx
docker run -d -p 80:80  \
              -p 443:443  \
 --name nginx \
 -v /docker/test/nginx/html:/usr/share/nginx/html \
 -v /docker/test/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
 -v /docker/test/nginx/conf/conf.d:/etc/nginx/conf.d \
 -v /docker/test/nginx/logs:/var/log/nginx \
 nginx 

-d # 表示在一直在后台运行容器

-p 80:80 # 对端口进行映射,将本地8081端口映射到容器内部的80端口

–name # 设置创建的容器名称 -v # 将本地目录(文件)挂载到容器指定目录;

–link answer-server:answerserver 这计划是指需要转向本机docker容器的别名

你可能感兴趣的:(docker,docker,nginx,运维)