创建配置文件: 使用以下命令将上述内容写入 /etc/nginx/conf.d/luci.conf:
[[定义一个虚拟主机(server]] block),用于处理 LuCI 的 HTTP 和 HTTPS 请求
server {
# 监听 IPv4 和 IPv6 的 80 端口(HTTP)
listen 80;
listen [::]:80;
# 监听 IPv4 和 IPv6 的 443 端口(HTTPS)
listen 443 ssl;
listen [::]:443 ssl;
# 虚拟主机的名称,可以是域名或任意标识符,这里使用 'luci'
server_name luci;
# SSL 证书路径(用于 HTTPS 加密通信)
ssl_certificate /etc/nginx/ssl/server.crt; # 证书文件路径
ssl_certificate_key /etc/nginx/ssl/server.key; # 私钥文件路径
# 处理根路径(/)的请求,通常用于 LuCI 的静态文件或首页
location / {
root /www; # 静态文件根目录,LuCI 的 HTML、JS、CSS 文件通常位于 /www
index index.html index.htm; # 默认首页文件
}
# 处理 LuCI 的动态请求(CGI 脚本),通过 uWSGI 协议与 LuCI 的后端通信
location /cgi-bin/luci {
include /etc/nginx/uwsgi_params; # 加载 Nginx 的 uWSGI 标准参数
uwsgi_pass unix:/var/run/luci-webui.socket; # uWSGI 服务的 socket 文件路径
}
# 处理静态文件(如 JS、CSS、图片等),设置缓存以提高性能
location ~* \.(js|css|png|jpg|gif|ico|html)$ {
root /www; # 静态文件根目录
expires 30d; # 缓存 30 天,减少服务器负载
}
}
-
验证配置文件: 检查 Nginx 配置文件语法是否正确:
nginx -t -
重启 Nginx: 应用配置并重启 Nginx:
/etc/init.d/nginx restart
配置 LuCI 的 uWSGI
LuCI 使用 uWSGI 协议与 Nginx 通信。确保 LuCI 的 uWSGI 服务正确配置。
编辑 /etc/config/luci:
uci set luci.main.uhttpd='0' # 禁用 uhttpd
uci set luci.main.uwsgi='1' # 启用 uWSGI
uci commit luci
编辑 /etc/config/uwsgi(如果不存在,手动创建):
config uwsgi 'luci'
option socket '/var/run/luci-webui.socket'
option master '1'
option processes '2'
option threads '2'
option cgi '/cgi-bin/luci=/usr/lib/lua/luci/dispatcher.lua'
option cgi_prefix '/cgi-bin/luci'
启动 uWSGI 服务:
/etc/init.d/uwsgi start
/etc/init.d/uwsgi enable
- 禁用 uhttpd
确保 uhttpd 不占用 80 或 443 端口
uci set uhttpd.main.listen_http='0'
uci set uhttpd.main.listen_https='0'
uci commit uhttpd
/etc/init.d/uhttpd stop
/etc/init.d/uhttpd disable
- 启动 Nginx
启动并启用 Nginx 服务:
/etc/init.d/nginx start
/etc/init.d/nginx enable
- 配置防火墙
为了从互联网访问 LuCI,需要开放 80(HTTP)或 443(HTTPS)端口。
编辑 /etc/config/firewall:
uci add firewall rule
uci set firewall.@rule[-1].name='Allow-HTTP'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].dest_port='80'
uci set firewall.@rule[-1].proto='tcp'
uci set firewall.@rule[-1].target='ACCEPT'
uci add firewall rule
uci set firewall.@rule[-1].name='Allow-HTTPS'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].dest_port='443'
uci set firewall.@rule[-1].proto='tcp'
uci set firewall.@rule[-1].target='ACCEPT'
uci commit firewall
/etc/init.d/firewall restart
- 配置动态 DNS(可选)
如果你的路由器使用动态 IP 地址,建议配置 DDNS 服务以便通过域名访问。
安装 DDNS 软件包:
opkg install luci-app-ddns ddns-scripts
在 LuCI 界面(或通过 UCI)配置 DDNS,指向你的域名提供商(如 No-IP、DuckDNS 等)。
-
测试访问
-
本地测试:在浏览器中访问 http://<路由器LAN IP> 或 https://<路由器LAN IP>。
-
互联网测试:使用你的公网 IP 或 DDNS 域名访问 http://<公网IP或域名> 或 https://<公网IP或域名>。
如果无法访问,检查:
-
Nginx 是否运行:ps | grep nginx
-
uWSGI 是否运行:ps | grep uwsgi
-
防火墙规则是否正确:uci show firewall
-
端口转发是否配置正确(如果路由器在 NAT 后)。