我正在尝试让Nginx
返回一个静态的json文件,我使用了以下方式:
location /health{
default_type "application/json";
alias /etc/health.json;
}
并且json文件包含:
{
"status" : "up"
}
我需要做的是找到一种方式,在响应内容的基础上返回JSON文件的状态码。任何帮助将不胜感激。谢谢。
我正在尝试让Nginx
返回一个静态的json文件,我使用了以下方式:
location /health{
default_type "application/json";
alias /etc/health.json;
}
并且json文件包含:
{
"status" : "up"
}
const http = require('http');
const fs = require('fs');
const filename = '/path/to/your/file.json';
const server = http.createServer((request, response) => {
fs.readFile(filename, 'utf8', (json) => {
const data = JSON.parse(json);
const statusCode = data.status === 'up' ? 200 : 503;
response.writeHead(status, {"Content-Type": "application/json"});
response.write(json);
});
response.end();
});
const port = process.env.PORT || 3000;
server.listen(port, (e) => console.log(e ? 'Oops': `Server running on http://localhost:${port}`));
import json
from flask import Flask
from flask import Response
app = Flask(__name__)
@app.route('/')
def health():
contents = open('health.json').read()
parsed = json.loads(contents)
status = 200 if parsed[u'status'] == u'up' else 503
return Response(
response=contents,
status=status,
mimetype='application/json'
)
您的Nginx配置需要包含一个代理传递到您的Web服务器。
location /health {
proxy_set_header Host $host;
proxy_set_header X-REAL-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3000;
}