
SearXNG 是一个免费的互联网元搜索引擎,它整合了来自超过 70 个搜索服务的结果。用户不会被跟踪或进行特征分析,很好地保护了用户隐私。
2022 年 11 月 OpenAI 发布 ChatGPT 后,大模型和知识库开始火爆,联网搜索成为弥补大模型知识陈旧的重要工具。提供元搜索功能的 SearXNG 开始被很多大模型应用比如 ChatNio[1] 采用,在大模型时代发挥了巨大作用。
本文将介绍如何基于 docker 部署私人的 SearXNG 服务,它既可以个人使用,也能整合到大模型应用中,是很重要的基础工具。
部署
单独部署 SearXNG 很简单,但是有一些注意事项。
官方专门维护了一个代码仓库 searxng-docker[2] 方便用户通过 docker 或者 docker compose 进行部署。
clone 代码仓库
git clone https://github.com/searxng/searxng-docker.git可选更新
.env中的 hostname 和电子邮件更新配置文件
searxng/settings.yml中的 secret key
# 下面的脚本仅支持 Linux 和 MacOS
sed -i "s|ultrasecretkey|$(openssl rand -hex 32)|g" searxng/settings.yml是否使用 Caddyfile?这个看个人,如果不需要,在
docker-compose.yaml中删除 caddy 相关配置即可删除
docker-compose.yaml中 chatnio 的cap_drop: - ALL,进行初次启动(后面还要加上的)
docker compose up -d待应用启动并生成
uwsgi.ini文件后,恢复docker-compose.yaml中 chatnio 的cap_drop: - ALL配置,重启 chatnio
docker compose down chatnio
docker compose up -d chatnio配置
本文使用默认配置+个别定制化配置的方式,来设置 SearXNG。
官方容器化部署仓库 searxng-docker[3] 中的定制化配置文件有:
searxng/limiter.tomlsearxng/settings.yml
searxng/limiter.toml 文件用于设置限流,文件中给出了配置的文档地址,可作为参考。
# This configuration file updates the default configuration file
# See https://github.com/searxng/searxng/blob/master/searx/botdetection/limiter.toml
[botdetection.ip_limit]
# activate link_token method in the ip_limit method
link_token = falsesearxng/settings.yml 的默认内容如下:
# see https://docs.searxng.org/admin/settings/settings.html#settings-use-default-settings
use_default_settings: true
server:
# base_url is defined in the SEARXNG_BASE_URL environment variable, see .env and docker-compose.yml
secret_key: "ultrasecretkey" # change this!
limiter: true # can be disabled for a private instance
image_proxy: true
ui:
static_use_hash: true
redis:
url: redis://redis:6379/0默认配置中规定了搜索引擎、返回内容的格式,以及限流和图片代理等。完整的默认配置内容在代码仓库中,默认配置地址[4]
下面是本文使用的定制化配置,其中指定了只使用 Google 搜索引擎、设置了返回格式:
# see https://docs.searxng.org/admin/settings/settings.html#settings-use-default-settings
use_default_settings:
engines:
keep_only:
- google
timeout: 10.0
server:
# base_url is defined in the SEARXNG_BASE_URL environment variable, see .env and docker-compose.yml
secret_key: "284f3ab907ae4deb557d8fe7ba504eef0f7c2245fbcfe562518d3b523064a2c7" # change this!
limiter: false # can be disabled for a private instance
image_proxy: false
search:
formats:
- html
- csv
- rss
- json
ui:
static_use_hash: true
redis:
url: redis://redis:6379/0搜索引擎设置
文档 engine[5] 给出了完整的搜索引擎配置选项。
大部分我们是用不到的,但是有以下几点需要注意:
搜索结果格式
默认返回 html 格式,但是通过 API 调用有时希望返回 json 格式,这时候就要调整返回格式,加上 json 格式。
# see https://docs.searxng.org/admin/settings/settings.html#settings-use-default-settings
use_default_settings: true
search:
formats:
- html
- csv
- rss
- json返回格式可以在 API 中指定,http://ip:port/search?q=xxx&format=json。
注意,如果 API 中指定了 format=json 但是 settings.xml 中没开启 json,这时候会报错,内容如下:

代理
**很多搜索引擎在大陆是不能使用的,原因你懂得~**因此 SearXNG 如果部署在大陆,最好设置下代理,以便一些搜索引擎能够正常使用。
设置格式为:
engine:
- name: google
engine: google
proxies:
http:
- http://proxy1:8080
https:
- http://proxy1:8080
- socks5://user:password@proxy3:1080
- socks5h://user:password@proxy4:1080超时 timeout
有时候网络环境不好,请求可能超时,这时候就要增加 timeout。默认超时配置是 3s,下面的配置改成 15s。
engine:
- name: google
engine: google
timeout: 15.0使用
SearXNG 支持直接在 UI 界面进行搜索,在浏览器访问地址,效果类似百度或者谷歌。

如果是本机部署,浏览器访问http://localhost:8080。如果是局域网部署,浏览器访问http://ip:8080,其中 ip 为 SearXNG 所在机器的 局域网 IP。下图是页面搜索结果:

下图是 API 搜索结果:

关于 SearXNG 的 API 用法,详情见:Search API[6]。
总结
SearXNG 的部署相对简单,根据官网的文档使用 docker 基本上开箱即用。
如果你很注重个人隐私,不希望自己的搜索记录被记录、追踪和分析,SearXNG 是很好的选择,它可以避免这些追踪。
同时,SearXNG 还能被集成到大模型知识库中,实现联网搜索功能,提升信息的实时性。
参考资料
[1]
ChatNio: https://github.com/zmh-program/chatnio
[2]searxng-docker: https://github.com/searxng/searxng-docker
[3]searxng-docker: https://github.com/searxng/searxng-docker.git
[4]默认配置地址: https://github.com/searxng/searxng/blob/master/searx/settings.yml
[5]engine: https://docs.searxng.org/admin/settings/settings_engine.html
[6]Search API: https://docs.searxng.org/dev/search_api.html

1万+

被折叠的 条评论
为什么被折叠?



