暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【三分钟教程】docker快速部署nginx服务

看见月亮的人 2020-08-02
359
  • 一、docker部署nginx

    • 1.1 下载nginx镜像

    • 1.2 新建nginx挂载目录:

    • 1.3 编写nginx容器启动脚本

    • 1.4 为脚本赋予执行权限

    • 1.5编写nginx主配置文件

    • 1.6 编写nginx虚拟主机配置文件

  • 二、nginx配置

    • 2.1 nginx 容器日志切割

    • 2.2 nginx容器中安装工具


一、docker部署nginx

1.1 下载nginx镜像

[root@localhost ]# docker pull nginx:1.14
1.14: Pulling from library/nginx
27833a3ba0a5: Pull complete
0f23e58bd0b7: Pull complete
8ca774778e85: Pull complete
Digest: sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Status: Downloaded newer image for nginx:1.14

1.2 新建nginx挂载目录:

[root@localhost /]# mkdir -p mnt/public/nginx/{conf,html,logs,cert}
[root@localhost /]# tree mnt/public/
/mnt/public/
└── nginx
├── cert
├── conf
├── html
└── logs

5 directories, 0 files

1.3 编写nginx容器启动脚本

[root@localhost nginx]# pwd
/mnt/public/nginx

[root@localhost nginx]# vim run.sh

#!/bin/bash

docker run -itd --restart=unless-stopped \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
--network=host \
--name nginx \
-v $(pwd)/html:/usr/share/nginx/html \
-v $(pwd)/logs:/var/log/nginx \
-v $(pwd)/cert:/etc/nginx/cert \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf \
-v $(pwd)/conf:/etc/nginx/conf.d \
nginx:1.14

docker logs -f nginx

1.4 为脚本赋予执行权限

[root@localhost nginx]# chmod +x run.sh
[root@localhost nginx]# ll run.sh
-rwxr-xr-x 1 root root 434 8 02 10:44 run.sh

1.5编写nginx主配置文件

[root@localhost nginx]# pwd
/mnt/public/nginx


[root@localhost nginx]# vim nginx.conf

worker_processes 4;
user nginx;
#pid opt/app/nginx/sbin/nginx.pid;
events {
worker_connections 409600;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
keepalive_timeout 65;
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;
error_log var/log/nginx/error.log error;
include etc/nginx/conf.d/*.conf;
}

1.6 编写nginx虚拟主机配置文件

[root@localhost conf]# pwd
/mnt/public/nginx/conf
[root@localhost conf]# ls
nginx-443.conf.template nginx-php.conf.template nginx.template.conf


  • https域名conf文件配置

[root@localhost conf]# vim nginx-443.conf

server {
listen 80;
server_name 127.0.0.1;
rewrite ^ https://$http_host$request_uri? permanent;
server_tokens off;
}
server {
listen 443 ssl;
server_name 127.0.0.1;
ssl_certificate /etc/nginx/cert/xxx.com.pem;
ssl_certificate_key /etc/nginx/cert/xxx.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
charset utf-8;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
}

  • http域名conf文件配置

[root@localhost conf]# vim nginx.template.conf

server
{
listen 80;
server_name 127.0.0.1;
charset utf-8;
location {
root usr/share/nginx/html;
index index.php index.html index.htm;
}
}

  • 反向代理配置

server {
listen 80;
server_name 127.0.0.1;

location {
proxy_redirect off;
proxy_pass http://0.0.0.0/;
}
access_log var/log/nginx/access.log main;
}

  • php环境conf文件配置

[root@k8s-node2 conf]# vim nginx-php.conf.template 

server {
listen 80;
server_name 127.0.0.1;
location {
root usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~* .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log var/log/nginx/access.xxx.com.log main;

}

二、nginx配置

2.1 nginx 容器日志切割

注意,我们把 nginx 的日志绑定挂载到了当前目录下的/mnt/public/nginx/logs目录下。

  • 编写一个模拟产生连续的nginx日志脚本

[root@localhost nginx]# vim logs.sh

#!/bin/bash

for ((i=1;i<=100000;i++))
do
curl http://localhost:8081 > dev/null
sleep 1
done


然后运行这个脚本,就会连续产生nginx日志了

[root@localhost nginx]# ./logs.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 166 100 166 0 0 52 0 0:00:03 0:00:03 --:--:-- 52
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 166 100 166 0 0 85 0 0:00:01 0:00:01 --:--:-- 85

  • 创建滚动日志的脚本

[root@localhost nginx]# vim rotatelog.sh

#!/bin/bash
getdatestring()
{
TZ='Asia/Shanghai' date "+%Y-%m-%d-%H-%M"
}
datestring=$(getdatestring)
mv var/log/nginx/8081.log var/log/nginx/8081.${datestring}.log
mv var/log/nginx/8082.log var/log/nginx/8082.${datestring}.log
kill -USR1 `cat var/run/nginx.pid`


解释一下这个脚本

  • getdatestring 函数取当前的时间并格式化为字符串,比如 "2018-07-24-13-10",使用日期及时间来命名日志文件。可准确知道日志文件的切割时间。注意这里通过 TZ='Asia/Shanghai' 指定了时区,因为默认情况下格式化的是 UTC 时间

  • 其次是两条 mv 命令用来重命名日志文件

  • 最后一条命令通过 kill 命令向 nginx master 进程发送 USR1 信号。

通过下面的命令为 rotatelog.sh 文件添加可执行权限并复制到 mnt/public/nginx/logs 目录下:

[root@localhost nginx]# chmod +x rotatelog.sh
[root@localhost nginx]# mv rotatelog.sh mnt/public/nginx/logs

  • 定时执行滚动操作 我们的 nginx 运行在容器中,所以需要在容器中给 nginx master 进程发送 USR1 信号。因此我们需要通过 docker exec 命令在 mynginx 容器中执行 rotatelog.sh 脚本:

[root@localhost nginx]#  docker exec nginx bash var/log/nginx/rotatelog.sh


执行一次上面的命令,会如期产生一批新的日志文件:

[root@localhost logs]# pwd
/mnt/public/nginx/logs
[root@localhost logs]# ls
8081.2019-07-19-19-47.log 8081_json.log 8081.log 8082.2019-07-19-19-47.log 8082_json.log 8082.log access.log error.log rotatelog.sh

下面我们把这个命令配置在定时任务中,让它每天凌晨一点执行一次。执行 crontab -e 命令,并在文件的末尾添加下面的行:

[root@localhost logs]# crontab -e

# Nginx log cutting
0 1 * * * /usr/bin/docker exec nginx bash /var/log/nginx/rotatelog.sh

保存并退出就可以了。下图是笔者测试过程中每 5 分钟滚动一次的效果:

为什么不在宿主机中直接 mv 日志文件?

理论上这么做是可以的,因为通过绑定挂载的数据卷中的内容从宿主机上看和从容器中看都是一样的。但是真正这么做的时候你很可能碰到权限问题。在宿主机中,你一般使用的是普通用户,而在容器中产生的日志文件的所有者是会是特殊的用户,并且一般不会给其它用户写和执行的权限:

[root@localhost logs]# ll
总用量 40
-rw-r--r-- 1 101 root 0 7 19 19:46 8081.2019-07-19-19-47.log
-rw-r--r-- 1 101 root 0 7 19 19:47 8081.2019-07-19-19-51.log
-rw-r--r-- 1 101 root 12425 7 19 19:33 8081_json.log
-rw-r--r-- 1 101 root 0 7 19 19:51 8081.log
-rw-r--r-- 1 101 root 0 7 19 19:46 8082.2019-07-19-19-47.log
-rw-r--r-- 1 101 root 0 7 19 19:47 8082.2019-07-19-19-51.log
-rw-r--r-- 1 101 root 5198 7 19 19:34 8082_json.log
-rw-r--r-- 1 101 root 0 7 19 19:51 8082.log


当然,如果你在宿主机中使用的是 root 用户就不会有问题。

能从宿主机中发送的信号吗?

其实这个问题的全称应该是:能从宿主机中给 docker 容器中的 nginx master 进程发送信号吗?答案是,可以的。

2.2 nginx容器中安装工具

Nginx镜像太精简了,启动一个容器进行测试时,常用的网络工具都没有,可以使用下面的命令进行安装。

docker exec -it nginx bash
apt update

  • telnet

apt install telnet

  • ping

apt install inetutils-ping

  • nslookup

apt install dnsutils

  • ifconfig

apt install net-tools

  • ip

apt install iproute2

  • curl

apt install curl
文章转载自看见月亮的人,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论