2017-01-30 103 views
1

proxy_pass工作,如果我保持default http://127.0.0.1:9200取消註釋, 但與下面的代碼地圖~^/kibana-devproxy_pass不工作,我得到500如果我去http://my.domain.com/kibana-devnginx的無效網址前綴地圖

user www-data; 
worker_processes auto; 
pid /run/nginx.pid; 

events { 
     worker_connections 768; 
     # multi_accept on; 
} 


http { 

    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 
    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
         '$status $body_bytes_sent "$http_referer" ' 
         '"$http_user_agent" "$http_x_forwarded_for"'; 
    access_log /var/log/nginx/access.log main; 
    sendfile  on; 
    keepalive_timeout 65; 

    map $request_uri $target { 
     #default http://127.0.0.1:9200; 
     ~^/kibana-dev http://127.0.0.1:9200; 
     ~^/kibana-test http://127.0.0.1:9100; 
    } 

    server { 

     listen 80 default_server; 
     listen [::]:80 default_server ipv6only=on; 

     if ($request_uri ~* ^/kibana-test) { 
     return 301 http://my.domain.com/_plugin/kibana; 
     } 

     if ($request_uri ~* ^/kibana-dev) { 
     return 301 http://my.domain.com/_plugin/kibana; 
     } 

     location/{ 
     proxy_set_header X-Real-IP 1.2.3.4; 
     proxy_http_version 1.1; 
     proxy_set_header Connect "Keep-Alive"; 
     proxy_set_header Proxy-Connection "Keep-Alive"; 
     proxy_set_header Authorization ""; 
     proxy_pass $target; 
     } 

    } 
}    

我也試過:

map $uri $target { 
     /kibana-dev http://127.0.0.1:9200; 
    } 

任何想法爲什麼? 感謝

+0

你'if'塊更改URI到'/ _plugin/kibana'前'map'提供諮詢。所以只有'default'的情況會被採用。 –

+0

謝謝,我認爲服務器配置來http /地圖 – Berlin

+0

@RichardSmith所以它就像這樣 - >如果塊,地圖塊,位置? – Berlin

回答

1

您的配置文件是沒有意義的。

如果呈現URI http://my.domain.com/kibana-dev,所述server塊將執行:

return 301 http://my.domain.com/_plugin/kibana; 

這將導致要表示爲/_plugin/kibana的URI。新的URI由location /塊處理,並執行:

proxy_pass $target; 

在這一點上,map被諮詢以確定$target的值,它是default值作爲/_plugin/kibana不符合任何其他值。

+0

它的工作原理是因爲機器內部的代理應用程序,如果有'/ kibana-dev',它會將值設置爲'$ target',稍後我會在代理傳遞中使用它。 – Berlin

+0

謝謝,你知道是否有辦法在服務器塊之前運行地圖嗎? – Berlin

+0

服務器是無狀態的,所以一旦你「返回」,交易就完成了。 'nginx'不知道原始URI是什麼。爲什麼你將URI重寫爲'/ _plugin/kibana'?瀏覽器是否需要知道,還是隻需要更改上游服務的URI? –