如何部署Docker Nginx?

Docker

1 基础知识

1.1 Nginx的简介

Nginx是一个高性能的HTTP和反向代理web服务器
Nginx同时也提供IMAP/POP3/SMTP服务
Nginx有伊戈尔.赛索耶夫为俄罗斯访问量第二的Rambler.ru站点开发
Nginx第一个公开版本0.1。0发布于2004年10月4日

1.2 Nginx的优点

Nginx可在大多数Unix或Linux OS上编译运行(有Windows移植版)
Nginx是一个强大的高性能Web和反向代理服务
Nginx可以支持50,000并发链接响应(以epool and kqueue作为开发模型)
Nginx基于C语言从头开发并拥有自己的函数库(系统库只使用C函数库)

2 最佳实践

2.1 部署Docker集群

如果你尚未部署Docker集群环境,请参阅如下章节部署,

如何部署Docker生产环境?

2.2 部署前的准备

In docker01

cd /data/docker/images/
docker load -i nginx_1.12.1.tar
docker tag nginx:1.12.1 docker01.cmdschool.org:5000/nginx:1.12.1
docker push docker01.cmdschool.org:5000/nginx:1.12.1
docker image rm nginx:1.12.1

另外,安装包的离线下载请在能上网的docker环境的机器上使用如下命令,

docker pull nginx:1.12.1
docker save nginx:1.12.1 -o nginx_1.12.1.tar

另外,其他版本请从以下链接下载,
https://hub.docker.com/_/nginx

2.2 部署nginx

In docker01

2.2.1 创建服务配置文件

vim /data/docker/yml/nginx-stack.yml

加入如下配置,

version: '3.8'

services:
  web:
    image: docker01.cmdschool.org:5000/nginx:1.12.1
    volumes:
      - /data/docker/service/ngixn/conf/nginx.conf:/etc/nginx/nginx.conf
      - /data/docker/service/ngixn/conf/conf.d:/etc/nginx/conf.d
      - /data/docker/service/ngixn/html:/usr/share/nginx/html
    ports:
      - 8080:80
    environment:
      - TZ=Asia/Shanghai
      - NGINX_HOST=foobar.com
      - NGINX_PORT=80
    deploy:
      replicas: 1
      placement:
        constraints: [node.labels.nginx == true]
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "5"

根据配置需求创建数据存放目录,
In docker01

mkdir -p /data/docker/service/ngixn/conf/conf.d
mkdir -p /data/docker/service/ngixn/html

根据配置创建主配置文件,

vim /data/docker/service/ngixn/conf/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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

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

根据主配置文件创建需要导入的配置,

vim /data/docker/service/ngixn/conf/conf.d/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;
    #}
}

根据子配置文件创建范例页面,

vim /data/docker/service/ngixn/html/index.html

加入如下配置,

<pre>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

另外,还需创建错误页面,

vim /data/docker/service/ngixn/html/50x.html

加入如下配置,

<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the <a href="http://nginx.org/r/error_log">error log</a> for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

根据配置的需求设置运行标记,
In docker01

docker node update --label-add nginx=true docker01.cmdschool.org

2.2.2 部署服务

docker stack deploy -c /data/docker/yml/nginx-stack.yml nginx

2.2.3 控制Nginx服务

In docker01

docker exec -it `docker container ls | grep 'nginx' | cut -d" " -f1 ` bash

以下脚本支持服务的控制,可使用如下命令获取帮助,

/etc/init.d/nginx

可见如下显示,

Usage: /etc/init.d/nginx {start|stop|status|restart|reload|force-reload|upgrade|configtest|check-reload}

尝试使用其中的服务状态查询(其他函数请自行测试,不一一演示),

/etc/init.d/nginx status

可见如下显示,

[ ok ] nginx is running.

2.2.4 访问测试页

http://10.168.0.210:8080/
如果能看到如下内容的页面则服务正常,

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.



For online documentation and support please refer to
nginx.org.

Commercial support is available at
nginx.com.



Thank you for using nginx.

参阅文档
=====================

官方参阅
————
https://hub.docker.com/_/nginx

文件兼容性参考
————–
https://docs.docker.com/compose/compose-file/

没有评论

发表回复

Docker
如何修复连接Docker容器虚机无响应?

1 前言 一个问题,一篇文章,一出故事。 笔者最近使用如下命令连接容器发现一直没有响应, docke …

Docker
如何打包Docker Nginx?

1 前言 一个问题,一篇文章,一出故事。 由于docker hub原版本的nginx镜像过于精简,于 …

Docker
如何打包Docker MAC-Telnet?

1 基础知识 一款可以使用MikroTik RouterOS MAC-Telnet协议连接Mikro …