2016-11-17 57 views
1

是否可以過濾來自locationOPTIONS調用並將它們重定向到其他位置?嘗試此配置,但沒有工作:基於http方法的Nginx代理服務器通行證

location /example/ { 
    proxy_pass http://example.com; 
    proxy_redirect off; 
    proxy_http_version 1.1; 
    proxy_set_header Connection ""; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_pass_request_headers on; 

    limit_except OPTIONS { 
     proxy_pass http://anotherurl.com; 
    } 
    } 

回答

1

有幾種方法可以實現。最簡單的一個,在我看來,與map指令:

upstream some_backend { 
    server example.com; 
} 

upstream another_backend { 
    server anotherurl.com; 
} 

map $request_method $upstream { 
    default some_backend; 
    OPTIONS another_backend; 
} 

server { 
    ... 
    location /example/ { 
     ... 
     proxy_pass http://$upstream; 
     ... 
    } 
    ... 
} 

使用upstreams不是強制性的,但建議。在大多數情況下,它會使您的配置更易於閱讀,維護和擴展。儘管如此,如果您希望省略upstream塊並直接在map塊中寫入主機,配置仍然可以使用:

map $request_method $upstream { 
    default example.com; 
    OPTIONS anotherurl.com; 
} 
相關問題