2017-06-27 18 views
0

我正在爲逆向代理服務器寫一個Nginx conf文件。NGINX有條件的secure_link

我的位置之一受安全鏈接模塊(http_secure_link_module)的保護。

但我需要的是一個有條件的secure_link指令。

類似的東西:如果URI包含正確的標記,我將添加一個Set-Cookie標頭,如status = is_authorized,然後在用戶沒有設置狀態cookie時再次保護鏈接。

location/{ 

    if ($cookie_STATUS = "IS_AUTHORIZED") { 
     // if possible cancel secure_link for authorized (with cookie) users. 
    } 

secure_link $arg_token; 
    secure_link_md5 "MD5_SECRET_PARAMETERS"; 

    set $token $arg_token; 

    if ($secure_link = "") { 
     return 403; 
    } 

    if ($secure_link = "0") { 
     return 410; 
    } 

# Set Cokie 
    add_header Set-Cookie STATUS=IS_AUTHORIZED; 
    add_header Set-Cookie TOKEN=$arg_token; 

# Rewrite rules 
    include /usr/local/nginx/conf/rewrite_rules.conf;   

    # Set Cache 
    proxy_cache confluence_cache; 
    include /usr/local/nginx/conf/cache.conf; 
    # include /etc/nginx/shared/google_analytics.conf; 

    # Confluence Authentication 
    proxy_set_header Authorization "Basic BASE_64_HASH"; 
    proxy_pass PROXY_URL; 
} 

任何人都可以幫助我實現這個目標嗎?基本上我只想爲沒有將STATUS cookie設置爲IS_AUTHORIZED的用戶保護鏈接。或僅在第一頁訪問時使用secure_link。

謝謝你們。

回答

0

我已經設法用我的配置文件中的另一個變量做到這一點。

# if token is invalid nginx set $secure_link as empty string 
    if ($secure_link = "") { 
     set $is_allowed 'forbidden'; 
    } 

    # if expiration time is gone nginx set $secure_link as 0 
    if ($secure_link = "0") { 
     set $is_allowed 'gone'; 
    } 

    # if status cookie is set $is_allowed get 'authorized' as value 
    if ($cookie_STATUS = "IS_AUTHORIZED") { 
     set $is_allowed 'authorized'; 
    } 

    # return 403 if $is_allowed = forbidden 
    if ($is_allowed = 'forbidden') { 
     return 403; 
    } 

    # return 410 if $is_allowed = gone 
    if ($is_allowed = 'gone') { 
     return 410; 
    } 

然後它繼續正常設置代理和其他東西。