近年来,Bing Search API 等商用搜索引擎价格一涨再涨,微软于 13 日宣布,将于 2025 年 8 月 11 日正式关闭 Bing Search API 服务,届时所有使用 Bing Search API 的实例将完全停用,同时不再接受新用户注册。作为免费的替代方案,SearXNG 是一个开源的、注重隐私的元搜索引擎,基于原始的 Searx 项目进行改进和优化。SearXNG 允许用户从多个搜索引擎中获取结果,同时避免被单一搜索引擎追踪。SearXNG 是很多 AI 搜索引擎的基础,广受欢迎的 Perplexica、FreeAskInternet 等流行 AI 搜索引擎都使用 SearXNG 进行底层抓取聚合。SearXNG 具有如下特性:
- 支持私有化部署
- 没有用户跟踪/分析
- 可以自定义设置脚本和 cookies
- 安全加密的连接
- 整合了近 200 个搜索引擎,用户可自定义选择不同的引擎
- 多语言支持,约 60 种,包含中文、英文等
- 提供近 100 个维护良好的公共查询实例
相比 Google、Bing 等 API 搜索的商用服务,SearXNG 提供了通过抓取本文方式的 web 搜索功能,本文简要介绍 SearXNG 的部署和使用方法,具体部署、发布过程参考「视频分享」。
安装与部署
多种部署方法
私有化安装与部署 SearXNG 没有特别的前备条件,但建议选择海外 VPS,以获得来自 google 等搜索引擎更为丰富、实时的抓取结果。
cd /opt git clone https://github.com/searxng/searxng.git #git clone https://github.com/searxng/searxng-docker.git cd searxng #cd searxng-docker
SearXNG 部署方法包括:
- docker compose 部署:拉取封装好的镜像,修改 docker-compose 配置文件对镜像进行取舍,并进行少量配置即可。
- 从源码安装部署:整个安装过程比较长,包括环境检查与更新、python 依赖安装、WSGI 安装、redis 安装与启动、缓存清理、反向代理安装等若干环节,过程中每个步骤都需要人工确认,默认 yes,可直接回车。
- 直接运行:修改少量配置后直接运行 SearXNG 脚本程序并置于后台。
pip install -r requirements.txt nano searx/settings.yml python -u searx/webapp.py >> dev/dull >> 2>&1 & ## * Running on http://127.0.0.1:8888/ (Press CTRL+C to quit) ## 也可将其手动添加为 systemd 守护
推荐部署方法
本文采用 docker 方式部署,直接拉取 searxng-docker 仓库。
cd /opt git clone https://github.com/searxng/searxng-docker.git cd /opt/searxng-docker/searxng nano settings.xml
分别修改 settings.xml 和 docker-compose.yaml 配置文件。/opt/searxng-docker/searxng/settings.xml 文件需要编辑的内容包括:
1.修改 secret_key 硬编码安全字符串或使用文中方式随机生成;
2.修改 limiter 选项为 false,不启用速率限制,避免 API 请求受限;
3.添加 search 块,确保支持 json 格式输出。
完成编辑之后,完整内容如下所示。
不包含 engines 块的情况下,SearXNG 将仅采用网页抓取的方式进行搜索。
# 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: $(openssl rand -hex 32) # change this! limiter: false # can be disabled for a private instance image_proxy: true ui: static_use_hash: true redis: url: redis://redis:6379/0 search: formats: - html - json
在 settings.xml 中,如果有 google、bing 等商用搜索引擎的 API key,可通过添加 engines 块进行配置。
engines: google: api_key: "YOUR_GOOGLE_API_KEY" cx: "YOUR_CUSTOM_SEARCH_ENGINE_ID" bing: api_key: "YOUR_BING_API_KEY"
接下来,编辑 docker-compose.yaml 文件。如果服务器上已经安装了 nginx、apache 等 web server,可以删除配置中的 caddy 块,复用系统中已有的 web server。同时,如果系统中已安装 redis,此处的 redis 块也可删除。本文以安装有 aapanel 的 VPS 为例,已经安装有 LNMP ,caddy 块删除。
对于实例环境(已有 nginx,未安装 redis),完整的 docker-compose.yaml 文件如下,注意底部的映射块(volumes)也应进行相应的调整。
version: "3.7" services: redis: container_name: redis image: docker.io/valkey/valkey:8-alpine command: valkey-server --save 30 1 --loglevel warning restart: unless-stopped networks: - searxng volumes: - valkey-data2:/data logging: driver: "json-file" options: max-size: "1m" max-file: "1" searxng: container_name: searxng image: docker.io/searxng/searxng:latest restart: unless-stopped networks: - searxng ports: - "127.0.0.1:8080:8080" volumes: - ./searxng:/etc/searxng:rw environment: - SEARXNG_BASE_URL=https://${SEARXNG_HOSTNAME:-localhost}/ - UWSGI_WORKERS=${SEARXNG_UWSGI_WORKERS:-4} - UWSGI_THREADS=${SEARXNG_UWSGI_THREADS:-4} logging: driver: "json-file" options: max-size: "1m" max-file: "1" networks: searxng: volumes: valkey-data2:
接下来,启动 SearXNG。
docker compose up -d
配置 Nginx 绑定域名
所有服务安装并正常运行时,接下来就可以将 SearXNG 添加到 Web server 中,如 nginx、apache。本文以 aapanel 下的 nginx 为例,绑定域名,其中的核心配置如下:
location / { proxy_http_version 1.1; proxy_set_header Connection ""; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_protocol_addr; proxy_pass http://127.0.0.1:8080; client_max_body_size 0; }
配置 SSL 证书及自动更新
域名绑定后,我们还需要配置 SSL 证书,可使用以下命令,修改邮箱、Cloudflare token、域名为实际有效的内容即可。这里使用 buypass 证书,虽然不支持泛域名证书,但支持多域名并且拥有 180 天的有效期,非常推荐。
备注:
-- aapanel/宝塔中创建的网站,其证书目录默认为 /www/server/panel/vhost/cert/站点域名/,此处示例为/www/server/panel/vhost/cert/search.example.com/
-- aapanel/宝塔中配置 SSL,默认存储的证书私钥文件分别为fullchain.pem、privkey.pem
-- 通过 acme.sh 请求多域名证书,命令中的第一个域名为该证书的名称
curl https://get.acme.sh | sh -s email=example@outlook.com cd ~/.acme.sh ./acme.sh --set-default-ca --server buypass export CF_Token="......" export CF_Email="test@gmail.com" ./acme.sh --issue --dns dns_cf -d example.com -d search.example.com --keylength 2048 --days 150 mkdir -p /www/server/panel/vhost/cert/search.example.com ./acme.sh --install-cert -d example.com \ --key-file /www/server/panel/vhost/cert/search.example.com/privkey.pem \ --fullchain-file /www/server/panel/vhost/cert/search.example.com/fullchain.pem \ --reloadcmd "nginx -s reload"
完成以上工作后,还需要在运维面板中启用 SSL,同时还可强制使用 https,手动编辑 nginx 配置文件的,现代的做法是使用 497 错误码重定向。
在浏览器中就可以使用 https://search.example.com 访问专属搜索引擎了。
SearXNG Search API 搜索支持 GET 和 POST 两种格式,默认提供了两个端点 / 和 /search。搜索查询参数进一个必选字段query/q,此字符串将传递给外部搜索服务,因此 SearXNG 支持每个搜索服务的语法,例如 site:github.com 就是 google 等搜索引擎的有效查询。需要注意的是,SearXNG 并不使用搜索引擎的官方 API,而是(默认行为)直接通过模拟用户访问的方式抓取网页,这是免费的“秘诀”,只要使用商用搜索引擎的官方 API,就一定会产生费用 。
商用搜索引擎用不起甚至不让用是大势所趋,想要免费使用或开发测试,SearXNG 一类的支持抓取方式的开源搜索引擎,无论是隐私保护需求还是需要多引擎结果,或者开发和研究者的特定搜索需求,都是现阶段最佳的解决方案。
更多精彩内容,敬请关注「老E的博客」!
文章评论