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

Posted 看见月亮的人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三分钟教程docker快速部署nginx服务相关的知识,希望对你有一定的参考价值。

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

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

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 /mnt/public/nginx/html:/usr/share/nginx/html \\
 -v /mnt/public/nginx/logs:/var/log/nginx \\
 -v /mnt/public/nginx/cert:/etc/nginx/cert \\
 -v /mnt/public/nginx/nginx.conf:/etc/nginx/nginx.conf \\
 -v /mnt/public/nginx/conf:/etc/nginx/conf.d \\
 nginx:1.14 

docker logs -f nginx

4、为脚本赋予执行权限

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

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

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;

}

以上是关于三分钟教程docker快速部署nginx服务的主要内容,如果未能解决你的问题,请参考以下文章

通过 docker-compose 快速部署 Redis 保姆级教程

Docker Nginx SSL证书认证启动教程

云原生 | Docker腾讯云部署Django项目 (服务器选型git配置docker三分钟部署)

Docker-在Docker中部署Nginx实现负载均衡(视频教程)

一片非常有趣的文章 三分钟读懂TT猫分布式微服务和集群之路

快速在服务器上使用 Docker 安装部署各种常用数据库和软件(无bug版,10分钟完成配置和各类软件安装)