打赏

星辰大海ゞ

That which does not kill us makes us stronger!

导航

Nginx返回固定json或者文本格式的方法

在系统还没有做集群的情况下,直接重启项目时刚好用户在使用的话,一般都会受到投诉,那么使用nginx返回类似“系统维护”的提示信息并且提前在应用上面做通知才是合适的做法

 

那么记录一下nginx里面的配置

server{
        listen xx;
        add_header Content-Type 'text/html; charset=utf-8';
        return 200 '{"msg":"系统临时维护中,请您耐心等待","code":10,"data":""}';

}

listen xx 表示监听的端口,我常用的做法时在维护的时候使用防火墙端口转发过来

第二行add_header解决的是浏览器中文乱码的问题

第三行就是你所要提示的信息格式

 

1、返回文本格式

1
2
3
4
location ~ ^/get_text {
  default_type text/html;
  return 200 'hello world!';
}

2、返回json格式

1
2
3
4
location ~ ^/get_json {
  default_type application/json;
  return 200 '{"status":"success","result":"hello world!"}';
}

3、也可以简单的根据请求的URL返回不同的字符串

1
2
3
4
5
6
location ~ ^/get_text/article/(.*)_(\d+).html$ {
  default_type text/html;
  set $s $1;
  set $d $2;
  return 200 str:$s$d;
}

4、返回的字符集设置,默认是以GBK字符集返回

1
2
3
4
5
location ~ ^/get_text {
  default_type text/html;
  add_header Content-Type 'text/html; charset=utf-8';
  return 200 '你好,世界!'; 
}

 

 

posted on 2019-09-20 15:42  星辰大海ゞ  阅读(2308)  评论(0编辑  收藏  举报