使用docker部署nginx

拉取镜像

#拉取镜像
docker pull nginx

创建挂载地址

#创建挂载目录
mkdir -p /usr/local/docker/nginx/{conf,conf.d,www,logs}

创建nginx配置

#编辑配置
vi /usr/local/docker/nginx/conf/nginx.conf

配置信息


#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  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

}

编写测试页面

#编辑测试页面
vi /usr/local/docker/nginx/www/index.html

测试页面




	


	123123

运行镜像.

docker run -dti \
	--network=host \
	--privileged=true \
	--name nginx --restart always \
	--volume /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  \
	--volume /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d \
	--volume /usr/local/docker/nginx/logs:/var/log/nginx \
	--volume /usr/local/docker/nginx/www:/www \
	nginx:latest

说明:

  1. -d: 后台运行容器,并返回容器ID;
  2. -i: 以交互模式运行容器,通常与 -t 同时使用;
  3. -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
  4. --name="nginx-lb": 为容器指定一个名称;
  5. --network="host": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;因为nginx多个端口最好使用host无需进行端口映射
  6. --restart always: 自动重启
  7. --privileged=true 使得容器内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限
  8. --volume /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  将配置信息映射
  9.  --volume /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d  配置信息映射
  10.  --volume /usr/local/docker/nginx/logs:/var/log/nginx 日志信息映射
  11.  --volume /usr/local/docker/nginx/www:/www 此为自定义目录,存放静态文件使用

测试

访问对应ip打开页面,成功

使用docker部署nginx_第1张图片

 

你可能感兴趣的:(运维,centos,linux)