Docker中配置nginx服务

125 阅读2分钟

一、安装Nginx环境

1、docker安装自不用说,

运行 docker version

[root@hnnd-devsvr ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:49:57 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

如果有版本号如上,表示docker环境安装正常了。

2、查找nginx可用镜像列表:

docker search nginx


[root@hnnd-devsvr ~]# docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        16754     [OK]       
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS165                  
bitnami/nginx                                     Bitnami nginx Docker Image                      125                  [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   48                   
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr18                   [OK]
rancher/nginx-ingress-controller                                                                  10                   
ibmcom/nginx-ingress-controller                   Docker Image for IBM Cloud Private-CE (Commu4                    
clearlinux/nginx                                  Nginx reverse proxy server with the benefits…   4                 

比如我看到 nginx 这个镜像使用的STARS最多,要相信群众,俺就用这个镜像啦!

3、拉取镜像:

docker pull nginx

4、运行镜像:

docker run -d -p 80:80 --name my_nginx nginx

5、使用浏览器或curl工具测试80端口,应该完全正常了。

二、配置本地运行

1、本地创建目录:

mkdir -p /data/nginx/{conf.d,html,logs}

2、创建nginx配置文件1:

vim /data/nginx/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;
}

3、创建nginx配置文件2:

vim /data/nginx/conf.d/default.conf
server {
  listen 8080;
  server_name localhost;

  #access_log /var/log/nginx/log/host.access.log main;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    autoindex on;
    try_files $uri /index/index/page.html;
    #try_files $uri /index/map/page.html;
  }

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

注意:这里的 root 路径不要修改,这是镜像中的路径,不是你本机(宿主)上的路径。

本配置文件中,我们启动用 8080 端口。

4、把你的HTML文件,如前端打包文件复制到 /data/nginx/html 目录下

5、开始启动了,通过参数把你本机(宿主)的路径挂载到docker容器环境中

docker run --name nginx-focean -d -p 8080:8080
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf
-v /data/nginx/conf.d:/etc/nginx/conf.d
-v /data/nginx/logs:/var/log/nginx
-v /data/nginx/html:/usr/share/nginx/html
nginx

三、维护 nginx 环境

1、修改 default.conf 后,如何运行 nginx -s reload 呢?

docker exec -it nginx-focean nginx -s reload

2、如何查看容器内的文件?

docker ps

把列表中 nginx-focean 的容器ID 24b86b33ef43 抄到如下语句中:

docker exec -it 24b86b33ef43 /bin/bash

此时通过 ls 等命令,可以看到容器内有 /etc/nginx 的相关目录和配置文件,还有 /usr/share/nginx/html 等。