使用Docker安装Nginx并配置端口转发问题及解决方法

 更新时间:2022年01月23日 10:21:49   作者:daiyuenlong110  
这篇文章主要介绍了使用Docker安装Nginx并配置端口转发,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

使用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

root@dc048fc59765:/var/log/nginx# cat /etc/nginx/nginx.conf 
  user  nginx;
  worker_processes  1;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  
 events {
     worker_connections  1024;
     }
 
 http {
     include       /etc/nginx/mime.types;
     default_type  application/octet-stream;
 
     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
 
     access_log  /var/log/nginx/access.log  main;
 
     sendfile        on;
     #tcp_nopush     on;
 
     keepalive_timeout  65;
 
     #gzip  on;
 
     include /etc/nginx/conf.d/*.conf;
 }
 root@dc048fc59765:/var/log/nginx#

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

打开default.conf文件:

root@dc048fc59765:/etc/nginx/conf.d# cat default.conf 
  server {
      listen       80;
      listen  [::]:80;
      server_name  localhost;
  
      #charset koi8-r;
      #access_log  /var/log/nginx/host.access.log  main;
     location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
     }
 
     #error_page  404              /404.html;
     # redirect server error pages to the static page /50x.html
     #
     error_page   500 502 503 504  /50x.html;
     location = /50x.html {
     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     #location ~ \.php$ {
     #    proxy_pass   http://127.0.0.1;
     #}
     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     #    root           html;
     #    fastcgi_pass   127.0.0.1:9000;
     #    fastcgi_index  index.php;
     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     #    include        fastcgi_params;
     # deny access to .htaccess files, if Apache's document root
     # concurs with nginx's one
     #location ~ /\.ht {
     #    deny  all;
 }
 root@dc048fc59765:/etc/nginx/conf.d# 

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

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

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

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

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

 docker run \
  --restart=always \
  --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

这里有几个注意事项:

(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挂载的是一个目录

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

[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/,浪费了我一点点时间,一时没转过弯来。

 server {
   listen  80;
   server_name localhost;
    
   #charset koi8-r;
   #access_log /var/log/nginx/log/host.access.log main;
    
   location / {
    #root /usr/nginx/dacheng-wechat-web;
   #root /usr/nginx/html;
   root /usr/share/nginx/html;
   index index.html index.htm;
   autoindex on;
   # try_files $uri /index/index/page.html;
   # try_files $uri /index/map/page.html;
  }
 
 location /mp {
   proxy_pass http://192.168.11.241:20001/;
 
 }   
  #error_page 404    /404.html;
   
  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root /usr/share/nginx/html;
  }
   
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  # proxy_pass http://127.0.0.1;
  #}
   
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
 # root   html;
  # fastcgi_pass 127.0.0.1:9000;
  # fastcgi_index index.php;
  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  # include  fastcgi_params;
  #}
   
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
 }

到此这篇关于使用Docker安装Nginx并配置端口转发的文章就介绍到这了,更多相关Docker安装Nginx配置端口转发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • docker-compose启动服务方式

    docker-compose启动服务方式

    这篇文章主要介绍了docker-compose启动服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • docker容器增加端口映射全过程(修改配置文件方式)

    docker容器增加端口映射全过程(修改配置文件方式)

    文章详细描述了如何通过修改Docker容器的配置文件来增加端口映射,以解决已经运行的容器需要增加端口映射的问题,以MySQL容器为例进行了具体操作步骤的说明
    2024-11-11
  • 详解docker pull 下来的镜像文件存放的位置

    详解docker pull 下来的镜像文件存放的位置

    本篇文章主要介绍了详解docker pull 下来的镜像文件存放的位置,具有一定的参考价值,有兴趣的可以了解一下。
    2017-04-04
  • Centos 7中Docker私有仓库的搭建方法

    Centos 7中Docker私有仓库的搭建方法

    本篇文章主要介绍了Centos 7中Docker私有仓库的搭建方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 删除docker容器中内容后打包镜像不变小问题及解决

    删除docker容器中内容后打包镜像不变小问题及解决

    文章讨论了在Docker中处理大压缩包时遇到的问题,以及如何通过分层存储和使用`docker load`与`docker import`命令来解决镜像大小过大的问题
    2025-03-03
  • Docker镜像优化打包速度思考

    Docker镜像优化打包速度思考

    本文主要介绍了Docker镜像优化打包速度思考,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • 使用docker安装部署NextCloud私人网盘的方法步骤

    使用docker安装部署NextCloud私人网盘的方法步骤

    本文主要介绍了使用docker安装部署NextCloud私人网盘的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • centOS7安装docker的详细步骤

    centOS7安装docker的详细步骤

    这篇文章主要为大家介绍了centOS7安装docker的详细步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 关于docker启动jenkins环境的问题

    关于docker启动jenkins环境的问题

    这篇文章主要介绍了docker启动jenkins环境的问题,文中提到了jenkins基本工作原理及管理员密码获取方法,对docker启动jenkins环境相关知识感兴趣的朋友一起看看吧
    2022-04-04
  • docker环境中websocket 通过nginx代理不通解决方案

    docker环境中websocket 通过nginx代理不通解决方案

    这篇文章主要介绍了docker环境中websocket 通过nginx代理不通解决方案,下面是一些可能会导致 WebSocket 代理失败的问题以及相应的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05

最新评论