如何使用nginx或php将请求转发到另一个地址

WBOY
Release: 2016-06-06 20:06:25
Original
2425 people have browsed it

我有两台web服务器,想实现这样一个功能:

  1. 用户请求服务器A(http://aaa);

  2. 服务器A 收到请求 取到用户请求的数据,请求服务器B(http://bbb);

  3. 服务器B 返回数据给服务器A;

  4. 服务器A 返回数据给用户。

请问服务器A需要做什么设置才能完成这种需求。

如果不行的话,能不能用php实现,有没有demo?

回复内容:

我有两台web服务器,想实现这样一个功能:

  1. 用户请求服务器A(http://aaa);

  2. 服务器A 收到请求 取到用户请求的数据,请求服务器B(http://bbb);

  3. 服务器B 返回数据给服务器A;

  4. 服务器A 返回数据给用户。

请问服务器A需要做什么设置才能完成这种需求。

如果不行的话,能不能用php实现,有没有demo?

nginx proxy_pass

https://www.baidu.com/s?wd=nginx+proxy_pass

<code>server {
    listen  80;
    server_name  aaa;

    location / {
        proxy_pass http://bbb;
    }
}</code>
Copy after login

举个PHP栗子。
aaa.com page1.php
比如发送一个get请求

<code>
<?php $data = file_get_contents('http://bbb.com/page2.php?id=2&key=kkk');
print_r($data);
?>
</code>
Copy after login

bbb.com page2.php

<code>
<?php $id = $_GET['id];
echo json_encode(array('status'=>0,'message'=>$id));
?>
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!