2016-09-16 87 views
0

我試圖通過nginx來平衡一個web應用程序,它對我的​​web應用程序調用具有子路徑的服務都會正常工作。使用上游時NGINX沒有響應

例如它的工作原理

http://example.com/luna/ 

而不是

http://example.com/luna/sales 

我的nginx.conf

user nobody; 
worker_processes auto; 

events { 
    worker_connections 1024; 
} 

http { 
    include  mime.types; 
    default_type application/octet-stream; 

    sendfile  on; 
    keepalive_timeout 65; 

    map $http_upgrade $connection_upgrade { 
     default upgrade; 
     '' close; 
    } 

    upstream lunaups { 
     server myhostserver1.com:8080; 
     server myhostserver2.com:8080; 
    } 


    server { 
     listen  80; 
     server_name example.com; 

     proxy_pass_header Server; 

     location =/{ 
      rewrite^http://example.com/luna redirect; 
     } 

     location /luna { 
      rewrite ^$/luna/(.*)/^ /$1 redirect; 
      proxy_pass http://lunaups; 
      #add_header X-Upstream $upstream_addr; 
     } 

     error_page 500 502 503 504 /50x.html; 
     location = /50x.html { 
      root html; 
     } 
    } 
} 

我的web應用程序調用與像/盧納/銷售更多的子路徑服務無法返回響應。我在這裏錯過了什麼?

它工作,如果我從上游移除我的主機服務器之一,但是當我在上游添加第二個主機時,它無法返回響應。

我的重寫規則是錯誤的還是我的配置整體錯誤?

+0

'^ $/luna /(.*)/ ^'應該做什麼?它似乎有一個僞造的'$'和一個虛假的'^'。 –

+0

嗨理查德,我是nginx新手,我自己構建了這個文件,只是在試驗和錯誤,請儘可能糾正我。 –

+0

在向上遊發送之前是否應該從URI中刪除'/ luna'前綴? –

回答

0

rewrite指令有四個後綴,它們都有特定用途。詳情請參閱this document

如果您希望將URI /映射到而不更改瀏覽器中的URL,則可以使用rewrite ... last進行內部重寫。例如:

location =/{ 
    rewrite^/luna last; 
} 

location /luna塊,你需要將它發送到proxy_pass聲明(無需離開位置塊),這需要一個rewrite ... break之前重寫URI。例如:

location /luna { 
    rewrite ^/luna(/.*)$ $1 break; 
    rewrite ^/break; 
    proxy_pass http://lunaups; 
} 

第一重寫改變任何URI與子路徑,並且所述第二重寫處理URI沒有子路徑。

有關正則表達式的信息,請參閱this useful resource

+0

Hi Richard,現在我可以在nginx訪問日誌中看到觸發到/ luna/sales的請求,但是我仍然看不到客戶端的響應,Nginx日誌中的Http狀態代碼似乎爲200 OK。 Chrome開發人員工具的「網絡」選項卡也顯示「無響應」。 –