使用docker安装并运行nginx命令:

docker run --name=nginx -p 80:80 -d docker.io/nginx

使用命令:

docker exec -it nginx /bin/bash 进入容器可查看到几个重要的文件

配置文件:nginx.conf 在 /etc/nginx/nginx.conf

日志文件: /var/log/nginx/access.log /var/log/nginx/error.log

使用cat命令打开nginx.conf

 1 root@dc048fc59765:/var/log/nginx# cat /etc/nginx/nginx.conf 2 3 user  nginx;4 worker_processes  1;5 6 error_log  /var/log/nginx/error.log warn;7 pid        /var/run/nginx.pid;8 9
10 events {
11     worker_connections  1024;
12 }
13
14
15 http {
16     include       /etc/nginx/mime.types;
17     default_type  application/octet-stream;
18
19     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
20                       '$status $body_bytes_sent "$http_referer" '
21                       '"$http_user_agent" "$http_x_forwarded_for"';
22
23     access_log  /var/log/nginx/access.log  main;
24
25     sendfile        on;
26     #tcp_nopush     on;
27
28     keepalive_timeout  65;
29
30     #gzip  on;
31
32     include /etc/nginx/conf.d/*.conf;
33 }
34 root@dc048fc59765:/var/log/nginx#

发现第32行,配置了一个子配置文件,进入conf.d发现有一个default.conf文件

打开default.conf文件:

 1 root@dc048fc59765:/etc/nginx/conf.d# cat default.conf 2 server {3     listen       80;4     listen  [::]:80;5     server_name  localhost;6 7     #charset koi8-r;8     #access_log  /var/log/nginx/host.access.log  main;9
10     location / {
11         root   /usr/share/nginx/html;
12         index  index.html index.htm;
13     }
14
15     #error_page  404              /404.html;
16
17     # redirect server error pages to the static page /50x.html
18     #
19     error_page   500 502 503 504  /50x.html;
20     location = /50x.html {
21         root   /usr/share/nginx/html;
22     }
23
24     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
25     #
26     #location ~ \.php$ {
27     #    proxy_pass   http://127.0.0.1;
28     #}
29
30     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
31     #
32     #location ~ \.php$ {
33     #    root           html;
34     #    fastcgi_pass   127.0.0.1:9000;
35     #    fastcgi_index  index.php;
36     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
37     #    include        fastcgi_params;
38     #}
39
40     # deny access to .htaccess files, if Apache's document root
41     # concurs with nginx's one
42     #
43     #location ~ /\.ht {
44     #    deny  all;
45     #}
46 }
47
48 root@dc048fc59765:/etc/nginx/conf.d# 

在浏览器中输入:http://192.168.11.241

从default.conf中11行可以看出,index页面在/usr/share/nginx/html

现在,我们配置一下容器卷:

docker rm -f nginx 删除一下之前的容器

再次执行比较完整的命令:

1 docker run \
2  --restart=always \
3  --name nginx \
4  -d -p 80:80 \
5  -v /data/nginx/html:/usr/share/nginx/html \
6  -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
7  -v /data/nginx/conf.d:/etc/nginx/conf.d \
8  -v /data/nginx/log:/var/log/nginx \
9  nginx

这里有几个注意事项:

(1)第一个“-v”,是项目位置,把html代码放到挂载到的目录下即可;

(2)第二个“-v”,是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",

这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。

(3)第三个“-v”,把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致

(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录

我们先启动一下,可以发现是有问题的,因为配置文件还没有。

1

2

3

[root@zuul-server data]# docker run  --name nginx  -d -p 80:80  -v /data/nginx/html:/usr/share/nginx/html  -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/log:/var/log/nginx  nginx

c8d49810b4afd4b6661beb942f0f19a67cf64f9798af9d2eb8a2aa242b2af434

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/data/nginx/nginx.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/ee154ee69264707a542409b514cfff950b31cefa4dcd4e66c3635d0aa94f5058/merged\\\" at \\\"/var/lib/docker/overlay2/ee154ee69264707a542409b514cfff950b31cefa4dcd4e66c3635d0aa94f5058/merged/etc/nginx/nginx.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

  

很明显,报错了,容器也没有启动成功,怎么解决了?

解决办法:

1 进入/data/nginx目录,

2 删除nginx.conf文件夹  rm  -rf nginx.conf

3 新建nginx.conf文件    touch nginx.conf

4 然后把之前cat /etc/nginx/nginx.conf的内容放入到nginx.conf

5 同理,cd /data/nginx/conf.d ,touch default.conf,把之前 cat /etc/nginx/conf.d/default.conf中的内容放入到新建的default.conf中。

最后 docker restart nginx 搞定。

需要配置一个端口转发功能,业务需求是:以http://192.168.11/241/mp开头的请求需要重定向到http://192.168.11.241:20001/mp上,

刚开始把 proxy_pass 对应的路径写成了 http://127.0.0.1:20001/;导致报404错误,原因很简单,nginx运作在容器里面,肯定找不到http://127.0.0.1:20001/,浪费了我一点点时间,一时没转过弯来。

1 server {2  listen  80;3  server_name localhost;4   5  #charset koi8-r;6  #access_log /var/log/nginx/log/host.access.log main;7   8  location / {9   #root /usr/nginx/dacheng-wechat-web;
10   #root /usr/nginx/html;
11   root /usr/share/nginx/html;
12   index index.html index.htm;
13   autoindex on;
14   # try_files $uri /index/index/page.html;
15   # try_files $uri /index/map/page.html;
16  }
17
18 location /mp {
19   proxy_pass http://192.168.11.241:20001/;
20
21 }
22
23
24  #error_page 404    /404.html;
25
26  # redirect server error pages to the static page /50x.html
27  #
28  error_page 500 502 503 504 /50x.html;
29  location = /50x.html {
30   root /usr/share/nginx/html;
31  }
32
33  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
34  #
35  #location ~ \.php$ {
36  # proxy_pass http://127.0.0.1;
37  #}
38
39  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
40  #
41  #location ~ \.php$ {
42  # root   html;
43  # fastcgi_pass 127.0.0.1:9000;
44  # fastcgi_index index.php;
45  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
46  # include  fastcgi_params;
47  #}
48
49  # deny access to .htaccess files, if Apache's document root
50  # concurs with nginx's one
51  #
52  #location ~ /\.ht {
53  # deny all;
54  #}
55 }

使用Docker安装Nginx并配置端口转发相关推荐

  1. Windows安装nginx并配置端口转发

    Windows安装nginx并配置端口转发 1.场景 在VMware虚拟机中启动了几个Linux,采用的是NAT网络配置,所以另一个Windows访问无法进行ssh或者其它应用的访问 2.安装 2.1 ...

  2. docker安装nginx,配置nginx,并成功访问

    [Nginx那些事]系列 [Nginx那些事]nginx 安装及常用指令 [Nginx那些事]Nginx 配置文件说明 [Nginx那些事]nginx原理解析 [Nginx那些事]nginx配置实例( ...

  3. docker安装nginx并配置SSL到个人博客

    1 准备 1.已安装好docker环境 2.已申请好域名 2 申请SSL证书 我使用的是腾讯云,申请免费的TrustAsia的SSL证书,阿里云等或者其他平台一般都会提供TrustAsia的SSL证书 ...

  4. docker安装nginx并配置ssl证书

    文章目录 一.准备SSL证书 二.下载最新nginx镜像 三.新建几个目录,把nginx容器内的配置文件挂载到主机上 四.启动一个nginx临时容器,把配置文件复制过来,然后删除 五.把SSL证书上传 ...

  5. nginx实现ip端口转发_配置Nginx实现端口转发

    #### 三.更改 Nginx 配置文件实现端口转发 1. 打开第二步中创建的 File Browser 网站([公网ip:端口]()),账号密码为 **admin/admin**,登录到 File ...

  6. Docker安装及镜像配置(常用命令介绍)

    一.安装Docker 官网 https://docs.docker.com/engine/install/centos/ # 1.卸载旧的版本 yum remove docker \docker-cl ...

  7. Docker(十):Docker实战 Docker 安装 Nginx

    Docker 安装 Nginx Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务 . 1.查看可用的 Nginx 版本 访问 Ngin ...

  8. Docker系列 二. Docker 安装 Nginx

    Docker 安装 Nginx Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务 . 1.查看可用的 Nginx 版本 访问 Ngin ...

  9. Docker系列三~docker安装nginx

    Docker系列三 docker安装nginx 搜索nginx版本 docker search nginx 拉取nginx最新版本镜像 docker pull nginx:latest 查看本地镜像 ...

最新文章

  1. 第二周作业-停车场门禁控制系统的状态机
  2. 提高Python运行效率的6大技巧!
  3. Sql2008R2 日志无法收缩解决方案
  4. .NET6之MiniAPI(二十九):UnitTest
  5. String... 参数定义中有三个点的意思
  6. 将本地的代码提交到github仓库
  7. Duilib中Webbrowser事件完善使其支持判断页面加载完毕
  8. ds18b20数字温度传感器特点及使用介绍
  9. 手把手教你搭建微信小程序服务器(HTTPS)
  10. B站视频搬运项目,宅男必选!
  11. Excel使用技巧大全
  12. firefox插件开发和调试
  13. 意识理论综述:众多竞争的意识理论如何相互关联?
  14. android调起应用商店某应用详情页
  15. 秋春招总结之MySQL
  16. mysql vacuum_PostgreSQL中快速对系统表实现vacuum full
  17. 抖音短视频编辑工具EffectCreator 6.4.0中文版
  18. EasyPOI导入多个sheet,既可更新又可新增,且从第二个sheet开始是根据第一个sheet内容的详情导入
  19. sql server 安装挂起
  20. 电脑办公记事app软件哪个好用?

热门文章

  1. pythonfor循环列表_如何在Python中通过for循环传递列表列表?
  2. Android开发——NFC标签读写
  3. js怎样截取字符串后几位以及截取字符串前几位
  4. 依赖倒置原则(Dependecy-Inversion Principle)
  5. 全程模拟新浪微博登录(2015)
  6. 电脑关机状态重置BIOS
  7. 行业安全解决方案 | 零售企业如何做好安全建设对抗黑灰产?
  8. 新华书店网上售书系统
  9. DS1302时钟总结
  10. 根据uuid获取通道session