首页 > 解决方案 > 告诉 Laravel / Nginx 使用代理

问题描述

解决了我只需要在代码中声明使用代理,请参阅下面的评论@apokryfos 做对了

嗨,我使用 Nginx、mysql 和 php7.2 托管网站,网站使用 laravel 框架。服务器只能通过代理访问互联网,我已经为所有用户设置了代理配置,如下所示:

sudo nano /etc/environment

这是文件的外观:

administrator@orion:/var/www/truckstock$ sudo nano /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/>
http_proxy=http://10.254.234.70:3128/
https_proxy=https://10.254.234.70:3128/

对于使用 git 设置服务器等所有目的,composer...代理就像一个魅力。

当我正在访问网站的用户尝试查询外部 API(我们的网站需要从Monday.com获取一些数据)这是api2.monday.com时出现问题,连接不起作用并且在我的本地服务器上(我的笔记本电脑)一切正常。

我的问题是如何告诉 Nginx 或 Laravel 或 Php(不确定是谁提出此请求)使用 porxy?

提前致谢

编辑 1: 这是执行 api 查询的代码部分的外观

 $query = 'mutation {
change_column_value (board_id: 570045226, item_id: '.$id.', column_id: "status", value: "{\"index\": 11}") {
id
}
}';
    $headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];
    $data = @file_get_contents($tempUrl, false, stream_context_create([
        'http' => [
            'method' => 'POST',
            'header' => $headers,
            'content' => json_encode(['query' => $query]),
        ]
    ]));
    $tempContents = json_decode($data, true);

标签: phplinuxlaravelnginxproxy

解决方案


所以我所做的是我改变了这段代码:

 $query = 'mutation {
change_column_value (board_id: 570045226, item_id: '.$id.', column_id: "status", value: "{\"index\": 11}") {
id
}
}';
    $headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];
    $data = @file_get_contents($tempUrl, false, stream_context_create([
        'http' => [
            'method' => 'POST',
            'header' => $headers,
            'content' => json_encode(['query' => $query]),
        ]
    ]));
    $tempContents = json_decode($data, true);

我添加了代理信息,所以它是这样的:

$query = 'mutation {
    change_column_value (board_id: 570045226, item_id: '.$id.', column_id: "status", value: "{\"index\": 11}") {
    id
    }
    }';
        $headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];
        $data = @file_get_contents($tempUrl, false, stream_context_create([
            'http' => [
                'proxy' => 'my proxy', 
                 'request_fulluri' => true
                'method' => 'POST',
                'header' => $headers,
                'content' => json_encode(['query' => $query]),
            ]
        ]));
        $tempContents = json_decode($data, true);

推荐阅读