Docker部署前端nginx

作者: pdnbplus | 发布时间: 2024/06/17 | 阅读量: 181

Docker部署前端nginx -- 潘登同学的Linux笔记

先在宿主机下创建目录

# 因为我之前有 root/project
mkdir project/wordle_project
mkdir project/wordle_project/conf
mkdir project/wordle_project/conf.d
mkdir project/wordle_project/html
mkdir project/wordle_project/log

再配置相关文件

进入conf文件夹下

touch nginx.conf
vim nginx.conf
  • 写入以下内容,i写入 ESC+:wq保存并退出
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;
}

进入conf.d文件夹下

touch default.conf
vim default.conf
  • 写入以下内容,i写入 ESC+:wq保存并退出
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;
    # }
}

把相关文件扔到html文件夹下

可以通过xftp 在这里插入图片描述

启动nginx

docker run -it -d --name=wordle -p 8080:80 \
-v /root/project/wordle_project/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /root/project/wordle_project/log:/var/log/nginx \
-v /root/project/wordle_project/conf.d:/etc/nginx/conf.d \
-v /root/project/wordle_project/html:/usr/share/nginx/html nginx

最后打开124.221.109.222:8080 发现!!

在这里插入图片描述

报错解决

除去防火墙之类的错误,一般执行完docker run xxx之后都会返回一个容器ID;如果返回了容器ID,但是执行以下语句后,没看到nginx在后台跑

docker ps

如果这样,一般出现的问题就是前面的配置文件写漏了,进入nginx日志查看报错

cd root/project/wordle_project/log
vim error.log

根据提示解决即可