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 也能做到这一点。



  






你这块配错了: -v /bing/nginx/conf/:/etc/nginx \


正确配置:-v /bing/nginx/conf:/etc/nginx \
可以参考我的这一篇 https://blog.csdn.net/runewbie/article/details/106462125

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

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

谢谢!我 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

还是原来的配置

[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;                                                            

  }  

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

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

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


[root@localhost html]# ls

es index.html

[root@localhost html]# pwd

/bing/nginx/html


用域名可以吗

用域名 gulimall.com 访问 404

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

[root@localhost html]# docker stop nginx

nginx


希望访问:

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


这两个地址都能访问:


http://gulimall.com:9200/

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

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

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

谢谢你能回答我的问题!

如果方便,明天继续。

?

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

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

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


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

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

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





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



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


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

exit

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

[root@localhost conf.d]# 


也没有错误日志。



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

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

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

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


配置后,nginx是否重启了

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

你的linux中80端口开放了吗

vim /etc/sysconfig/iptables

配置后,无数次重启过。


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 请问这个是什么?



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

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


[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



你的是vware虚拟机吗



算是啊

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

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

不知道是不是这么用的

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

你把nginx中的server_name 的值改为localhost

看看能不能通过localhost访问


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


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





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

[root@localhost conf.d]# 


没有任何错误日志。

那就是你docker的问题了

你直接安装nginx呢

不用docker安装

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

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

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

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

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

?

问题还是没难解决。

恭喜恭喜,终于解决了

环境问题,确实很头疼

是啊,一个礼拜了。?

这个问题,自动关了?

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


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


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

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


nginx笔记_shuangmu9768的博客-CSDN博客 【1】ubuntu安装nginx【2】nginx.conf 基本配置【3】命令【4】Centos安装nginx【5】超时设置【6】显示目录【7】是否拼接URL中的路径【8】location 配置【9】Rewrite规则【10】nginx作为tcp代理【11】文件名中文乱码【12】nginx启用账号与密码控制访问【13】nginx 配置SSL【14】nginx动态添加模块【15】配置正向代理【16】nginx内置变量【17】windows下配置nginx+php【18】ngi https://blog.csdn.net/shuangmu9768/article/details/108397686