nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Mac配置Nginx域名转发

Mac配置Nginx域名转发实践

作者:乌龟车队长

想要在MacOS Monterey上快速配置Nginx并运行端口88的Web项目?这篇实战指南手把手教你修改hosts文件、配置Nginx反向代理,轻松实现域名映射与请求转发,点击立即掌握Mac环境Web部署技巧,解决本地开发痛点

配置环境

1、修改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

# 在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_passhttp://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、测试

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文