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