dou7466 2016-05-12 07:35 采纳率: 0%
浏览 739

为反向代理配置Laravel

我必须上传一个使用Laravel的网站。 必须使用的服务器正在使用反向代理,将我开发的文件放在计算机上时,出现了DNS错误。

我无权访问服务器配置,只能在网站的服务器分区上上传/下载文件。

我试图找到一个解决方案,但我能找到的一切都与此问题有关。

  • 写回答

1条回答 默认 最新

  • douya2982 2016-05-12 07:57
    关注

    So, this is not Laravel version, but this can help you, I hope !

    Here is some code I wrote in cakePHP 2.X because I have some issues with a reverse proxy too

    Cache class is the one from cakePHP, it's very simple (60 sec expiration, automatically serialized data).

    LogError function is a simple log function

    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(); 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. 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

    OR

    gethostbyname('reverse-proxy-addr') to get one IP for rproxy

    In other case you just have to check that REMOTE_ADDR is in a list of IPs marked as Reverse-proxy IPs

    Hope it helps !

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题