docker 安装nginx 配置目录挂载

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

参考文档:

posted @ 2020-08-02 06:56  HaimaBlog  阅读(2351)  评论(0编辑  收藏  举报