nginx返回带有状态代码的json文件

nginx返回带有状态代码的json文件,json,nginx,http-status-codes,Json,Nginx,Http Status Codes,我试图使Nginx返回一个静态json文件,我使用以下方法实现了这一点: location /health{ default_type "application/json"; alias /etc/health.json; } json文件包含: { "status" : "up" } 我需要做的是找到一种方法,根据响应的内容返回带有json文件的状态代码 任何帮助都将不胜感激 谢谢最简单的方法是将nginx作为反向代理运行,然后使用一些web服务器返回状态代码 网络服务器

我试图使
Nginx
返回一个静态json文件,我使用以下方法实现了这一点:

location /health{
   default_type "application/json";
   alias /etc/health.json;
}
json文件包含:

{
  "status" : "up"
}
我需要做的是找到一种方法,根据响应的内容返回带有json文件的状态代码

任何帮助都将不胜感激


谢谢

最简单的方法是将nginx作为反向代理运行,然后使用一些web服务器返回状态代码

网络服务器 例如,下面是一个简单的节点服务器,它执行以下操作:

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}`));
蟒蛇2示例(带烧瓶):

如果你甚至不能安装烧瓶,那么你可以使用。在这种情况下,您可能会定制SimpleHTTPRequestHandler来发送响应

Nginx配置 您的nginx配置需要包含到Web服务器的代理\u传递

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;
}

你可以在这里看到一个完整的例子:

我需要在返回状态为“向上”或“向下”之前进行一些逻辑分析。可能是我问题的副本。谢谢你的帮助,我想了一下,但问题是我在VM上没有任何服务器,只有Nginx2甚至python 2?许多编程语言都有一个内置的Web服务器,或者一个简单的库,你可以将请求转发到你控制的任何URL,它不必在同一个框中。我有python python2,你能编辑你的代码以匹配python2吗?
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;
}