首页 > 系统相关 >在虚拟机(Linux)中Docker中部署Nginx成功,但是在宿主机无法访问Nginx站点?

在虚拟机(Linux)中Docker中部署Nginx成功,但是在宿主机无法访问Nginx站点?

时间:2023-11-03 13:00:12浏览次数:50  
标签:notice 09 21 05 无法访问 宿主机 Nginx nginx

1.问题

本文是基于黑马程序员Docker基础--常见命令一课中部署Nginx时遇到的问题作出解答。

在虚拟机(Linux)中Docker中部署Nginx成功,但是在宿主机无法访问Nginx站点
如图,Nginx服务已经启动成功

但是我们在宿主机的浏览器试图访问的时候却总是报错:
image

2.解决思路

2.1 查看端口号是否映射正确

这里Nginx是主机80端口映射到docker中的80端口,经检查并没有错误

2.2 检查防火墙状态

systemctl status firewalld
参考下图,此时Active一栏,已经是关闭状态,不会影响我们的步骤,跳过
image

2.3 在Linux中测试直连Nginx

1.先查看Nginx于docker中的ip地址

docker inspect nginx
image
经查看后得知是172.17.0.1
image

2.尝试直连

curl 172.17.0.1
发现返回的html代码正是Nginx的欢迎界面,说明这里的Nginx服务是没有问题的
image

2.4 正在运行的nginx容器需要进入内部启动nginx

经查阅博客:虚拟机CENTOS中DOCKER启动容器NGINX后,网页打不开,无法访问80端口,或被拒
这里正在运行的nginx容器需要进入内部启动nginx后才能使用

1.先通过Docker进入nginx容器内部

docker exec -it nginx bash

image

2.启动服务

root@409d25d8add2:/# service nginx start
image
我这里并没有显示具体开启过程,但是博客中提供的过程大概如下:

[root@localhost ~]# docker exec -it nginx(此处是你的容器name) bash #进入容器内部

root@c431554b3059:/# service nginx start #开启nginx

2022/05/09 03:35:02 [notice] 21#21: using the "epoll" event method

2022/05/09 03:35:02 [notice] 21#21: nginx/1.21.6

2022/05/09 03:35:02 [notice] 21#21: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)

2022/05/09 03:35:02 [notice] 21#21: OS: Linux 3.10.0-1160.62.1.el7.x86_64

2022/05/09 03:35:02 [notice] 21#21: getrlimit(RLIMIT_NOFILE): 1048576:1048576

root@c431554b3059:/# 2022/05/09 03:35:02 [notice] 22#22: start worker processes

2022/05/09 03:35:02 [notice] 22#22: start worker process 23

root@c431554b3059:/# exit #退出容器

3.访问成功

image

标签:notice,09,21,05,无法访问,宿主机,Nginx,nginx
From: https://www.cnblogs.com/trmbh12/p/17807391.html

相关文章

  • linux下安装nginx
    下载nginxwgethttps://nginx.org/download/nginx-1.25.3.tar.gzyum安装依赖包yum-yinstallgcczlibzlib-develpcre-developensslopenssl-devel解压tar-zxvfnginx-1.22.0.tar.gz配置当前nginxcdnginx-1.22.0./configure--prefix=/usr/local/nginx......
  • 前后端都用得上的 Nginx 日常使用经验
    前言nginx是一个高性能的开源反向代理服务器和web服务器,一般用来搭建静态资源服务器、负载均衡器、反向代理,本文将分享其在Windows/docker中的使用,使用nssm部署成服务的方案脚本,局域网中自定义域名解决https提示不安全的解决方案,以及一路踩过的坑。特点高性能:事件驱......
  • 启动nginx报错nginx: [emerg] unexpected end of file, expecting "}" in /usr/local/
    启动nginx报错:“nginx:[emerg]unexpectedendoffile,expecting“}”in/usr/local/nginx/conf/nginx.conf:118”重启nginx时,报这么个错:[root@localhostconf]#/usr/local/nginx/sbin/nginx-sreloadnginx:[emerg]unexpectedendoffile,expecting“}”in/usr/lo......
  • nginx coturn socat privoxy opencv 静态编译
    文档说明:只记录关键的地方;发文时间:2023-11-02意义:linux环境,免安装下载后即可使用环境:alpine:3.18dockerclang状态:完善中体验编译结果nginx静态编译关键点nginxusePCRE2libraryonnginx1.21.5hg脚本全称是mercurialopensslzlibpcpre2等静态库......
  • nginx设置允许跨域
    server{listen8088;server_namelocalhost;#charsetkoi8-r;#access_loglogs/host.access.logmain;add_header'Access-Control-Allow-Methods''POST,GET,OPTIONS';add_header......
  • nginx报错 [error] 612#4188: CreateFile() "C:\yjzx\nginx-1.24.0/logs/nginx.pid"
    背景无论是nginx-sstop还是nginx-sreload命令,都会出现这个错误。[error]612#4188:CreateFile()"C:\yjzx\nginx-1.24.0/logs/nginx.pid"failed(2:Thesystemcannotfindthefilespecified)查找logs下nginx.pid文件确实没有创建成功,在网上查找了下了解决办法。发......
  • 7. 从零开始编写一个类nginx工具, HTTP及TCP内网穿透原理及运行篇
    wmproxywmproxy是由Rust编写,已实现http/https代理,socks5代理,反向代理,静态文件服务器,内网穿透,配置热更新等,后续将实现websocket代理等,同时会将实现过程分享出来,感兴趣的可以一起造个轮子法项目++wmproxy++gite:https://gitee.com/tickbh/wmproxygithub:https://github.com/tic......
  • nginx反向代理的重定向
    bi.kailinjt.com.confserver{listen80;server_namebi.kailinjt.com;location=/{return302$scheme://$host/webroot/login.html;}location/{#return302......
  • yum安装nginx-源码编译安装nginx
    #nginx官网http://nginx.org/#nginx主要代理七层协议,也就是应用层;nginx是一个高性能HTTP和反向代理,负载均衡服务器.............#nginx默认端口是80#https默认端口是443#官网查看安装步骤http://nginx.org/——>documentation——>Installingnginx——>InstallationonLinux,pac......
  • nginx server_name配置多域名的坑
    问题介绍项目配置了多个域名,如下,php代码中有获取 $_SERVER['SERVER_NAME'] 的值server{ server_namea.demo.comb.demo.com;...}当访问a.demo.com时,其获取的值是符合预期的。但是当访问b.demo.com时,其获取的值还是a.demo.com,导致代码中的判断出现错误......