解决mac无法使用80端口问题,亲测可用
前言:
在mac os中,非root用户是无法使用小于1024的常用端口的。如果开发中需要用到80端口, 就要设置端口转发。
hosts文件介绍
(1)hosts文件是将域名和IP地址建立映射关系的系统文件,用户可以自定义常用域名跟IP,当在浏览器上输入网址时,系统会优先从hosts文件找到相应的IP地址,打开相应的网页。
(2)hosts文件的作用是域名解析,构建映射关系,屏蔽垃圾网站。
(3)为什么要修改hosts文件?
在微信公众号开发时候,需要获取用户的微信信息,如果在微信开发者工具的地址栏填写localhost就获取不了,所以必须要填写审核通过的微信公众号域名。在这里,假设我的微信公众号域名为:https://test.cn 。本地启动了java项目,当你很高兴地在在微信开发者工具输入https://test.cn 时候,会注意到点击网页任意一个按钮,都不会触发到本地java项目的任何一个接口。原因好简单,因为默认hosts文件中,域名localhost 映射的是 127.0.0.1 ip地址。因此,需要把localhost 映射为https://test.cn。
修改hosts文件的步骤
(1)打开Terminal,输入以下指令:
cd /private/etc sudo vim hosts
打开后,默认的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 samuel.local # added by Apache Friends XAMPP
(2)把localhost 映射为https://test.cn (改成你要映射的域名)后 ,修改后是张这样的:
## # 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 127.0.0.1 test.cn 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 samuel.local # added by Apache Friends XAMPP
保存退出。
端口转发
域名映射完成后,需要做的是端口转发,也就是将监听的80端口转发到8080。
1.创建idea.tomcat.forwarding文件:
sudo vim /etc/pf.anchors/idea.tomcat.forwarding
在idea.tomcat.forwarding添加以下命令:
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080 rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 443 -> 127.0.0.1 port 8443
2.创建pf-tomcat.conf文件
sudo vim /etc/pf-tomcat.conf
在pf-tomcat.conf添加以下命令:
rdr-anchor "forwarding" load anchor "forwarding" from "/etc/pf.anchors/idea.tomcat.forwarding"
3.启动端口转发功能
sudo pfctl -ef /etc/pf-tomcat.conf
如果你在终端看到以下提示,恭喜你成功启动:
pfctl: Use of -f option, could result in flushing of rules present in the main ruleset added by the system at startup. See /etc/pf.conf for further details. No ALTQ support in kernel ALTQ related functions disabled pfctl: pf already enabled
4.关闭端口转发功能
sudo pfctl -d
或者关闭全部
pfctl -F all -f /etc/pf.conf
注意事项:
重启mac,需要手动重启端口转发命令
sudo pfctl -ef /etc/pf-tomcat.conf
java项目配置
在由spring boot搭建的java开发项目中,用到的开发工具是IDEA,操作系统mac OS 10.13.4。因为微信公众号的开发需要,端口号要设置为80,
之前已经设置好端口转发,80转发到8080,所以在本地调试的时候,项目的服务器端口设置为8080,如下图:
- 解决MAC系统在做微信开发时候tomcat无法使用80端口问题
- 解决MAC系统在做微信开发时候tomcat无法使用80端口问题
- Mac下使用svnx进行版本控制,并解决无法上传.a库文件的问题
- 解决mac使用homebrew安装MySQL无法登陆问题
- mac 使用MyEclipse时遇到无法创建web项目问题解决方法
- Mac OS X中virtualenv里python shell无法使用光标键问题的解决
- BCM4330无法使用客户MAC地址问题解决办法
- mac下使用android studio,解决无法打开问题。
- 解决mac版本的Eclipse中svn无法使用的问题
- Mac系统开发常见问题-80端口被占用的解决方案2-使用Nginx解决
- 解决PING指令无法正常使用问题
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题(转)
- 使用installshild解决某些安装文件无法在win2003上安装的问题
- OP:windows 2008 安装之后无线网无法使用问题解决(Windows 2008 Wireless)
- IIS ,NET中"无法显示 XML 页。 使用 XSL 样式表无法查看 XML 输入。"问题的解决
- [ThinkPad ] 解决 ubuntu 下内建麦克风无法使用的问题
- 使用startx无法进入图形界面的问题终于解决
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题
- 终于解决了eclipse的metrics插件使用一段时间后就无法使用的问题
- 解决Firefox无法使用“查看图片”或“查看页面源代码”的问题