Docker部署nginx

1,675 阅读2分钟

前言

在现代软件开发和部署过程中,容器化技术已经成为一种主流选择。Docker 和 Docker Compose 提供了一种简单且高效的方法来管理和部署应用程序。本文将介绍如何通过 docker-compose 快速部署 Nginx 服务器。我们将详细介绍每一步,包括创建必要的目录和文件,以确保 Nginx 能够顺利运行。

步骤

  1. 新增docker-compose.yaml文件

    tee docker-compose.yml <<'EOF'
    version: '3' #指定版本
    services:    #服务根节点
      nginx:   #jenkins服务/其他服务(web服务/nginx服务等)
        image: nginx:1.26.3  #nginx镜像,如果镜像容器没有会去自动拉取
        container_name: nginx       #容器的名称
        restart: always             #跟随docker的启动而启动
        volumes:                    #挂载卷命令
          - ./conf/nginx.conf:/etc/nginx/nginx.conf              #映射配置文件入口文件
          - ./html:/usr/share/nginx/html                         #静态资源根目录挂载
          - ./logs:/var/log/nginx                                #日志文件挂载
          - ./conf.d:/etc/nginx/conf.d #映射配置文件
        ports:
          - 80:80    #宿主主机端口80 映射到 容器端口80
    EOF
    
  2. 创建初始目录

    mkdir -p ./{conf,logs,html,conf.d}
    
  3. 准备初始文件

    执行下面命令,创建./conf/nginx.conf文件

    tee ./conf/nginx.conf <<'EOF'
    
    user  root;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log notice;
    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;
    }
    EOF
    

    执行下面命令,创建./html/index.html文件

    tee ./html/index.html <<'EOF'
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
    html { color-scheme: light dark; }
    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>
    EOF
    

    执行下面命令,创建./html/50x.html文件

    tee ./html/50x.html <<'EOF'
    <!DOCTYPE html>
    <html>
    <head>
    <title>Error</title>
    <style>
    html { color-scheme: light dark; }
    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 error log for details.</p>
    <p><em>Faithfully yours, nginx.</em></p>
    </body>
    </html>
    EOF
    

    执行下面命令,创建./conf.d/default.conf文件

    tee ./conf.d/default.conf <<'EOF'
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;
    
        #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;
        #}
    }
    EOF
    
  4. 启动容器

    docker-compose up -d
    

结语

通过本文的步骤,你应该已经成功地使用 docker-compose 部署了一个 Nginx 服务器。这种方法不仅简化了部署过程,还提高了配置的可移植性和可维护性。你可以根据需要进一步定制 Nginx 配置文件,以满足特定的应用需求。希望这篇文章对你有所帮助,祝你在使用 Docker 和 Nginx 的过程中一切顺利!