zoukankan      html  css  js  c++  java
  • 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 '你好,世界!'; 
    }
  • 相关阅读:
    欧拉筛,线性筛,洛谷P2158仪仗队
    树形DP和状压DP和背包DP
    洛谷P1144最短路计数题解
    洛谷P1373小a和uim大逃离题解
    LCA
    108. Convert Sorted Array to Binary Search Tree
    230. Kth Smallest Element in a BST
    94. Binary Tree Inorder Traversal
    144. Binary Tree Preorder Traversal
    236. Lowest Common Ancestor of a Binary Tree
  • 原文地址:https://www.cnblogs.com/wjoyxt/p/11557625.html
Copyright © 2011-2022 走看看