惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

小威博客

Tencent EdgeOne 腾讯云CDN免费申请兑换计划申请指南 – 小威博客 XArrPay支付系统个人开心版 – 小威博客 雷池WAF安全加固:基于OAuth 2.0与微信开放平台的双因子认证 – 小威博客 雷池WAF离线安装搭建全流程指南(2025年最新版) – 小威博客 雷池WAF社区版安装配置全指南:从零搭建网站防护体系 – 小威博客 宝塔面板+雷池WAF实战教程:双服务器分离部署实现网站高安全防护 – 小威博客 Windows 安装 NVM(Node.js 版本管理工具)教程与配置方法 – 小威博客 解决 Debian 10 中的 Locale 设置问题 – 小威博客 尊狐云 Linux 工具箱 – 小威博客
使用Docker和Nginx搭建STUN服务器并配置反向代理 – 小威博客
萌新 · 2024-08-13 · via 小威博客

使用Docker和Nginx搭建STUN服务器并配置反向代理

1. 背景介绍

STUN(Session Traversal Utilities for NAT)服务器在 WebRTC 等应用中用于帮助客户端获取其外部网络地址,尤其是在 NAT(Network Address Translation)环境下。使用 Docker 可以简化 STUN 服务器的部署,而通过 Nginx 反向代理并配置 HTTPS,可以提高服务器的安全性并方便用户通过域名访问。

2. 环境准备

  • 一台配置了 Docker 和 Nginx 的 Linux 服务器(本文以 Ubuntu 为例)。

  • 一个可用的域名(本文假设为 stun.example.com)。

  • 能够访问互联网,用于获取 SSL 证书(例如使用 Let’s Encrypt 提供的免费证书)。

3.1 拉取 Docker 镜像

首先,拉取 coturn/coturn Docker 镜像。这个镜像包含了流行的 coturn STUN/TURN 服务器实现。

docker pull coturn/coturn

3.2 运行 coturn Docker 容器

运行容器时需要指定一些必要的配置,以便 STUN 服务器可以正常工作:

docker run -d --name coturn-stun \
  -p 3478:3478/udp \
  -p 3478:3478/tcp \
  coturn/coturn \
  turnserver \
  --no-auth \
  --listening-port=3478 \
  --listening-ip=0.0.0.0 \
  --external-ip=你的服务器公网IP \
  --min-port=49160 \
  --max-port=49200 \
  --no-tls \
  --no-dtls

参数说明:

  • -p 3478:3478/udp-p 3478:3478/tcp:将容器内部的 STUN 服务端口映射到主机的 3478 端口。

  • --no-auth:禁用认证,允许匿名 STUN 请求。

  • --listening-port=3478:指定 STUN 服务的监听端口(默认 3478)。

  • --listening-ip=0.0.0.0:监听所有网络接口的连接请求。

  • --external-ip=你的服务器公网IP:指定服务器的外部 IP 地址,帮助客户端获取正确的外部 IP。

  • --no-tls--no-dtls:禁用 TLS 和 DTLS,因为我们将通过 Nginx 来处理 HTTPS。

4. 配置 Nginx 反向代理

4.1 安装 Nginx

如果你尚未安装 Nginx,可以通过以下命令安装:

sudo apt-get update
sudo apt-get install nginx

4.2 获取 SSL 证书

使用 Certbot 来获取 Let’s Encrypt 提供的免费 SSL 证书。

安装 Certbot 和 Nginx 插件

sudo apt-get install certbot python3-certbot-nginx

获取证书

运行以下命令为你的域名获取 SSL 证书:

sudo certbot --nginx -d stun.example.com

Certbot 将自动为 Nginx 配置 SSL,并设置证书的自动续期。

4.3 配置 Nginx 反向代理

在 Certbot 配置完成后,你可以检查 /etc/nginx/sites-available/stun.example.com 文件,确保 Nginx 正确代理到 Docker 中的 STUN 服务器。

编辑配置文件:

sudo nano /etc/nginx/sites-available/stun.example.com

配置文件内容如下:

server {
    listen 80;
    server_name stun.example.com;
    return 301 https://$host$request_uri;  # 强制重定向到 HTTPS
}
​
server {
    listen 443 ssl;
    server_name stun.example.com;
​
    ssl_certificate /etc/letsencrypt/live/stun.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/stun.example.com/privkey.pem;
​
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
​
    location / {
        proxy_pass http://localhost:3478;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

4.4 重新加载 Nginx 配置

应用更改并重新加载 Nginx 配置:

sudo nginx -t
sudo systemctl reload nginx

5. 验证 STUN 服务器

配置完成后,你可以使用 stunclient 或类似工具来验证 STUN 服务器是否正常工作:

stunclient stun.example.com:443

如果 stunclient 返回了客户端的外部 IP 地址,说明配置成功。

6. 总结

通过以上步骤,我们使用 coturn/coturn Docker 镜像成功搭建了一个 STUN 服务器,并通过 Nginx 反向代理配置了 HTTPS 访问。这种架构不仅提高了服务器的安全性,还使用户可以通过域名方便地访问 STUN 服务。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

尊狐云 Linux 工具箱

2024-8-16 10:34:17

个人中心

购物车

优惠劵

今日签到

有新私信 私信列表

搜索

幸运之星正在降临...

点击领取今天的签到奖励!

恭喜!您今天获得了{{mission.data.mission.credit}}积分

  • 限制以下商品使用: 限制以下商品分类使用: 不限制使用:

    所有商品和商品类型均可使用