bing_yu2001 2020-08-12 17:41 采纳率: 66.7%
浏览 468
已结题

docker 安装的 nginx(使用本地 hosts 配置域名)实现反向代理失败

一、环境:

mac、Centos8、docker、nginx 1.10




二、说明:

1、在 Linux 中,使用压缩包安装的 nginx 实现反向代理(本地配置的域名)访问成功。说明端口没问题。

2、使用 docker 安装的 Nginx 一直操作失败。





三、详细说明


1)docker 安装 nginx 方法如下(谷粒商城教程的方法):


[root@localhost nginx]# mkdir nginx

现在路径为:cd /bing/nginx/nginx



下载并启动 nginx,只为复制出配置文件:

docker run -p80:80 --name nginx -d nginx:1.10   【 下载兼启动 】


复制 nginx 配置文件:

[root@localhost nginx]# docker container cp nginx:/etc/nginx .  



将 nginx 文件夹改名为 conf :

[root@localhost nginx]# mv nginx conf


删除之前启动的 nginx :

[root@localhost nginx]# docker stop nginx                                                                                                                                           

[root@localhost nginx]# docker rm nginx 



创建新的Nginx,执行以下命令

docker run -p 80:80 --name nginx \

 -v /bing/nginx/html:/usr/share/nginx/html \

 -v /bing/nginx/logs:/var/log/nginx \

 -v /bing/nginx/conf/:/etc/nginx \

 -d nginx:1.10



创建 nginx 访问的首页面:

[root@localhost nginx]# ls                                                                     

conf html logs      

                                                                     

[root@localhost nginx]# cd html/                                                                                                                                     

[root@localhost html]# vi index.html 


<h1>Gulimall</h1> 


保存退出



浏览器访问 nginx :

http://192.168.88.129/


页面输出:

Gulimall



说明 nginx 安装成功。






2)以下是配置反向代理过程


在本地 hosts 文件中配置:

192.168.88.129 gulimall.com # 192.168.88.129 是linux ip 地址



docker 安装的 nginx 配置文件中配置:

[root@localhost conf.d]# cd /bing/nginx/conf/conf.d/

[root@localhost conf.d]# cp default.conf gulimall.conf 



修改配置:

[root@localhost conf.d]# vi gulimall.conf




