【openresty】獲取post請求數據FormInputNginxModule模塊

  關於openresty的一些介紹看這裏html

  首先,實驗背景爲openresty做爲後臺來處理前臺post傳遞的數據。nginx

  在openresty內,有一個FormInputNginxModule模塊,做用是解析post請求中的參數。git

  若是要使用該模塊,須要在編譯openresty源碼時,在"./configure"一步添加參數:github

./configure --add-module=/somepath/form-input-nginx-module --add-module=/somepath/ngx_devel_kit

  在這裏能夠查看到FormInputNginxModule模塊的說明。json


  咱們須要獲取post請求中的數據,因此這裏用到FormInputNginxModule模塊的下面這個命令:post

set_form_input $variable argument;

  因而,咱們有post請求:lua

 1 var json = {
 2     data: "Hello!"
 3 };
 4 $.post(
 5     'save', 
 6     json, 
 7     function(callback){
 8         alert(callback);
 9     }
10 );    

  其中function用以輸出後臺返回的數據。spa

  接着,咱們有nginx配置文件nginx.conf:3d

 1 user root;
 2 worker_processes  2;
 3 
 4 error_log  logs/error.log;
 5 pid        logs/nginx.pid;
 6 
 7 events {
 8     worker_connections  1024;
 9     # multi_accept on;
10 }
11 
12 http {
13     include       mime.types;
14 
15     access_log    logs/access.log;
16     
17     server {
18        listen 80;
19        server_name  localhost;
20        
21        location / {
22            root /var/www/aceEditor;
23            index index.htm index.html;
24        }
25            
26        location /save {
27            set_form_input $data data;
28           echo $data;
29        }
30     }
31 }

  這樣,當前臺post數據時,就會觸發nginx的/save塊,因而經過FormInputNginxModule模塊的set_form_input指令將post的data數據set給了變量$data,而後咱們就能夠處理獲取的變量。在這個實驗中,使用echo向前臺返回數據。rest

  因而,前臺點擊post後,會彈出響應:

  

  到這裏,一個完整的post過程就完成了。

  隨後,在post提交的json中傳遞的參數,均可以用FormInputNginxModule模塊的set_form_input指令來獲取,這樣,後臺就取到了前天傳遞的數據,接着結合lua,繼續更多的操做。

相關文章
相關標籤/搜索