我今天遇到了類似的情況,希望廣播對一些我的服務器正在監聽的API來觸發git的拉一個GET。我無法在Nginx中找到任何處理這種行爲的方法,而且我也不想跳過編寫擴展程序。
相反,我砍死在一起使用Python的瓶的東西,並請求模塊:
from flask import Flask, jsonify, request
import requests
app = Flask(__name__)
# add whatever you need here to the methods
@app.route("/http/replay/<method>", methods=["POST", "GET", "PUT", "OPTIONS"])
def http_replay(method):
"""Replay an incoming request of type <method> against the parameter list of endpoints"""
endpoint_list = request.args.get("host", "")
endpoint_list = endpoint_list.split(";")
timeout = request.args.get("timeout", None) or 5
if not endpoint_list:
return jsonify(status=500,
message="Expected parameters in the form of ?host=http://host/path;host=http://host2/path")
else:
responses = []
for ep in endpoint_list:
try:
_r = requests.__getattribute__(method.lower())(ep, timeout=timeout)
status_code = _r.status_code
except requests.exceptions.Timeout:
status_code = 408 # request timeout
except requests.exceptions.RequestException:
status_code = 520
responses.append(status_code)
_r_status = set(responses)
if len(_r_status) == 1 and _r_status.pop == 200:
status = 200
else:
status = 520
return jsonify(status=status)
這只是監聽路徑上,並允許我發送一個請求,如:
curl "http://localhost:5000/plugins/http/replay/POST?host=http://localhost:80/api;host=http://dev.localhost:8080/api?timeout=10"
和回報如果URL參數中的所有端點都相同,則爲HTTP 200狀態碼。
你想發送同樣的請求到所有4個實例嗎?然後他們都會回答。我不認爲在* nginx *中是可能的,但代碼更改不應該很難應付這種行爲。 –