Rootop 服务器运维与web架构

配置php-fpm服务走http代理

配置了http代理,端口10809。
# shell中设置http代理,输出2个环境变量。

$  export http_proxy=http://127.0.0.1:10809
$  export https_proxy=http://127.0.0.1:10809

$ curl https://www.rootop.org/ip2.php
ip地址为:192.227.146.237<br>

# 执行php脚本,默认也会走代理。
$ php c.php
HTTP/1.1 200 Connection established

HTTP/2 200
date: Thu, 03 Aug 2023 05:30:31 GMT
content-type: text/html; charset=UTF-8
strict-transport-security: max-age=604800; includeSubdomains; preload
x-frame-options: SAMEORIGIN

ip地址为:192.227.146.237<br>

默认情况下系统设置http_proxy和https_proxy环境变量后,shell里的命令操作基本都会走代理
比如 wget 、curl、php、git等

但是像php-fpm服务,就不会读取变量作为代理主动往外请求了。
也就是说通过浏览器访问php就不会走代理。
需要配置www.conf加入:

env[HTTP_PROXY] = http://127.0.0.1:10809
env[HTTPS_PROXY] = http://127.0.0.1:10809

重启php-fpm后才可以通过代理访问。
或者是在php里的curl中配置代理

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_PROXY, "http://127.0.0.1:10809");
curl_setopt($curl, CURLOPT_URL, 'https://www.rootop.org/ip2.php');
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($curl);
curl_close($curl);
print_r($data);

这种代理场景适合不让上外网的服务器,但是还要请求三方接口的情况。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/5289.html

作者:Venus

服务器运维与性能优化

评论已关闭。