2015-01-20 132 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