Featured image of post Docker-Run命令部署Nginx

Docker-Run命令部署Nginx

推荐文章:

【合集】Docker

Nginx配置Https免费证书

docker-run命令启动

第一次运行容器,目的:复制nginx里面的配置目录和html目录到宿主机

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
docker rm -f nginx
docker run --name nginx \
        -p 80:80 \
        -p 443:443 \
        -v ~/nginx/logs:/var/log/nginx  \
        --restart always \
        -d nginx

# 复制nginx目录到config
docker container cp nginx:/etc/nginx ~/nginx/config
# 复制html目录到html
docker container cp nginx:/usr/share/nginx/html ~/nginx/html

删除后重启,并挂载配置容器卷

1
2
3
4
5
6
7
8
9
docker rm -f nginx
docker run --name nginx \
        -p 80:80 \
        -p 443:443 \
        -v ~/nginx/html:/usr/share/nginx/html \
        -v ~/nginx/logs:/var/log/nginx  \
        -v ~/nginx/config:/etc/nginx \
        --restart always \
        -d nginx

60.44.3.223 访问nginx(记得服务器安全组80端口打开)

img_196.png

上传网站文件,配置 nginx.conf

将要部署的网站文件夹,放到html文件夹下面

img_193.png

将下面server覆写到default.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen       80;
    # 注意:root对应的是nginx容器内部的目录,而不是数据卷的路径
    root   /usr/share/nginx/html/zxalive/public;
    index index.html index.htm index.php;
    server_name  localhost;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/zxalive/public;
    }
}

img_197.png

docker restart nginx 重启nginx容器后,再次ip访问234.44.3.223,成功

img_194.png

其他项目的default.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
server
   {
    listen 80;
    server_name 116.62.6.243;
    index index.html index.htm index.php;
    root   /usr/share/nginx/html/dist-pc;

    location / {
        if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL\-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" ) {
            root   /usr/share/nginx/html/dist;
        }
    }

    location /admin {
        alias   /usr/share/nginx/html/dist-admin;
    }
}
皖ICP备2024056275号-1
发表了78篇文章 · 总计149.56k字
本站已稳定运行