nginx內置變量如何在不同location之間傳遞

問題起因是要實現nginx認證通過後才允許訪問mp4視頻文件進行直播。
除了使用auth_request模塊功能外,還有一個關鍵點是不同location之間傳遞變量。
nginx爲了方便配置文件修改,實際上nginx的配置文件相當於一個腳本語言了。
nginx定義了許多變量,這裏簡單列出四個此次配置要使用的。
$arg_PARAMETER    #這個變量包含GET請求中,如果有變量PARAMETER時的值。ex: arg_tk
$args                    #請求中的參數值
$query_string            #同 $args
$is_args                 #如果請求中有參數,值爲"?",否則爲空字符串
在不同層級的標籤中聲明的變量性的可見性規則如下:
    location標籤中聲明的變量中對這個location塊可見
    server標籤中聲明的變量對server塊以及server塊中的所有子塊可見
    http標籤中聲明的變量對http塊以及http塊中的所有子塊可見
如何傳遞參數呢?location#1中定義:set $http_query $is_args$args;
在location#2中直接使用$http_query變量。
完整配置文件nginx.vod.conf如下:

upstream vod_files  {
    server 127.0.0.1:4001; 
}

server {
    listen 17019;
    server_name  vod.fili58.com;
 
    access_log  /var/log/nginx/vodauth.access.log  main;
    error_log  /var/log/nginx/vodauth.error.log;
    root   html;
    index  index.html index.htm index.php;

    location / {
        set $http_query $is_args$args;
        auth_request /auth-proxy;

        error_page 401 500 502 503 504  /50x.html;

        proxy_pass http://vod_files;
    }

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location = /auth-proxy {
        internal;
        proxy_pass http://fili58.com:17000/api/v1/sessions$http_query;
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章