You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ISP仅开放25端口时,Mac如何在该端口运行HTTP服务器?

How to Run an HTTP Server on Port 25 on macOS (Bypassing ISP & System Restrictions)

Got it, let's tackle your problem head-on. Your ISP has locked down all ports except 25, but macOS blocks non-SMTP processes from using port 25 by default (hence the "not allowed to use restricted network port" error). Here are three workable solutions to get your HTTP server accessible via port 25:

1. Port Forwarding with Socat

This is the quickest way to test things out—we'll forward traffic from port 25 to a non-restricted port (like 8080) where your HTTP server can run normally.

  • First, install socat if you don't have it (use Homebrew: brew install socat)
  • Run the forwarding command as root:
    sudo socat TCP-LISTEN:25,fork TCP:localhost:8080
    
  • Now start your HTTP server on port 8080 (no root needed, e.g., python3 -m http.server 8080)
  • When someone visits xx.xxx.xxx.xx:25, their traffic will be routed to your server on 8080.

Pros: No permanent system changes, easy to set up temporarily.
Cons: You need to keep the socat process running in the background.

2. Modify System Port Reservations

macOS reserves port 25 for SMTP in the /etc/services file. By commenting out this reservation, you can run an HTTP server on port 25 directly (with root privileges).

  • Edit the services file with root access:
    sudo nano /etc/services
    
  • Find the line that looks like this:

    smtp 25/tcp # Simple Mail Transfer Protocol

  • Add a # at the start to comment it out:

    #smtp 25/tcp # Simple Mail Transfer Protocol

  • Save the file (Ctrl+O, then Enter) and exit (Ctrl+X)
  • Restart your system to apply the changes
  • Now you can run your HTTP server on port 25 as root:
    sudo python3 -m http.server 25
    

Pros: Directly uses port 25 without extra tools.
Cons: May break system or third-party SMTP services (like Mail.app) that rely on port 25. Remember to uncomment the line if you need SMTP functionality later.

3. Reverse Proxy with Nginx

This is the most robust solution for long-term use. Nginx will listen on port 25 (as root) and forward requests to your HTTP server running on a non-restricted port.

  • Install Nginx if you don't have it: brew install nginx
  • Edit the Nginx configuration file (usually at /usr/local/etc/nginx/nginx.conf or /etc/nginx/nginx.conf):
    Add this server block inside the http section:
    server {
        listen 25;
        server_name xx.xxx.xxx.xx;
    
        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
    
  • Reload Nginx to apply changes:
    sudo nginx -s reload
    
  • Start your HTTP server on port 8080 (e.g., python3 -m http.server 8080)

Pros: Stable, doesn't interfere with system SMTP (since Nginx just forwards traffic), easy to manage multiple services.
Cons: Requires setting up and maintaining Nginx.


内容的提问来源于stack exchange,提问作者nobody user

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:39:02