Apache/nginx设置反向代理

今天来说下apache和nginx设置反代理功能:

首先要开启代理设置

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_connect_module modules/mod_proxy_connect.so

LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

LoadModule proxy_http_module modules/mod_proxy_http.so

然后就是设置网站配置

<VirtualHost *:80>

    ServerName web.com

    DocumentRoot /Users/momo/www/web/

    SetEnv APPLICATION_ENV "development"



    #反代理设置

     ProxyPass /api http://webapi.com/

     ProxyPassReverse /api/ http://webapi.com/



    <Directory /Users/momo/www/web/>

        DirectoryIndex index.html index.htm index.php

        AllowOverride All

        Order allow,deny

        Allow from all

    </Directory>

</VirtualHost>



<VirtualHost *:80>

    ServerName webapi.com

    DocumentRoot /Users/momo/www/webapi/public

    SetEnv APPLICATION_ENV "development"



    <Directory /Users/momo/www/webapi/public>

        DirectoryIndex index.php

        AllowOverride All

        Order allow,deny

        Allow from all

    </Directory>

</VirtualHost>

我们现在来看反代理设置这两句

ProxyPass /api http://webapi.com/ : 将 web.com/api 下的所有请求全都交给 webapi.com 代理,例如 web.com/api/info.php 会交给 webapi.com/info.php 代理进行实际处理;

ProxyPassReverse /api http://webapi.com/:

webapi.com/info.php 中有如下代码:

<?php
      header('Location: http://webapi.com/result.php');
?>

那么在重定向的时候,Apache会将HTTP请求重新设为http://web.com/api/result.php, 这样的作用稍后讲解

webapi.com/result.php 中有如下代码:

<?php
    phpinfo();die;
?>

那么访问结果就如下:

访问web.com/api/info.php

apache将请求交给 webapi.com/info.php 代理,HTTP请求如下:

Request URL:

http://web.com/api/info.php

Request Method:

GET

Status Code:

200 Ok

Remote Address:

127.0.0.1:80

Referrer Policy:

no-referrer-when-downgrade



Connection:

Keep-Alive

Content-Length:

0

Content-Type:

text/html; charset=UTF-8

Date:

Wed, 20 Dec 2017 02:12:33 GMT

Keep-Alive:

timeout=5, max=100

Location:

http://web.com/api/result.php

Server:

Apache/2.4.27 (Unix) PHP/7.1.4

X-Powered-By:

PHP/7.1.4

可以发现其实request中的请求还是 web.com 的,但是它确实是由 webapi.com 来处理的

webapi.com/info.php 重定向到 webapi.com/result.php显示PHP版本信息

Request URL:

http://web.com/api/result.php

Request Method:

GET

Status Code:

200 OK

Remote Address:

127.0.0.1:80

Referrer Policy:

no-referrer-when-downgrade

也可以看到请求依然是 web.com/api/result.php

这里就是 ProxyPassReverse 发挥作用的地方,如果不加这个项,重定向后HTTP请求会如下所示:

Request URL:
http://webapi.com/result.php

Request Method:

GET

Status Code:

200 OK

Remote Address:

127.0.0.1:80

Referrer Policy:

no-referrer-when-downgrade

请求中的GET是 webapi.com 而不是 web.com ,这是因为配置了ProxyPassReverse后,webapi.com/info.php 在重定向到 webapi.com/result.php 时,apache会将它调整回 web.com/api/result.php , 然后apache再将 web.com/api/result.php 代理给 webapi.com/result.php,所以说配置了 ProxyPassReverse 后,即使 web.com/api 下的程序有重定向到其他 webapi.com/的文件的(如 info.php 重定向到 result.php),加上了ProxyPassReverse在请求中就看不到实际的代理接口请求地址

nginx的配置,原理就不再赘述了

server {

    listen  80;

    server_name  web.com;

    root   /usr/share/nginx/html/web;

    index  index.html index.htm index.php;

    location /api/ {

        proxy_pass http://webapi.com/;

    }

}

猜你喜欢

转载自blog.csdn.net/momo_mutou/article/details/81835138
今日推荐