由於概念證明我結束了這一點。如果不先檢查後果,請不要使用它。
events {
worker_connections 1024;
}
http {
upstream elasticsearch {
server 127.0.0.1:9200;
keepalive 15;
}
server {
listen: 8080;
# ES backend for search autocomplete
# 1. Allow only GET requests.
# 2. Capture the search_term which cannot contain the " character.
# 3. Rewrite the request to the desired index's _search elasticsearch endpoint.
# 4. Build the request body by concatenating the appropriate json with the search_term.
# 5. proxy_pass the translated request to elasticsearch.
location ~* ^/search/autocomplete/(?<search_term>[^"]+)$ {
if ($request_method != GET) {
return 403;
}
proxy_http_version 1.1;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
set $search_prefix '{"query":{"match":{"user.name":"';
set $search_suffix '"}},"highlight":{"pre_tags":["<strong>"],"post_tags":["</strong>"],"fields":{"user.name":{}}}}';
set $search $search_prefix$search_term$search_suffix;
proxy_set_body $search;
proxy_redirect off;
proxy_buffering off;
rewrite ^.*$ /my_index/blogpost/_search break;
proxy_pass http://elasticsearch;
}
# All other requests go to the web application server.
location/{
proxy_pass http://server;
}
}
}