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

iPhone真机无法连接WebSocket求助:IOS模拟器运行正常

Troubleshooting iPhone Real Device Connection Issues with Django + Channels + Redis + Nginx

Hey there, let’s break down why your app works flawlessly on the iOS simulator but fails to connect to your Google Cloud VM server on a real iPhone. That TCP Conn Failed : error 0:61 error translates to Connection Refused—so let’s walk through the most likely fixes step by step:

1. Check Google Cloud VM Firewall Rules

The simulator shares your computer’s network, which might already have access to the VM via internal IP or pre-approved firewall rules, but your real iPhone is accessing from the public internet.

  • Head to the Google Cloud Console > VPC Network > Firewall.
  • Make sure you have an inbound rule that allows traffic on the ports your app uses (e.g., 80 for HTTP, 443 for HTTPS, or the port your Daphne/ASGI server listens on like 8000).
  • Set the source IP range to 0.0.0.0/0 (for public access) or narrow it to your mobile network’s IP range if you prefer tighter security.

2. Verify Server Address & ATS Compliance

  • Avoid localhost/internal IP: If your app is hardcoded to connect to localhost or the VM’s internal IP, the simulator can reach it (since it shares your computer’s network), but the real iPhone can’t. Replace this with the VM’s public IP address or a registered domain name.
  • ATS Restrictions (iOS 14+): Apple blocks non-HTTPS connections by default via App Transport Security. If your server uses plain HTTP:
    • Either add an ATS exception in your Info.plist (not recommended for production):
      <key>NSAppTransportSecurity</key>
      <dict>
          <key>NSAllowsArbitraryLoads</key>
          <true/>
      </dict>
      
    • Or configure your server to use HTTPS with a valid SSL certificate (use Let’s Encrypt for free, trusted certificates).

3. Fix Nginx WebSocket Proxy Configuration

Nginx needs specific headers to properly proxy WebSocket traffic—if these are missing, connections will be refused. Update your Nginx config for the WebSocket endpoint:

location /ws/ {
    proxy_pass http://localhost:8000;  # Match your Daphne/ASGI port
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

After updating, restart Nginx with sudo systemctl restart nginx on your VM.

4. Ensure ASGI Server & Redis Are Configured Correctly

  • Daphne/ASGI Listening Address: Make sure your ASGI server (like Daphne) is listening on 0.0.0.0 instead of 127.0.0.1—this allows external connections. For example, start Daphne with:
    daphne -b 0.0.0.0 -p 8000 your_project.asgi:application
    
  • Redis Channel Layer: Verify your Django settings.py has the correct Redis configuration (ensure Redis is running locally on the VM):
    CHANNEL_LAYERS = {
        'default': {
            'BACKEND': 'channels_redis.core.RedisChannelLayer',
            'CONFIG': {
                "hosts": [('127.0.0.1', 6379)],
            },
        },
    }
    
    Check Redis is running with sudo systemctl status redis-server.

5. Test Directly from the iPhone

To rule out app-specific issues, open Safari on your iPhone and try accessing:

  • Your server’s HTTP endpoint (e.g., http://your-public-ip/api/) to confirm basic connectivity.
  • The WebSocket endpoint using a simple test tool—if the connection is still refused, you’ll know the issue is with the server/network, not your app code.

内容的提问来源于stack exchange,提问作者Thai Tran

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:33:08