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. KingbaseES 参数设置优先级别

    Oracle的参数可以设置system和session级别,当设置了session级别的参数时,会覆盖值system级别. KingbaseES除了该两个级别外,还有database级别.user/r ...

  2. CI和自动化测试的结合(jenkins的搭建和使用)

    CI持续集成是一种思想,具体实现是通过jenkins持续集成工具去实现的. Jenkins的安装和配置(war包启动) 安装jenkins的方式有两种: 方式一:通过下载安装包安装,jenkins.m ...

  3. 【放假第1天】采购季倒计时 2G 50/年,4G 618/3年 云服务器选购攻略 阿里云 腾讯云 京东云对比 搭建网站、数据分析

    ​ 更新日期:4月4日(阿里云价格回调,京东云采购季持续进行) <最新对比表>已更新在文章头部-腾讯云文档,文章具有时效性,请以腾讯文档为准! https://docs.qq.com/do ...

  4. #莫比乌斯反演#BZOJ 2694 LCM

    题目 多组询问求 \[\sum_{i=1}^n\sum_{j=1}^m{|\mu(\gcd(i,j))|*lcm(i,j)}\pmod {2^{30}} \] \(T\leq 10^4,n,m\leq ...

  5. #差分,前缀和#nssl 1469 U

    分析 考虑离线,因为异或和很难在线处理 然后可以用两个数组来差分斜与横的情况,然后将答案竖着传 时间复杂度\(O(n^2)\) 代码 #include <cstdio> #include ...

  6. #线段树#洛谷 4588 [TJOI2018]数学计算

    题目传送门 分析 由于曾经做过原题 所以就直接说了,因为每个数最多被除掉一次 所以可以用线段树维护区间乘,也就很简单了,删除就单点修改就行了 代码 #include <cstdio> #i ...

  7. 体验Semantic Kernel图片内容识别

    前言 前几日在浏览devblogs.microsoft.com的时候,看到了一篇名为Image to Text with Semantic Kernel and HuggingFace的文章.这篇文章 ...

  8. 共筑使能千行百业的数字底座 | HDC 2022松湖对话顺利召开

     11月5日,华为开发者大会2022松湖对话在东莞松山湖凯悦酒店召开,开放原子开源基金会秘书长冯冠霖.华为终端BG软件部总裁龚体.深圳国家金融科技测评中心董事长钟剑.鸿湖万联(江苏)科技发展有限公司董 ...

  9. C 语言入门:如何编写 Hello World

    C 语言简介 C 语言是由 Dennis Ritchie 于 1972 年在贝尔实验室创建的一种通用编程语言.尽管年代久远,它仍然是一款非常流行的语言.它之所以受欢迎的主要原因是它是计算机科学领域的基 ...

  10. 干货分享|身为顶尖的Hr,这个Excel插件你不能不知道,用上它事业开挂!

    第一季度,老板看了历年不同地区各销售业绩数据表的总结,说想知道新人进来多久才能成为成熟的销售,成长周期有多长? 我们人事被老板这个灵光一现的想法吓到了,大家伙上上下下为这件事情忙了4个日夜. 整整五年 ...