nginx 的安装

mkdir /usr/local/docker
cd /usr/local/docker
docker run --name nginxtest -d nginx #运行一个测试的nginx
docker cp nginxtest:/etc/nginx ./ #把容器里的nginx的目录复制
docker rm -f nginxtest #删除测试的nginx-test

复制出来的nginx目录结构

#复制出来的nginx目录结构
[root@localhost docker]# tree nginx
nginx
├── conf.d
│   └── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── logs
│   ├── access.log
│   ├── error.log
│   ├── gin.haimait.com.access.log
│   ├── gin.haimait.com.error.log
│   ├── localhost.access.log
│   └── localhost.error.log
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

运行mynginx容器

#运行mynginx 挂载到/wwwroot是为了保持容器和宿主机目录一致,以免配置时出错

docker run  \
--name mynginx \
-p 8081:80 \
-p 443:443 \
-v /etc/localtime:/etc/localtime:ro \
-v /wwwroot:/wwwroot \
-v $PWD/nginx:/etc/nginx \
-v $PWD/nginx/logs:/var/log/nginx \
--restart always \
-d nginx

编写默认的配置文件

vim $PWD/nginx/conf.d/default.conf

把内容换下面的内容

server {

    listen       80;

    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/localhost.error.log; # 配置前端静态文件目录 location / {
root /wwwroot/html;
index index.html index.htm index.php;
} #location ~ \.php$ {
# fastcgi_pass myphp73-fpm:9000; #myphp73-fpm容器的名字
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#} # 配置后台go服务api接口服务 代理到8877端口
#location ~ ^/goadminapi/ {
# proxy_set_header Host $http_host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
# rewrite ^/goadminapi/(.*)$ /$1 break;
# proxy_pass http://127.0.0.1:8877;
# }
}

配置一个域名也指向到/wwwroot/html目录 确定你的域名已经解析到服务器的ip上了

vim $PWD/nginx/conf.d/gin.haimait.com.conf

内容如下:

server {

    listen       80;

    server_name  gin.haimait.com;

    access_log  /var/log/nginx/gin.haimait.com.access.log;
error_log /var/log/nginx/gin.haimait.com.error.log; # 配置前端静态文件目录
location / {
root /wwwroot/html;
index index.html index.htm index.php;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} #location ~ \.php$ {
# fastcgi_pass myphp73-fpm:9000; #myphp73-fpm容器的名字
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#} # 配置后台go服务api接口服务 代理到8877端口
#location ~ ^/goadminapi/ {
# proxy_set_header Host $http_host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
# rewrite ^/goadminapi/(.*)$ /$1 break;
# proxy_pass http://127.0.0.1:8877;
# } }

重启nginx

docker restart mynginx

新建index.html

mkdir /wwwroot/html
vim /wwwroot/html/index.html

内容如下:

hello nginx , html

测试

以下说明都已经配置成功了

[root@localhost ~]# curl localhost:8081
hello nginx , html
[root@localhost ~]# curl gin.haimait.com:8081
hello nginx , html

浏览器访问

删除nginx容器重启容器,wwwroot里的内容也不会丢

到此已经完成安装

设置开机自动启动容器

在运行docker容器时可以加如下参数来保证每次docker服务重启后容器也自动重启:

docker run --restart=always CONTAINER ID

如果已经启动了则可以使用如下命令:

docker update --restart=always CONTAINER ID #设置启动nginx时自动启动

docker update --restart=no CONTAINER ID #设置启动nginx时不启动

[root@localhost www]# docker update --restart=always nginx
nginx

参考文档:

docker 安装nginx 配置目录挂载的更多相关文章

  1. docker安装MySQL8,目录挂载、配置用户名密码、忽略表名大小写、连接数、特殊字符、时区

    原文:docker安装MySQL8,目录挂载.配置用户名密码.忽略表名大小写.连接数.特殊字符.时区 一.环境配置 1.系统:centos7.3 2.docker版本:Docker version 1 ...

  2. Linux docker 安装nginx 配置ssl证书

    Linux docker 安装nginx 配置ssl证书 如果觉得样式不好:跳转即可 md文件复制过来有些样式会不一样) 原文地址:https://lifengying.site/archives/b ...

  3. 【转载】Docker 安装 Nginx 并个性化挂载配置文件 nginx.conf

    首先,系统(3.8以上内核的linux)中安装好 Docker 这个运用程序.由于网络原因,我们下载一个Docker官方的镜像需要很长的时间,甚至下载失败.为此,阿里云容器镜像服务提供了官方的镜像站点 ...

  4. docker安装nginx并配置通过https访问

    1. 下载最新的nginx的docker image docker pull nginx:latest 创建挂载路径 2.准备nginx需要的文件 nginx的配置文件 首先是nginx.conf文件 ...

  5. docker安装和配置nginx

    配置nginx docker配置nginx 本机ip是192.168.0.200 docker pull nginx 配置nginx主机 vi /root/docker/nginx/nginx01.c ...

  6. 阿里云CentOS7.3服务器通过Docker安装Nginx

    前言 小编环境: 阿里云CentOS7.3服务器 docker 下面分享一次小编在自己的阿里云CentOS7.3服务器上使用Docker来安装Nginx的一次全过程 温馨小提示: 如果只是希望单纯使用 ...

  7. Ubuntu Docker 安装和配置 GitLab CI 持续集成

    相关文章: Ubuntu Docker 简单安装 GitLab 劈荆斩棘:Gitlab 部署 CI 持续集成 目的:在 Ubuntu 服务器上,使用 Docker 安装和配置 GitLab Runne ...

  8. Linux、Docker安装Nginx

    Docker安装Nginx #docker images nginx #docker search nginx #docker pull nginx #docker run -it -p 8084:8 ...

  9. centos7下使用docker安装nginx

    需要环境docker,此处不做介绍. 1. docker拉取官方nginx镜像 docker pull nginx 2. 等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 ng ...

  10. Docker实战 | 第三篇:Docker安装Nginx,实现基于vue-element-admin框架构建的项目线上部署

    一. 前言 在上一文中 点击跳转 通过IDEA集成Docker插件实现微服务的一键部署,但 youlai-mall 是前后端分离的项目,除了后端微服务的部署之外,当然还少不了前端工程的部署.所以本篇讲 ...

