Openwrt 安装 Nginx

OpenWrt 上安装 Nginx 非常简单,以下是标准步骤:


✅ 1. 更新软件源并安装 Nginx

通过 SSH 登录 OpenWrt 后执行:

opkg update
opkg install nginx-ssl

如果你只需要基础版本,也可以用 opkg install nginx,但建议用 nginx-ssl,支持 HTTPS。


✅ 2. 启动并设置开机自启

/etc/init.d/nginx start
/etc/init.d/nginx enable

✅ 3. 检查 Nginx 是否正常运行

nginx -t

输出 syntax is oktest is successful 表示配置无误。


✅ 4. 访问测试

在浏览器中访问你的路由器 IP(如 http://192.168.1.1),如果看到 Nginx 的欢迎页,即安装成功。


✅ 5. 配置说明(可选)

OpenWrt 的 Nginx 默认使用 UCI 管理配置,路径如下:

  • 主配置目录:/etc/nginx/
  • 默认站点配置:/etc/nginx/conf.d/uci.conf
  • 使用 uci 命令查看或修改配置(例如):
uci show nginx

✅ 6. 进阶:配置反向代理或 HTTPS(可选)

如需配置 HTTPS、反向代理或 Token 验证,可手动创建配置文件:

vi /etc/nginx/conf.d/my_site.conf


server {
    listen 443 ssl;
    server_name domain_name;
    ssl_protocols    TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_certificate     /etc/nginx/certs/xxx.crt;
    ssl_certificate_key /etc/nginx/certs/xxx.key;

    location / {
        proxy_pass_header Server;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_pass http://127.0.0.1:xxx;


    }

}

示例配置参考:


附录

一般来说,是启动不了的,因为 uttpd(Web 管理页面) 占据了 80 和 443 端口,需要调整.


✅ 目标:把 80.443 端口也腾出来给 Nginx


🔧 一步到位修改 uhttpd 配置:

uci set uhttpd.main.listen_http='8080'
uci set uhttpd.main.listen_https='0.0.0.0:8443 [::]:8443'
uci commit uhttpd
/etc/init.d/uhttpd restart

✅ 然后重启 Nginx:

/etc/init.d/nginx restart

✅ 验证端口占用:

netstat -tlnp | grep -E ':80|:443'

你应该看到:

  • 80 → nginx
  • 443 → nginx
  • 8080 → uhttpd(HTTP)
  • 8443 → uhttpd(HTTPS)

✅ 最终效果:

服务HTTP 端口HTTPS 端口
Nginx80443
uhttpd80808443

✅ 一句话总结:

把 uhttpd 的 HTTPS 端口从 443 改成 8443,Nginx 就能接管 443 并提供 HTTPS 服务了。

评论栏