前言
Nginx做正向代理,实现内网PC可以通过互联网服务器的代理访问上互联网。
一、安装Nginx
官网地址:https://nginx.org/en/download.html
下载 Linux 1.20.2版本
将nginx-1.20.2.tar.gz 文件上传到服务器 路径 /home/test/va/ ,进行解压
#解压
tar -zxvf nginx-1.20.2.tar.gz
首先需要按照依赖,否则 编译时会出现报错
*make: *** 没有规则可以创建“default”需要的目标“build”。 停止。
#安装依赖
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
进入/home/test/va/nginx-1.20.2目录下,编译安装Nginx,编译后默认安装目录为/usr/local/nginx
./configure
make && make install
./configure 检查要通过,否则在编译时也会提示:*make: *** 没有规则可以创建“default”需要的目标“build”。 停止。
nginx.conf配置文件在/usr/local/nginx/conf 目录下,默认开启的80端口
进入目录到/usr/local/nginx/sbin下面,启动nginx服务
cd /usr/local/nginx/sbin
#启动nginx服务
./nginx
#开启80端口,以便外部访问
firewall-cmd --zone=public --add-port=80/tcp --permanent
#重启防火墙
firewall-cmd --reload
#查看nginx服务是否启动成功,访问80端口查看是否成功
ps -ef | grep nginx
#执行nginx stop命令,可停止
nginx -s stop
#重新加载Nginx配置文件,然后以优雅的方式重启Nginx
nginx -s reload
#从容停止服务,需要进程完成当前工作后再停止。
nginx -s quit
执行nginx -v 若提示提示-bash: nginx: 未找到命令,则需要配置环境变量
#打开环境变量所在的文件
vim /etc/profil
#在profile文件末尾,加上一行 ,指向nginx的安装位置的sbin 目录
export PATH=$PATH:/usr/local/nginx/sbin
#重新加载环境即可
source /etc/profile
二、正向代理(http)
Nginx官方并不支持直接转发https请求,Nginx Windows版本只支持http。
若代理PC访问Https协议会无法访问,查看代理服务器的error.log,发现其报400错误码。
#在/usr/local/nginx/conf 目录下 修改nginx.conf配置文件添加以下server即可
server {
listen 8889;
resolver 223.5.5.5; # dns解析服务器
location /
{
proxy_pass $scheme://$host$request_uri;
#proxy_pass 用来要代理的网站,
#$scheme 客户端请求的协议(如http);
#$host 客户端请求的域名(如baidu.com);
#$request_uri是客户端访问的url地址(如/baidu?s=12345)。
#拼接成就是http://baidu.com/baidu?s=12345
}
}
三、正向代理(https)
可以使用第三方模块ngx_http_proxy_connect_module 实现直接转发https请求,只支持Linux。
下载地址:https://github.com/chobits/ngx_http_proxy_connect_module
GitHub上下载ngx_http_proxy_connect_module的zip源码
nginx版本与代理模块对照表
将下载的ngx_http_proxy_connect_module-master.zip 解压到 文件名ngx_http_proxy_connect_module文件夹。
将文件夹的源码文件上传到服务器Nginx源码目录 /home/test/va/nginx-1.20.2
#1.使用root用户进入nginx的资源目录/home/test/va/nginx-1.20.2,添加新模块ngx_http_proxy_connect_module和并重新编译ngin
cd /home/test/va/nginx-1.20.2
#2.添patch 导入模块添加新模块到nginx , 后面为模块路径
patch -p1 < /home/test/va/nginx-1.20.2/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
#3.查看现有版本nginx 的 configure 配置信息
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --add-module=/home/test/va/nginx-1.20.2/ngx_http_proxy_connect_module
[root@localhost ~]#
#关注的是下面的配置参数configure arguments,如果这个参数后面没有内容,就可以直接执行对应的./configure文件,如果后面跟了内容,那么在配置的时候一定要加上对应的参数
##配置configure --prefix 代表安装的路径,--with-http_ssl_module 安装ssl,--with-http_stub_status_module查看nginx的客户端状态
./configure --add-module=/home/test/va/nginx-1.20.2/ngx_http_proxy_connect_module
#或者
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2 22 Jan 2015
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/nginx/pcre-8.35 --with-openssl=/usr/local/src/nginx/openssl-1.0.2
[root@localhost ~]#
#关注的是下面的配置参数configure arguments,如果这个参数后面没有内容,就可以直接执行对应的./configure文件,如果后面跟了内容,那么在配置的时候一定要加上对应的参数
./configure --add-module=/home/test/va/nginx-1.20.2/ngx_http_proxy_connect_module --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/nginx/pcre-8.35 --with-openssl=/usr/local/src/nginx/openssl-1.0.2
#4.安装nginx, 安装位置根据编译时配置的–prefix= ,若未配置 默认为/usr/local/nginx
make && make install
#在/usr/local/nginx/conf 目录下 修改nginx.conf配置文件添加以下server即可
#该配置包含了 http和https 都可以直接进行代理
server {
#指定DNS服务器IP地址
resolver 223.5.5.5;
#监听888端口,https默认端口443
listen 8888;
#正向代理转发https请求 开放所有端口 或开放80 443
proxy_connect;
proxy_connect_allow all;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
location / {
proxy_pass http://$host;
proxy_set_header Host $host;
}
}
验证是代理服务器是否能正常代理转发https请求。
curl https://www.baidu.com/ -v -x 127.0.0.1:8888