1,环境交代:
互联网服务器(服务器ip代号:111.111.111.111),通互联网
内网服务器(服务器ip代号:222.222.222.222),不通互联网
2,互联网服务器(111.111.111.111)nginx安装 (nginx默认支持http代理,但不支持https代理,所以需要编译装一个插件,且只能编译安装这个插件,看到编译就头疼)
安装前准备:
先装编译时需要的各种软件,执行 yum -y install gcc gcc-c++ autoconf automake make openssl openssl-devel
1>, 前往https://github.com/chobits/ngx_http_proxy_connect_module下载 ngx_http_proxy_connect_module-master.zip
2>,上传ngx_http_proxy_connect_module-master.zip到/opt/nginx (目录随意)
3>,解压 ngx_http_proxy_connect_module-master.zip
4>,停止当前安装的nginx:pkill -9 nginx (如果该机器未安装过nginx,此步骤忽略)
5>, 卸载当前已安装的nginx:yum remove nginx -y (如果该机器未安装过nginx,此步骤忽略)
6>,获取1.19.1版本的nginx:wget http://nginx.org/download/nginx-1.19.1.tar.gz(nginx版本太高了不支持ngx_http_proxy_connect_module-master ,至少21年7月15日此时是不支持的 )
7>,解压安装包:tar zxf nginx-1.19.1.tar.gz
8>,进入nginx-1.19.1目录:cd nginx-1.19.1/
9>,执行下面代码
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-dynamic-module=/opt/nginx/ngx_http_proxy_connect_module-master
10>,patch一下,执行:
patch -d /opt/nginx/nginx-1.19.1 -p 1 < /opt/nginx/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_1018.patch
11>,make && make install
12>,在 /etc/nginx/conf.d(nginx配置默认目录,具体情况看你服务器怎么配的) ,添加一个proxy.conf(名称随意,但要是 .conf 结尾),加入以下内容
server {
resolver 8.8.8.8;
listen 3128; #正向代理监听的端口 根据实际情况填写你自己的 就这个地方要改 其他不动
proxy_connect;
proxy_connect_allow all;
#proxy_connect_allow 443 563 5000;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
access_log /var/log/nginx/http_proxy.access.log main;
error_log /var/log/nginx/http_proxy.error.log;
# location / {
# proxy_pass $scheme://$http_host$request_uri;
#}
location / {
# 配置正向代理参数
proxy_pass $scheme://$host$request_uri;
#`proxy_pass $scheme://$host$request_uri;
# 解决如果URL中带"."后Nginx 503错误
proxy_set_header Host $http_host;
# 配置缓存大小
proxy_buffers 256 4k;
# 关闭磁盘缓存读写减少I/O
proxy_max_temp_file_size 0;
# 代理连接超时时间
proxy_connect_timeout 30;
# 配置代理服务器HTTP状态缓存时间
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
}
}
12>,启动nginx:service nginx start
到此 互联网这台服务器配置好了
我们再来看内网这台服务器(222.222.222.222),这台nignx正常yum装就好(有人要骂,都不通互联,yum毛线,碰到yum安装不了的 ,可以在通互联网的随便哪台机器下载你需要的安装软件 命令:yum install --downloadonly --downloaddir=/tmp/openssl openssl /tmp/openssl是目录,openssl 是你要装的软件,它默认会把所需要的依赖都下载下来 ),不用装上面那个扩展。
添加环境变量
1>,vim /etc/profile
2>,在末尾添加:
http_proxy=http://111.111.111.111:3128
https_proxy=https://111.111.111.111:3128
export http_proxy
export https_proxy
保存退出后执行 source /etc/profile
3>,划重点,我就是卡这里卡半天。上述步骤完了后,命令行里wget 各种网页都正常访问,但浏览器 http://***/test.php (test.php是222.222.222.222中的php文件,里面写了段测试代码,curl访问百度),就是不行
命令行里执行 php -f test.php 却可以访问到百度,蛋疼
咨询了下大佬 ,才发现php没有识别到环境变量,需要修改下php-fpm配置
骚操作来了:
第一步:
打开php.ini
找打 variables_order 这个配置
将默认配置 variables_order = "GPCS" 改为 variables_order = "EGPCS"
第二步:
vim /etc/php-fpm.d/www.conf
找到 clear_env = no将前面的;去掉
并添加下面2行 (IP就是互联网那台的ip,互访的策略要提前开通啊)
env[HTTP_PROXY] = 111.111.111.111:3128
env[HTTPS_PROXY] = 111.111.111.111:3128
重启php-fpm
systemctl restart php-fpm
至此,大功告成!