2015-01-20 130 views
0

我試圖設置一個簡單的nginx服務器作爲我的前端UI和我的後端API之間的代理。該設置非常簡單。用戶界面向/ api/endpoint發送所有api請求,代理服務器將請求傳遞給api。代理還需要重寫請求,以便不去http://api.location.net/api/endpoint,而是去http://api.location.net/endpoint。用戶界面位於http://api.location.net。這部分工作不正常(我得到500錯誤),我很確定它與我如何編寫我的重寫規則有關。這是我的nginx配置。nginx重寫不起作用

daemon off; 
error_log off; 

worker_processes 2; 
worker_rlimit_nofile 100000; 
events { 
    worker_connections 50000; 
    accept_mutex off; 
} 

http { 
    include /etc/nginx/mime.types; 
    access_log off; 
    sendfile on; 

    server { 
     listen 80 default_server; 
     server_name localhost _; 

     location/{ 
      alias /srv/site/; 
     } 

     location /api/ { 
      rewrite ^/api ""; # I think this is the problem 
      proxy_pass http://api.location.net; 
      proxy_pass_request_headers on; 
      proxy_pass_header X-ResponseData; 
      proxy_redirect off; 
     } 
    } 
} 

任何幫助將不勝感激,nginx的是對我來說還是相當新的和nginx的重寫文檔似乎並不有我需要什麼。

回答

2

如果我理解你的權利,這應該有助於

location /api/ { 
      proxy_pass http://api.location.net/; 
      proxy_pass_request_headers on; 
      proxy_pass_header X-ResponseData; 
      proxy_redirect off; 
     } 

注意在proxy_pass指令中的URI部分

如果使用URI指定了proxy_pass指令,那麼當一個 請求傳遞到服務器,匹配位置的標準化請求URI 的部分被指令中指定的URI替換:

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

+0

謝謝!這與其他一些更具特定項目的調整相結合,修復了它。 – taylorc93 2015-01-22 16:28:35