美文网首页
mac无法使用80端口问题

mac无法使用80端口问题

作者: 污嘿 | 来源:发表于2022-02-25 15:55 被阅读0次

前言:
在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
1
2
3
4
5
6
7
8
9
10
11

保存退出。

端口转发
域名映射完成后,需要做的是端口转发,也就是将监听的80端口转发到8080。
1.创建idea.tomcat.forwarding文件:

sudo vim /etc/pf.anchors/idea.tomcat.forwarding

1
在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
1
2
3
4
5
6
7

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,如下图:


image.png

原文链接:https://blog.csdn.net/samuelandkevin/article/details/80279773

相关文章

  • Mac 使用 80 端口

    Mac 使用 80 端口 更新日志:20170807 更新mac使用80端口的错误问题。 [toc] [ ] 方案...

  • mac无法使用80端口问题

    前言:在mac os中,非root用户是无法使用小于1024的常用端口的。如果开发中需要用到80端口, 就要设置端...

  • Mac上启用80端口——nginx的安装

    现在mac上有个新需求,需要tomcat使用80端口而不是8080端口。 mac上使用80端口需要root权限,所...

  • Laravel Docker

    解决mac os 80端口占用问题 1、使用lsof -i:80查看当前占用80端口的进程,如果有就kill掉。2...

  • Mac os使用技巧

    开启转发功能 在mac os中,非root用户是无法使用小于1024的常用端口的。如果开发中需要用到80端口, 就...

  • Mac解决Nginx无法使用80端口

    之前在Mac下安装Nginx,默认它会选择8080端口,查了一下说是因为Mac默认占用了80端口,因为不紧急所以就...

  • 80端口被system占用的解决办法

    今天要测试 go 服务端的 80 端口接收数据和文件,结果发现自己的电脑 80 端口无法使用。说是权限问题。这应该...

  • MAC 接口转发

    mac与linux一样,1024以下的端口为特权端口,只有root用户才有权监听。 因此要使用80端口要么使用ro...

  • mac 使用 pf 做端口转发

    在使用mac os 进行web开发时,会遇到80端口已经被占用的情况。mac禁止了普通用户访问1024以下的端口,...

  • 解决Mac电脑 nginx 无法使用80端口代理其他端口

    一、Mac安装nginx并且使用 Mac nginx基本操作 二、解释为什么“Mac电脑 nginx 无法使用8...

网友评论

      本文标题:mac无法使用80端口问题

      本文链接:https://www.haomeiwen.com/subject/vxnxrrtx.html