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

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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序