Nginx直接處理接口請求,返回相應內容(帶html標簽)


停服業務需要,服務器更新升級時,有時候需要維護一段時間,這段時間內,為了保證用戶體驗,通常都會在app端展示維護信息,和維護時間等等,

下面是一種app停服方案:

Nginx中編碼,提供一個接口,app客戶端每次登錄前都會先請求這個接口,這個接口如果返回服務器已經停服,那么就根據返回值中的維護信息進行展示,

接口如下圖:

server {
        server_name  192.168.19.218;
        listen 80;
		
		# 導入停服配置
		include maintenance/serviceInfo.conf;
		
		# 服務狀態接口
		location /service_info {
			default_type "application/json;charset=UTF-8";
			if ($service_status = "NORMAL") {
				return 200 '{"service_status":"NORMAL","maintenance_begin_time":"","maintenance_end_time":"","maintenance_intro":""}'; 
			}
			if ($service_status = "WAIT_STOP") {
				return 200 '{"service_status":"WAIT_STOP","maintenance_begin_time":"$maintenance_begin_time","maintenance_end_time":"$maintenance_end_time","maintenance_intro":"$maintenance_intro"}'; 
			}
			if ($service_status = "STOP") {
				return 200 '{"service_status":"STOP","maintenance_begin_time":"$maintenance_begin_time","maintenance_end_time":"$maintenance_end_time","maintenance_intro":"$maintenance_intro"}'; 
			}
		}
		
		# 訪問后端接口
        location /
        {
			# 若停服,則直接以503的錯誤碼返回
			if ($service_status = "STOP") {
				return 503 '{"code": "503", "message": "停服更新中!"}';
			}
			add_header Access-Control-Allow-Origin "*";
			add_header Access-Control-Allow-Credentials "true";
			add_header Cache-Control no-cache,private;
			add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
			add_header Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept,authorization,bysyskey;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://apiserver;
            #proxy_http_version 1.1;
            #proxy_set_header Connection "";
        }
    }

  

 

maintenance/serviceInfo.conf 配置停服信息

# 服務狀態,NORMAL:正常,WAIT_STOP:待停服,STOP:停服
set $service_status NORMAL;
# 維護開始時間
set $maintenance_begin_time 1627530630000;
# 維護結束時間
set $maintenance_end_time 1627530630010;
# 維護內容:
set $maintenance_intro '<h3><span class=\\"ql-size-large\\">停服維護公告</span></h3><p>1、修復BUG</p><p>2、<span style=\\"color: rgb(230, 0, 0);\\">增加關卡</span></p><p><br></p>';

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM