Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure nginx to return text or json

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to configure the return text or json for nginx" with detailed content, clear steps and proper handling of the details. I hope that this article "how to configure the nginx to configure the return text or json" can help you solve your doubts.

First, fixed text and json are returned. You can configure location interception in server. The configuration example is as follows:

Fixed text:

Location ~ ^ / get_text {default_type text/html; return 200' this is textbooks;}

Fixed json:

Location ~ ^ / get_json {default_type application/json; return 200'{"status": "success", "result": "nginx json"}';}

After saving, reloading the configuration will take effect. Note: default_type must be added, otherwise the browser will download it as an unrecognized file.

Alternatively, you can simply return different strings based on the requested url, as shown below:

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

In this way, you can simply intercept the strings in url, of course, you can also use (. *) to match all of them. In practice, you can define them according to different requirements.

The above are some simple examples. For simple processing in a server environment, making full use of nginx can save some programming work.

In addition, I would like to add the problem of Chinese display, because utf-8 character encoding is used under linux, by default, our browser will render the page with gbk encoding when the server does not specify the encoding or the static page does not declare the encoding. If the browser returns Chinese by default, the browser uses gbk to parse the utf-8 code, obviously garbled. At this point, you need to actively add header to the nginx location block to output the correct encoding, adding the following content: add_header content-type 'text/html Charset=utf-8'; so that the browser knows which code we are using, as shown in the following figure:

Or it is possible to change the add_header line to charset utf-8;.

After reading this, the article "how to configure nginx to return text or json" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report