server {                                                                              

  listen    80;                                                                        

  server_name gulimall.com;        # 本地 hosts 文件中 配置的域名名称                                                                

                                                                                  

  #charset koi8-r;                                                                        

  #access_log /var/log/nginx/log/host.access.log main;                                                     

                                                                                  

  location / {

    proxy_pass http://192.168.88.1:10000;  

                                                           

  }  




说明:

  http://192.168.88.1:10000 是本地 springBoot 微服务项目的访问地址,浏览器输入 http://192.168.88.1:10000 可成功访问到页面  



重启 nginx 

[root@localhost conf.d]# docker restart nginx 




访问

http://gulimall.com/


页面404 





尝试了百度来的好些方法无效。

尝试了在 linux 中用压缩包安装 nginx ,实现域名映射 反向代理成功。

希望 docker 安装的 nginx 也能做到这一点。



  






  • 写回答

58条回答 默认 最新

  • runewbie 2020-08-12 23:04
    关注
    你这块配错了: -v /bing/nginx/conf/:/etc/nginx \
    
    


    正确配置:-v /bing/nginx/conf:/etc/nginx \
    可以参考我的这一篇 https://blog.csdn.net/runewbie/article/details/106462125
    
    评论
  • bing_yu2001 2020-08-12 23:25
    关注

    命令处果真多了一个/ ,于是我停止这个 nginx 容器,删除这个容器,命令处去掉多的那个 / ,再创建并启动一个新的 nginx 容器,访问反向代理的页面还是 404 。我哪里还出了问题?

    评论
  • runewbie 2020-08-12 23:29
    关注

    这个要具体看你的配置 我也没法确定 你看一下启动日志 ng 有没有正常启动

    评论
  • bing_yu2001 2020-08-12 23:34
    关注

    谢谢!我 docker nginx 一直正常启动,也可以访问到 Linux 80 端口。


    [root@localhost conf.d]# docker logs nginx

    /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

    /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

    /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

    10-listen-on-ipv6-by-default.sh: error: IPv6 listen already enabled

    /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

    /docker-entrypoint.sh: Configuration complete; ready for start up

    /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

    /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

    /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

    10-listen-on-ipv6-by-default.sh: error: IPv6 listen already enabled

    /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

    /docker-entrypoint.sh: Configuration complete; ready for start up

    评论
  • bing_yu2001 2020-08-12 23:38
    关注

    还是原来的配置

    [root@localhost ~]# vi /bing/nginx/conf/conf.d/gulimall.conf



    server {                                                                              

      listen    80;                                                                        

      server_name gulimall.com;                                                                   

                                                                                      

      #charset koi8-r;                                                                        

      #access_log /var/log/nginx/log/host.access.log main;                                                     

                                                                                      

      location / {

        proxy_pass http://192.168.88.1:10000;                                                            

      }  

    评论
  • runewbie 2020-08-12 23:40
    关注

    我这边看你的其他配置 也没有什么问题 我这边也没法确定你的问题 你再看一看

    评论
  • runewbie 2020-08-12 23:42
    关注

    你这边不配置反向代理是可以访问到ng的页面吗?在docker中

    评论
  • bing_yu2001 2020-08-12 23:44
    关注

    是的,可以访问 index.html 页面


    [root@localhost html]# ls

    es index.html

    [root@localhost html]# pwd

    /bing/nginx/html


    评论
  • bing_yu2001 2020-08-12 23:45
    关注

    评论
  • runewbie 2020-08-12 23:53
    关注

    用域名可以吗

    评论
  • bing_yu2001 2020-08-12 23:54
    关注

    用域名 gulimall.com 访问 404

    评论
  • runewbie 2020-08-12 23:56
    关注

    不配置反向代理 使用域名啊访问也是404?

    评论
  • bing_yu2001 2020-08-12 23:59
    关注

    [root@localhost html]# docker stop nginx

    nginx


    希望访问:

    本地 hosts 的配置应该有效的。


    评论
  • bing_yu2001 2020-08-13 00:00
    关注

    这两个地址都能访问:


    http://gulimall.com:9200/

    http://gulimall.com:5601/app/kibana

    评论
  • bing_yu2001 2020-08-13 00:08
    关注

    现在时间不早了,身体要紧,早点休息!

    我卡这三天了,现在困了,睡觉了。

    谢谢你能回答我的问题!

    如果方便,明天继续。

    ?

    评论
  • runewbie 2020-08-13 00:10
    关注

    你访问一下http://gulimall.com/可以吗

    评论
  • runewbie 2020-08-13 00:21
    关注

    不知道你的服务有没有启动?映射到网关后的那个服务

    评论
  • sinJack 2020-08-13 09:30
    关注

    https://blog.csdn.net/qq_29479041/article/details/107016992

    评论
  • bing_yu2001 2020-08-13 10:44
    关注


    我尝试过,我这关访问不了,配置网关也还是 404。

    要反向代理的微服务一直启动着。

    我用 nginx 配置上的 ip http://192.168.88.1:10000/ 能访问到页面。





    评论
  • bing_yu2001 2020-08-13 10:47
    关注

    本地域名配置是没问题的。


    评论
  • bing_yu2001 2020-08-13 11:23
    关注


    我进入 nginx 容器看,配置文件映射也是没有问题的。


    root@8c210f8a3b47:/etc/nginx/conf.d# exit

    exit

    [root@localhost conf.d]# docker logs nginx

    [root@localhost conf.d]# 


    也没有错误日志。



    评论
  • bing_yu2001 2020-08-13 11:34
    关注

    @sinJack 我本地和 linux 里安装的 nignx 配置反向代理是成功的。

    就是 docker 里安装的 nginx 配置反向代理失败。

    我删除了 docker 安装 nginx 的所有目录和容器、镜像,按照 runewbie 的那一贴重新再来,还是配置失败。

    我搞不明白,为什么配置文件无效。


    评论
  • sinJack 2020-08-13 12:48
    关注

    配置后,nginx是否重启了

    评论
  • sinJack 2020-08-13 12:49
    关注

    还有,http://gulimall.com/这样访问可以吗

    评论
  • sinJack 2020-08-13 12:54
    关注

    你的linux中80端口开放了吗

    评论
  • sinJack 2020-08-13 12:54
    关注

    vim /etc/sysconfig/iptables

    评论
  • bing_yu2001 2020-08-13 13:07
    关注

    配置后,无数次重启过。


    http://gulimall.com/ 无法访问


    80 端口已开放

    [root@localhost conf.d]# firewall-cmd --list-all 

    public (active)

     target: default

     icmp-block-inversion: no

     interfaces: ens33

     sources: 

     services: cockpit dhcpv6-client ssh

     ports: 9200/tcp 80/tcp 8080/tcp 8081/tcp 9001/tcp

     protocols: 

     masquerade: no

     forward-ports: 

     source-ports: 

     icmp-blocks: 

     rich rules: 

         


    vim /etc/sysconfig/iptables 请问这个是什么?



    评论
  • bing_yu2001 2020-08-13 13:08
    关注

    [root@localhost conf.d]# cat /etc/sysconfig/iptables

    cat: /etc/sysconfig/iptables: 没有那个文件或目录


    评论
  • bing_yu2001 2020-08-13 13:13
    关注

    [root@localhost sysconfig]# ls

    anaconda chronyd  crond      grub       irqbalance kernel  man-db  network-scripts radvd    rpcbind  samba   smartmontools virtlogd

    atd    console  ebtables-config ip6tables-config kdump    ksm    modules nftables.conf  raid-check rsyslog  saslauthd sshd      wpa_supplicant

    cbq    cpupower firewalld    iptables-config  keepalived libvirtd network qemu-ga     rhn     run-parts selinux  virtlockd


    [root@localhost sysconfig]# pwd

    /etc/sysconfig



    评论
  • sinJack 2020-08-13 13:40
    关注

    你的是vware虚拟机吗

    评论
  • sinJack 2020-08-13 13:42
    关注


    评论
  • bing_yu2001 2020-08-13 13:47
    关注


    算是啊

    评论
  • sinJack 2020-08-13 13:49
    关注

    我也不知道,你为什么没有。一般来说,你需要访问linux上的端口的话,需要开放指定的端口才能访问,并且关闭防火墙

    评论
  • sinJack 2020-08-13 13:54
    关注

    https://blog.csdn.net/u011542994/article/details/49824729

    评论
  • sinJack 2020-08-13 13:54
    关注

    不知道是不是这么用的

    评论
  • bing_yu2001 2020-08-13 14:24
    关注

    端口我已经暴露和开放。关闭防火墙也还是不行。至于VMware fusion设置端口转发,我还不懂。现在还在百度相关信息。

    评论
  • sinJack 2020-08-13 14:27
    关注

    你把nginx中的server_name 的值改为localhost

    评论
  • sinJack 2020-08-13 14:27
    关注

    看看能不能通过localhost访问


    评论
  • bing_yu2001 2020-08-13 15:25
    关注

    我开启了 linux 的 tomcat , 用本地配置的域名可以访问。


    但用 docker 安装的 nginx 配置域名转发,就访问不了了。





    评论
  • bing_yu2001 2020-08-13 15:26
    关注

    [root@localhost conf.d]# docker logs nginx

    [root@localhost conf.d]# 


    没有任何错误日志。

    评论
  • sinJack 2020-08-13 15:27
    关注

    那就是你docker的问题了

    评论
  • sinJack 2020-08-13 15:27
    关注

    你直接安装nginx呢

    评论
  • sinJack 2020-08-13 15:27
    关注

    不用docker安装

    评论
  • bing_yu2001 2020-08-13 15:29
    关注

    直接安装的 nginx 什么都可用。

    用 docker 安装的 nginx 为的是后面学习集群。

    评论
  • bing_yu2001 2020-08-13 15:33
    关注

    更重要的问题是,为什么人家可以,我不可以,而且,没有错误提示看。真的想不明白。

    评论
  • sinJack 2020-08-13 15:42
    关注

    这个就尴尬了。哈哈。那明确了docker的问题了

    评论
  • sinJack 2020-08-13 15:42
    关注

    百度看看有没有类似你一样的情况

    评论
  • bing_yu2001 2020-08-13 15:47
    关注

    ?

    评论
  • bing_yu2001 2020-08-14 20:06
    关注

    问题还是没难解决。

    评论
  • bing_yu2001 2020-08-15 13:06
    关注

    评论
  • sinJack 2020-08-15 13:07
    关注

    恭喜恭喜,终于解决了

    评论
  • sinJack 2020-08-15 13:08
    关注

    环境问题,确实很头疼

    评论
  • bing_yu2001 2020-08-15 13:08
    关注

    是啊,一个礼拜了。?

    评论
  • sinJack 2020-08-15 13:10
    关注

    这个问题,自动关了?

    评论
  • bing_yu2001 2020-08-15 14:54
    关注

    问题是我关闭的。原因:我已经放弃再解决了,是群里有人帮助了我,并支持鼓励我一定要解决这个问题,还给了我新的安装环境。这贴我算他结的。


    评论
  • bing_yu2001 2020-08-20 11:15
    关注

    2天过去了,一直没收到退款,是程序问题,还是工作人员过失。


    评论
  • shuangmu9768 2021-10-21 12:42
    关注

    日志、日志、要nginx日志干嘛?看日志啊

    评论
  • shuangmu9768 2021-10-21 12:45
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
  • ¥15 prophet运行报错,如何解决?
  • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢
  • ¥15 eclipse无法正常运行
  • ¥15 定义了函数,但是无法根据函数定义触发器
  • ¥20 5变量卡诺图化简得出与非门电路图
  • ¥15 Python爬取交通拥堵指数数据
  • ¥15 使用vba抓取重定向网页问题
  • ¥20 付费需求测试程序(细谈)。
  • ¥15 为什么这段c++代码会报这么多语法错误?