Mac 配置Nginx域名转发

配置环境

  • MacOS Monterey 12.0.1
  • 提前运行端口88的Web项目

1.修改hosts文件

  • 打开host文件 sudo vi /etc/hosts
XaysdeMacBook-Pro:~ xay$ sudo vi /etc/hosts
  • 添加域名映射 
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       www.em.cn #添加域名映射到nginx所在服务器127.0.0.1
127.0.0.1       www.es.cn

2、配置Nginx

  • 配置nginx.conf 
# 在Http内增加
server {
        # www.es.cn 转发到127.0.0.1:88
        listen 9090; #监听端口号
        server_name www.es.cn;  #监听的域名,根据host头判断
        location / {
            proxy_pass http://127.0.0.1:88;
        }
    }
#没有配置Nginx之前的访问地址:http://localhost:88
#配置Nginx之后的访问地址是:http://www.em.cn
#通过nginx转发到http://127.0.0.1:88,也就是http://localhost:88

        location匹配的是当前url地址中过滤域名端口后的字符串内容

        “/”就是location的匹配逻辑,是通配符,表示”/”后面的所有字符串都能匹配成功。

        比如”http://www.em.cn/index/login”,“/”后的“index/logn”满足匹配逻辑。

        此处匹配成功后,才会进入到proxy_pass转发

        proxy_pass http://127.0.0.1:88; //转发到后端服务器的地址。

        例如上面地址”http://www.em.cn/index/login”,

        nginx会将过滤掉域名和端口号后剩下的“/index/login”拼接到 http://127.0.0.1:88 的后面,

        地址最后被转换成http://127.0.0.1:88/index/login

3、测试        

  • 浏览器访问 www.em.cn

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值