Apache代理传递给nginx

我在Apache中有以下configuration:

RewriteEngine On #APP ProxyPass /abc/ http://remote.com/abc/ ProxyPassReverse /abc/ http://remote.com/abc/ #APP2 ProxyPass /efg/ http://remote.com/efg/ ProxyPassReverse /efg/ http://remote.com/efg/ 

我想在nginx中有相同的configuration。 阅读一些链接后,这是我有:

 server { listen 8081; server_name localhost; proxy_redirect http://localhost:8081/ http://remote.com/; location ^~/abc/ { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://remote.com/abc/; } location ^~/efg/ { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://remote.com/efg/; } } 

我已经有了以下configuration:

 server { listen 8080; server_name localhost; location / { root html; index index.html index.htm; } location ^~/myAPP { alias path/to/app; index main.html; } location ^~/myAPP/images { alias another/path/to/images autoindex on; } } 

这里的想法是克服同源政策问题。 主页在localhost:8080,但我们需要ajax调用http://remote.com/abc 。 两个域都在我的控制之下。

使用上述configuration,ajax调用要么不到达远程服务器,要么因为交叉原点而被切断。

上面的解决scheme在Apache中工作,而不是在Nginx的工作,所以我认为这是一个configuration问题。

我认为这里有一个隐含的问题:我应该有两个server声明,还是应该以某种方式将它们合并为一个?

编辑:添加一些更多的信息

编辑2:我已经将所有proxy_passconfiguration移动到主server声明,并更改所有ajax调用通过端口8080.我现在得到一个新的错误: 502 Connection reset by peer 。 Wireshark显示数据包出去http://remote.com与一个错误的IP头校验和。

不工作不是特别详细,但让我试着猜测问题可能是什么。 我会build议尝试下面的configuration:

 server { listen 8081; server_name localhost; location /abc/ { proxy_pass http://remote.com/abc/; proxy_http_version 1.1; proxy_redirect http://localhost:8081/ http://remote.com/; proxy_set_header Host $host; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /efg/ { proxy_pass http://remote.com/efg/; proxy_http_version 1.1; proxy_redirect http://localhost:8081/ http://remote.com/; proxy_set_header Host $host; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

我真的改变了位置块的定义,并添加了一个主机头,以防远程主机使用基于名称的虚拟主机。 所有其他的变化都是表面化的 – 当所有相应的指令被收集在一起并且正确地sorting时(这可能按照重要性或者其他特征的顺序),可读性被改进。

如果上面的configuration不能按预期的那样工作,请添加一些信息,究竟哪些不起作用,以及如何失败。

编辑:我还添加了一个主机头到上面的configuration,以防远程使用基于名称的虚拟主机。