Sometimes when requesting certain interfaces, you need to return a specified text string or json string. If the logic is very simple or simply a fixed string, you can use nginx to quickly implement it, so you don't have to write a program to respond to the request, which can reduce server resource usage and have very fast response performance. First, let's look at returning fixed text and JSON. All you need to do is configure location interception in the server. The configuration example is as follows: Fixed text: location ~ ^/get_text { default_type text/html; return 200 'This is text!'; } Fixed json: location ~ ^/get_json { default_type application/json; return 200 '{"status":"success","result":"nginx json"}'; } After saving, reload the configuration to 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; } This way you can simply intercept the string in the URL. Of course, you can also use (.*) to match all of them. In practice, you can define it according to different needs. The above are some simple cases. For simple processing in the server environment, making full use of nginx can save some programming work. In addition, let me add the problem of Chinese display. Because Linux uses utf-8 character encoding, by default, our browser will render the page in GBK encoding when the server does not specify the encoding or the static page does not declare the encoding. So by default, if Chinese is returned, the browser will use gbk to parse utf-8 encoding, which will obviously result in garbled characters. At this time, you need to actively add a header in the nginx location block to output the correct encoding. The added content is: Or you can change the add_header line The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: JS canvas realizes the functions of drawing board and signature board
>>: Mysql database master-slave separation example code
<META http-equiv="Page-Enter" CONTENT...
1. Install MySQL This article is installed via AP...
Introduction to Swap Swap (i.e. swap partition) i...
Ubuntu 15.04 opens MySQL remote port 3306. All th...
Error message: Job for mysqld.service failed beca...
Table of contents 1. Understanding 2. Use 1. h() ...
Detailed introduction to the steps of installing ...
1. First download from the official website of My...
Web front end 1 Student ID Name gender age 01 Zha...
Prepare war package 1. Prepare the existing Sprin...
Because the docker daemon needs to bind to the ho...
3 ways to implement tab switching in Vue 1. v-sho...
A simple record of the database startup problems ...
Preface: In MySQL, views are probably one of the ...
Let's first look at the MySQL official docume...