> 技术文档 > 使用Docker搭建SearXNG搜索引擎

使用Docker搭建SearXNG搜索引擎


1、安装Docker

# 安装Dockerhttps://docs.docker.com/get-docker/# 安装Docker Composehttps://docs.docker.com/compose/install/# CentOS安装Dockerhttps://mp.weixin.qq.com/s/nHNPbCmdQs3E5x1QBP-ueA

2、安装SearXNG

详见:https://docs.searxng.org/admin/installation-docker.htmlhttps://github.com/searxng/searxng-docker

创建目录:

mkdir searxngcd searxng

下载:

wget https://github.com/searxng/searxng-docker/archive/refs/heads/master.zip

解压:

# 安装zip、unzip# yum install -y zip unzip# 解压unzip master.zip

切换目录:

cd searxng-docker-master

查看.env文件

# By default listen on https://localhost# To change this:# * uncomment SEARXNG_HOSTNAME, and replace  by the SearXNG hostname# * uncomment LETSENCRYPT_EMAIL, and replace  by your email (require to create a Let\'s Encrypt certificate)# SEARXNG_HOSTNAME=# LETSENCRYPT_EMAIL=

备份.env文件:

cp .env .env-bak

修改.env文件:

# 指定域名或ip,# 假设当前ip为192.168.186.128,端口3000,供外网访问sed -i \'s/# SEARXNG_HOSTNAME=/SEARXNG_HOSTNAME=http:\\/\\/192.168.186.128:3000/g\' .env

查看Caddyfile文件:

{admin offlog {output stderrformat filter {# Preserves first 8 bits from IPv4 and 32 bits from IPv6request>remote_ip ip_mask 8 32request>client_ip ip_mask 8 32# Remove identificable informationrequest>remote_port deleterequest>headers deleterequest>uri query {delete urldelete hdelete q}}}servers {client_ip_headers X-Forwarded-For X-Real-IP# Allow the following IP to passthrough the \"X-Forwarded-*\" headers to SearXNG# https://caddyserver.com/docs/caddyfile/options#trusted-proxiestrusted_proxies static private_rangestrusted_proxies_strict}}{$SEARXNG_HOSTNAME}tls {$SEARXNG_TLS}encode zstd gzip@api {path /configpath /healthzpath /stats/errorspath /stats/checker}@static {path /static/*}@imageproxy {path /image_proxy}header {# CSP (https://content-security-policy.com)Content-Security-Policy \"upgrade-insecure-requests; default-src \'none\'; script-src \'self\'; style-src \'self\' \'unsafe-inline\'; form-action \'self\' https:; font-src \'self\'; frame-ancestors \'self\'; base-uri \'self\'; connect-src \'self\'; img-src * data:; frame-src https:;\"# Disable browser featuresPermissions-Policy \"accelerometer=(),camera=(),geolocation=(),gyroscope=(),magnetometer=(),microphone=(),payment=(),usb=()\"# Only allow same-origin requestsReferrer-Policy \"same-origin\"# Prevent MIME type sniffing from the declared Content-TypeX-Content-Type-Options \"nosniff\"# Comment header to allow indexing by search enginesX-Robots-Tag \"noindex, nofollow, noarchive, nositelinkssearchbox, nosnippet, notranslate, noimageindex\"# Remove \"Server\" header-Server}header @api {Access-Control-Allow-Methods \"GET, OPTIONS\"Access-Control-Allow-Origin \"*\"}route {# Cache policyheader Cache-Control \"no-cache\"header @static Cache-Control \"public, max-age=30, stale-while-revalidate=60\"header @imageproxy Cache-Control \"public, max-age=3600\"}# SearXNGreverse_proxy localhost:8080

备份Caddyfile文件:

cp Caddyfile Caddyfile-bak

修改Caddyfile文件:

sed -i \'s/admin off/#admin off\\n http_port 3000\\n auto_https off/g\' Caddyfilesed -i \'s/tls {$SEARXNG_TLS}/#tls {$SEARXNG_TLS}/g\' Caddyfilesed -i \'s/Content-Security-Policy/#Content-Security-Policy/g\' Caddyfile

查看searxng/settings.yml文件:

# see https://docs.searxng.org/admin/settings/settings.html#settings-use-default-settingsuse_default_settings: trueserver: # base_url is defined in the SEARXNG_BASE_URL environment variable, see .env and docker-compose.yml secret_key: \"ultrasecretkey\" # change this! limiter: false # enable this when running the instance for a public usage on the internet image_proxy: trueredis: url: redis://redis:6379/0

备份searxng/settings.yml文件:

cp searxng/settings.yml searxng/settings.yml-bak

修改searxng/settings.yml文件:

sed -i \"s/ultrasecretkey/$(openssl rand -hex 32)/g\" searxng/settings.ymlsed -i \"s/redis:6379/searxng-redis:6379/g\" searxng/settings.yml

查看docker-compose.yaml文件:

version: \"3.7\"services: caddy: container_name: caddy image: docker.io/library/caddy:2-alpine network_mode: host restart: unless-stopped volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - caddy-data:/data:rw - caddy-config:/config:rw environment: - SEARXNG_HOSTNAME=${SEARXNG_HOSTNAME:-http://localhost} - SEARXNG_TLS=${LETSENCRYPT_EMAIL:-internal} logging: driver: \"json-file\" options: max-size: \"1m\" max-file: \"1\" 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 - searxng-data:/var/cache/searxng:rw environment: - SEARXNG_BASE_URL=https://${SEARXNG_HOSTNAME:-localhost}/ logging: driver: \"json-file\" options: max-size: \"1m\" max-file: \"1\"networks: searxng:volumes: caddy-data: caddy-config: valkey-data2: searxng-data:

备份docker-compose.yaml文件:

cp docker-compose.yaml docker-compose.yaml-bak

修改docker-compose.yaml文件:

services: caddy: container_name: searxng-caddy image: caddy:2-alpine network_mode: host restart: unless-stopped volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - ./caddy-data:/data:rw - ./caddy-config:/config:rw environment: - SEARXNG_HOSTNAME=${SEARXNG_HOSTNAME} logging: driver: \"json-file\" options: max-size: \"1m\" max-file: \"1\" redis: container_name: searxng-redis image: 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: searxng/searxng:latest restart: unless-stopped networks: - searxng ports: - \"127.0.0.1:8080:8080\" volumes: - ./searxng:/etc/searxng:rw - ./searxng-data:/var/cache/searxng:rw environment: - SEARXNG_BASE_URL=${SEARXNG_HOSTNAME} logging: driver: \"json-file\" options: max-size: \"1m\" max-file: \"1\"networks: searxng:
说明:使用caddy做反向代理假设ip为192.168.186.128,caddy默认端口80,searxng默认端口8080在浏览器访问192.168.186.128:80如果将caddy端口改成3000,那么在浏览器访问192.168.186.128:3000

创建并启动容器

docker-compose up -d

查看容器列表:

docker ps

查看容器日志:

# Caddy容器: docker logs -f searxng-caddy# SearXNG容器: docker logs -f searxng# Valkey容器: docker logs -f searxng-redis

停止并销毁容器:

docker-compose down

删除目录:

rm -rf ./caddy-data ./caddy-config ./valkey-data2 ./searxng-data

3、浏览器访问

假设当前ip为192.168.186.128浏览器访问:http://192.168.186.128:8080

4、详见

https://docs.searxng.org/https://github.com/searxng/searxnghttps://github.com/searxng/searxng-dockerhttps://mp.weixin.qq.com/s/04sosQUYlnabyC2fa-5PIA