目录
1. 缘起
最近碰到了一个麻烦事情,就是公司的centos测试服务器放在内网环境,而且不能直接上外网,导致无法通过yum安装软件,非常捉急。
幸好,内网还是有可以可以访问外网的机器,所以就想到应该可以利用nginx搭建一个代理服务器,然后centos通过这个nginx来访问外网。当然,如果只是代理http还是很简单的,而要代理https还是需要稍费周折,因为nginx本身不能部署被代理的网站的证书,不能部署成https终结点来,因此与被代理客户端之间不能用ssl协议通讯,因此需要通过http协议中的CONNECT请求打通和外网的连接,然后客户端到nginx走明文,nginx到外网走https协议。这里需要用到ngx_http_proxy_connect_module模块来实现CONNECT的代理功能。
2. 部署nginx
-
步骤1: 从nginx官网下载nginx源码包。
-
步骤2: 因为nginx原生是不支持CONNECT请求的,需要安装一个扩展插件,即ngx_http_proxy_connect_module,从github下载ngx_http_proxy_connect_module,另外还要下载一个nginx内核补丁。
-
步骤3: 解压nginx源码包,进入nginx源码目录,创建modules目录(mkdir modules)。
-
步骤4: 将ngx_http_proxy_connect_module源码目录放到modules目录中。
-
步骤5: 将nginx内核补丁放到nginx源码目录,姑且名字叫p1.patch
-
步骤6: 在nginx源码目录,执行以下命令给nginx内核打上补丁:
patch -p 1 < p1.patch
-
步骤7:编译nginx,这里假设nginx安装到/opt/nginx目录中(在编译前确认pcre、zlib、openssl的库是否已经正常安装),编译命令如下:
./configure --prefix=/opt/nginx --with-http_ssl_module -add-module=./modules/ngx_http_proxy_connect_module
make & make install -
步骤8:配置nginx
配置文件如下:#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;
events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; keepalive_timeout 65;
server {
# 代理端口
listen 8080;
server_name localhost;# 解析被代理网站域名的dns