随机推荐

  1. 如何使用Java代码混淆技术保护您的应用程序

    摘要 本文探讨了代码混淆在保护Java代码安全性和知识产权方面的重要意义.通过混淆技术,可以有效防止代码被反编译.逆向工程或恶意篡改,提高代码的安全性.常见的Java代码混淆工具如IPAGuard.A ...

  2. JS实现决策报表缓存最后一次查询条件

    问题描述 决策报表在打开时希望参数控件的值可以默认是上一次页面关闭前最后一次查询所选择的值. 解决方案 每次点击查询后将参数值保存到浏览器缓存中(适用于控件在参数栏内),或每次控件值发生改变后将参数值 ...

  3. 【Java】这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";

    String s1 = "a"; String s2 = s1 + "b"; String s3 = "a" + "b" ...

  4. #计数,记忆化搜索#C 连边方案

    分析 设\(dp[i][j][k][l]\)表示处理到\([i-l+1,i]\)的连边,二进制状态(奇点还是偶点)为\(k\)的方案数, 最后一维是为了避免算重,那么如果第\(i-l+1\)位是偶点可 ...

  5. 您有一份OpenHarmony开发者论坛2023年度总结,请查收~

    2023 年 11 月,OpenHarmony 开发者论坛 1.0 版本正式上线. 感谢各位开发者对 OpenHarmony 的大力支持和热爱,成为 OpenHarmony 开发者论坛的第一批体验用户 ...

  6. 深入理解 SQL UNION 运算符及其应用场景

    SQL UNION运算符 SQL UNION运算符用于组合两个或多个SELECT语句的结果集. 每个UNION中的SELECT语句必须具有相同数量的列. 列的数据类型也必须相似. 每个SELECT语句 ...

  7. Pandas选择与索引

    Series和DataFrame对象与Numpy数组和标准Python字典的数据索引和选择模式一样. 字典形式选择和索引 Series In [1]: import pandas as pd In [ ...

  8. 容器开发运维人员的 Linux 操作机配置优化建议

    "工欲善其事必先利其器", 作为一个PAAS平台架构师, 容器相关技术(docker, k8s等)是必不可少的. 本文简单介绍下我自己的Linux操作机配置. 提升工作效率, 提高 ...

  9. windows下配置mask2former(facebook版)

    1.安装Anaconda 2.安装PyCharm 3.创建python3.8环境(最高3.8因为有一个依赖包最高支持python3.8) 4.安装GCC 下载地址:https://sourceforg ...

  10. C++对象封装后的内存布局

    在C语言中,数据和数据的处理操作(函数)是分开声明的,在语言层面并没有支持数据和函数的内在关联性,我们称之为过程式编程范式或者程序性编程范式.C++兼容了C语言,当然也支持这种编程范式.但C++更主要 ...