使用docker部署nginx容器

来源:互联网 发布:cosplay服装淘宝 编辑:程序博客网 时间:2024/05/16 09:21

使用版本说明(系统版本:CentOS  3.10.0,docker版本:17.04.0-ce,nginx版本: nginx/1.13.5)。

第一步 :安装nginx容器,并将nginx配置文件挂载到宿主机

1.先创建一个临时的nginx容器:

docker run -d --name nginx  -p 82:80 -v /opt/local/nginx/:/etc/nginx nginx

-d 以守护进程运行(运行在后台);

--name nginx  容器名称;

-p 82:80  端口映射(先不要占用80端口,因为这个容器是临时的)

不需要提前下载nginx的镜像(除非你需要一个特定的版本),docker会自动下载最新版本。
运行完成后,使用docker ps查看容器是否开始运行。

2.将容器中的配置文件 复制到本地目录:  

docker cp nginx:/etc/nginx/ /opt/local

 如果不先将配置文件复制出来,在下一步中nginx配置文件不会挂载到宿主机,只会创建一个新目录。

目录文件不需要提前创建。

3.创建正式nginx容器: 

docker run -d --name mynginx  -p 80:80 -v /opt/local/nginx/logs:/var/log/nginx -v /opt/local/nginx:/etc/nginx nginx

-v 配置挂载路径 宿主机路径:容器内的路径

在这里挂载了两个 目录到 宿主机,一个是配置文件(etc/nginx),;另一个是日志文件(/var/log/nginx);

4.删除第一次创建的临时nginx容器

docker stop nginx  //先将容器停止运行docker rm nginx  //删除容器

以后可以直接进入/opt/local/nginx/对nginx进行配置,并查看日志。

可以直接进入 http:// [服务器ip] 访问,成功访问如下图所示。

成功访问页面

----------------------------------------------------------------------------------

路径说明:

nginx容器中nginx配置文件路径:/etc/nginx     

 nginx.conf:nginx常用配置项(其中没有server配置)

/etc/nginx/conf.d: 反向代理以及负载均衡的配置   

nginx容器中nginx日志文件路径:/opt/local/nginx/logs   

access.log:正常访问日志  

error.log:错误日志(在配置nginx时,如果出错可以在这里找原因)

主机中nginx配置文件路径:/opt/local/nginx
主机中nginx日志文件路径:/opt/local/nginx/logs
----------------------------------------------------------------------------------
容器时间与主机时间相差8个时区解决办法:
在安装完nginx容器后发现,容器时间与主机时间相差了 8个时区,导致nginx访问日志时间出错。
解决办法:使用主机中的时区文件替换容器中的文件  
docker cp /etc/localtime mynginx:/etc/localtime

----------------------------------------------------------------------------------

第二步:配置一个简单的反向代理

    我先在docker中新建了一个tomcat容器作为后台服务,访问主机端口是6004。

docker run -d --name tomcat -p 6004:8080 tomcat

    进入../nginx/conf.d,其中有一个 默认的server配置文件:default.conf

    当需要添加反向代理时,我一般会复制default.conf

cp default.conf my.conf
    这样 my.conf中就会存在默认的server配置。

   注意:因为 default.conf 里面也是一个server配置,它会默认监听80端口与 my.conf 中的冲突,可能会在访问时页面显示错误,所以需要修改 default.conf 监听端口。如果 my.conf 监听其他端口那就没有影响。

   my.conf 配置文件如下:

server {    listen       80;     #监听80端口    server_name  [ip];   #设置基于域名的虚拟主机    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;    location / {       proxy_pass http://[ip]:6004;  # 实际请求后台地址    }    # 下面这些都不动    #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;    #}}
配置完成以后,记得要重启nginx容器,否则配置不会生效。
docker restart mynginx
   可以访问页面测试了:http: [服务器ip]

第三步:使用nginx做一个简单的负载均衡

    首先还需要在docker中创建一个tomcat 容器,此时我们有两台tomcat容器,访问主机端口是6005,不过需要修改tomcat默认页面,不然两个tomcat 显示页面相同,看不出效果。
    my.conf配置文件如下(我直接修改第二步的配置文件,也可以重新创建一个*.conf,不过注意端口不要冲突):
upstream tomcat_test {    server [主机ip]:6004 weight=1;#weight:设置的是轮询权重,其他权重配置自行百度    server [主机ip]:6005 weight=1;}server {    listen       80;    server_name  [ip];    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;    location / {        proxy_pass http://tomcat_test;    }    #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;    #}}
   由于nginx的版本问题,server的配置位置可能不相同,有的直接在nginx.conf里面就可以配置,当然我的这个版本也可以在nginx.conf中配置

附录:  

    1.默认default.confnginx.conf配置文件。

default.conf配置文件:

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

nginx.conf配置文件

user  nginx;         # Nginx用户及组:用户 组。window下不指定worker_processes  1; #工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。error_log  /var/log/nginx/error.log warn;  # 错误日志路径pid        /var/run/nginx.pid;   # 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;     # sendfile指令指定 nginx 是否调用sendfile     #tcp_nopush     on;     # 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用    keepalive_timeout  65;  # 超时时间。    #gzip  on;    include /etc/nginx/conf.d/*.conf; # 代理配置文件}
    2.nginx安装配置中使用到的一些小点

  • 进入容器  :docker-enter mynginx 注意空格
  • 查找文件  :find / -name nginx
  • 如果docker 安装 nginx出错,可以查看docker日志文件, 

docker logs -f -t --since="2017-10-17" --tail=10 mynginx  

--tail = 10 : 读取最后10条记录

--since : 哪天的日志

mynginx : 容器名

原创粉丝点击