简介

UCI是OpenWrt系统的配置管理工具,主要用于网络设备和路由器的配置。配置文件是uci.conf,不能直接编辑,需要通过uci命令进行修改。本文以OpenWRT为例。

安装

1
2
opkg update
opkg install luci-nginx luci-ssl-nginx

uci命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Usage: uci [<options>] <command> [<arguments>]

Commands:
batch
export [<config>]
import [<config>]
changes [<config>]
commit [<config>]
add <config> <section-type>
add_list <config>.<section>.<option>=<string>
del_list <config>.<section>.<option>=<string>
show [<config>[.<section>[.<option>]]]
get <config>.<section>[.<option>]
set <config>.<section>[.<option>]=<value>
delete <config>[.<section>[[.<option>][=<id>]]]
rename <config>.<section>[.<option>]=<name>
revert <config>[.<section>[.<option>]]
reorder <config>.<section>=<position>

Options:
-c <path> set the search path for config files (default: /etc/config)
-d <str> set the delimiter for list values in uci show
-f <file> use <file> as input instead of stdin
-m when importing, merge data into an existing package
-n name unnamed sections on export (default)
-N don't name unnamed sections
-p <path> add a search path for config change files
-P <path> add a search path for config change files and use as default
-t <path> set save path for config change files
-q quiet mode (don't print error messages)
-s force strict mode (stop on parser errors, default)
-S disable strict mode
-X do not use extended syntax on 'show'

示例

查看当前配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
uci show nginx

# 输出内容
root@ImmortalWrt:/etc/nginx# uci show nginx
nginx.global=main
nginx.global.uci_enable='true'
nginx._lan=server
nginx._lan.listen='443 ssl default_server' '[::]:443 ssl default_server'
nginx._lan.server_name='_lan'
nginx._lan.include='restrict_locally' 'conf.d/*.locations'
nginx._lan.uci_manage_ssl='self-signed'
nginx._lan.ssl_certificate='/etc/nginx/conf.d/_lan.crt'
nginx._lan.ssl_certificate_key='/etc/nginx/conf.d/_lan.key'
nginx._lan.ssl_session_cache='shared:SSL:32k'
nginx._lan.ssl_session_timeout='64m'
nginx._lan.access_log='off; # logd openwrt'
nginx._redirect2ssl=server
nginx._redirect2ssl.listen='80' '[::]:80'
nginx._redirect2ssl.server_name='_redirect2ssl'
nginx._redirect2ssl.return='302 https://$host$request_uri'

删除nginx._redirect2ssl.return:

1
uci delete nginx._redirect2ssl.return

设置nginx._redirect2ssl.return:

1
uci set nginx._redirect2ssl.return='302 https://$host$request_uri'

删除端口(不监听ipv6):

1
2
uci del_list nginx._redirect2ssl.listen='[::]:80'
uci del_list nginx._lan.listen='[::]:443 ssl default_server'

添加端口:

1
2
uci add_list nginx._redirect2ssl.listen='8080 default_server'
uci add_list nginx._redirect2ssl.listen='[::]:8080 default_server'

修改完成后记得提交:

1
2
3
uci commits
/etc/init.d/nginx reload
service nginx restart
1
2
3
4
uci set nginx.@server[0].listen='80'
uci set nginx.@server[0].server_name='example.com'
uci set nginx.@server[0].location='/'
uci set nginx.@server[0].location.@file[0].path='/var/www/html'