简体   繁体   English

为反向代理配置Laravel

[英]Configuring Laravel for Reverse Proxy

I have to upload a website that uses Laravel. 我必须上传一个使用Laravel的网站。 The server I must use are using a reverse proxy and when I put the files i developped on my computer, I'm getting a DNS error. 我必须使用的服务器正在使用反向代理,当我将开发的文件放在计算机上时,出现DNS错误。

I do not have access to the server configuration, I can only upload/download files on the website's server partition. 我无权访问服务器配置,只能上传/下载网站服务器分区上的文件。

I searched to find a solution but anything I can find was sort of related to this question . 我搜寻了一个解决方案,但我能找到的一切都与此问题有关

So, this is not Laravel version, but this can help you, I hope ! 所以,这不是Laravel版本,但是希望对您有所帮助!

Here is some code I wrote in cakePHP 2.X because I have some issues with a reverse proxy too 这是我在cakePHP 2.X中编写的一些代码,因为我对反向代理也有一些问题

Cache class is the one from cakePHP , it's very simple (60 sec expiration, automatically serialized data). Cache类是来自cakePHP的类 ,它非常简单(60秒到期,自动序列化数据)。

LogError function is a simple log function LogError函数是一个简单的日志函数

The code is the following 代码如下

// Constants
define('REVERSE_PROXY_ADDR' , 'r-proxy.internal-domain.com');
define('REVERSE_PROXY_CACHE', 'r-proxy');

// Cache Config
Cache::config(REVERSE_PROXY_CACHE, array(
    'engine'    => 'File',
    'prefix'    => REVERSE_PROXY_CACHE . '_',
    'path'      => CACHE . REVERSE_PROXY_CACHE . '/',
    'serialize' => true,
    'duration'  => 60,
));


function clientIp()
{
    // Originaly from CakePHP 2.X
    // ------------------------------

    if ( isset($_SERVER['HTTP_CLIENT_IP']) )
    {
        $ipaddr = $_SERVER['HTTP_CLIENT_IP'];
    }
    else
    {
        $ipaddr = $_SERVER['REMOTE_ADDR'];
    }

    if ( isset($_SERVER['HTTP_CLIENTADDRESS']) )
    {
        $tmpipaddr = $_SERVER['HTTP_CLIENTADDRESS'];

        if ( !empty( $tmpipaddr ) )
        {
            $ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr);
        }
    }

    $ip = trim($ipaddr);

    // ------------------------------

    // Reverse proxy stuff

    if ( defined('REVERSE_PROXY_ADDR') && defined('REVERSE_PROXY_CACHE') )
    {
        $xfor = preg_replace('/(?:,.*)/', '', $_SERVER['HTTP_X_FORWARDED_FOR']);

        $list = Cache::read('iplist', REVERSE_PROXY_CACHE);

        if ( $list === false )
        {
            $list = gethostbynamel(REVERSE_PROXY_ADDR);

            Cache::write('iplist', $list, REVERSE_PROXY_CACHE);
        }

        // empty or unreadable
        if ( empty( $list ) )
        {
            logError('Unable to gather reverse proxy ip list, or empty list');
            logError('Type : ' . gettype($list));
            logError('IP : ' . $ip . ' - X-FORWARDED-FOR : ' . $xfor);

            return $ip;
        }
        // not array ?!?!
        if ( !is_array($list) )
        {
            logError('Given list was not an array');
            logError('Type : ' . gettype($list));
            logError($list);

            return $ip;
        }

        // if in array, give forwarded for header
        if ( in_array($ip, $list, true) )
        {
            return $xfor;
        }
    }

    return $ip;
}

Then you just have to call the clientIp(); 然后,您只需要调用clientIp(); function. 功能。

If you have a static IP Address for your reverse proxy, you can just set it manually in the code, but that's not a good practice. 如果您的反向代理具有静态IP地址,则可以在代码中手动进行设置,但这不是一个好习惯。 But you will not need to use cache, and it will simplify a lot the code. 但是您不需要使用缓存,它将简化很多代码。

If you use a dynamic reverse proxy, you will have to query it on its hostname like this (what I did in the posted code) : 如果使用动态反向代理,则必须像这样在其主机名上查询它(我在发布的代码中做了什么):

gethostbynamel('reverse-proxy-addr') to get a list of possible rproxy IPs gethostbynamel('reverse-proxy-addr')获取可能的rproxy IP列表

OR 要么

gethostbyname('reverse-proxy-addr') to get one IP for rproxy gethostbyname('reverse-proxy-addr')为rproxy获取一个IP

In other case you just have to check that REMOTE_ADDR is in a list of IPs marked as Reverse-proxy IPs 在其他情况下,您只需要检查REMOTE_ADDR是否在标记为反向代理IP的IP列表中即可

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM