应用服务器Nginx之正向代理功能详解

一、Nginx背景介绍

      Nginx是一款面向性能设计的HTTP服务器,相较于Apachelighttpd具有占有内存少,稳定性高等优势。与旧版本(<=2.2)的Apache不同,Nginx不采用每客户机一线程的设计模型,而是充分使用异步逻辑从而削减了上下文调度开销,所以并发服务能力更强。

      Nginx 的编写有一个明确目标就是超越 Apache Web 服务器的性能。[19] Nginx 提供开箱即用的静态文件,使用的内存比 Apache 少得多,每秒可以处理大约四倍于 Apache 的请求。[20] 低并发下性能与 Apache 相当,有时候还低于,但是在高并发下 Nginx 能保持低资源低消耗高性能。还有高度模块化的设计,模块编写简单。配置文件简洁。

      整体采用模块化设计是Nginx的一个重大特点,甚至http服务器核心功能也是一个模块。旧版本的Nginx的模块是静态的,添加和删除模块都要对Nginx进行重新编译,1.9.11以及更新的版本已经支持动态模块加载。     

      根据Netcraft在2016年11月网络服务器调查,[13] Nginx被发现是所有“活跃”站点(被调查站点的18.22%)和百万最繁忙站点(被调查站点的27.83%)中使用次数最多的Web服务器。Nginx在官方测试的结果中,能够支持五万个并行连接,而在实际的运作中,可以支持二万至四万个并行连接。

二、与PHP集成关系

      Nginx和PHP-FPM的组合,是一种稳定、高效的PHP运行方式,效率要比传统的Apache和mod_php高出不少。PHP-FPM加入到了PHP核心,编译时加上--enable-fpm即可提供支持。 PHP-FPM以守护进程在后台运行,Nginx响应请求后,自行处理静态请求,PHP请求则经过fastcgi_pass交由PHP-FPM处理,处理完毕后返回。

三、手工编译安装最新nginx

1、安装软件依赖包

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

2、安装开发工具包

yum -y groupinstall "Development Tools"

3、下载nginx

wget https://nginx.org/download/nginx-1.15.6.tar.gz

4、解压编译安装nginx

yum -y install openssl openssl-devel

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log

 

make && make install

5、创建软连接

ln -s /usr/local/nginx/nginx /usr/sbin/nginx

6、验证安装结果

nginx -v

四、配置nginx服务器为正向代理服务功能

1、正向代理服务功能说明

ç¸å³å¾ç

个人计算机访问代理服务器后由代理服务器获取客户端http请求后代为向互联网服务器发出http请求,待互联网服务器返回结果后代理服务器上存一份并将结果回传给个人计算机。

ç¸å³å¾ç

上图的五个步骤显示正向代理的标准流程。

2、标准版nginx不支持对https的正向代理 

NGINX不支持HTTPS转发代理的原因是因为它不支持CONNECT方法。但是,如果您有兴趣将其用作HTTPS转发代理,则可以使用ngx_http_proxy_connect_module

因为 nginx 不支持 CONNECT,收到 “CONNECT /:443 HTTP/1.1” 后会报一个包含“client sent invalid request while reading client request line,” 的错误。

3、安装编译外挂包使得nginx支持https的正向代理

git clone https://github.com/chobits/ngx_http_proxy_connect_module.git

patch -p1 < /home/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1015.patch 

打补丁新增1.15.x对于版本补丁

 进入nginx1.15.6安装根目录下重新编译安装nginx

cd /home/nginx-1.15.6

yum -y install openssl openssl-devel

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --add-module=/home/ngx_http_proxy_connect_module --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 --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_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --without-http_gzip_module --with-mail --with-stream_realip_module

make && make install

4、nginx配置文件 

[root@localhost ~]# cat /etc/nginx/nginx.conf


events {
    # configuration of connection processing
}

http {

server {
     listen                         3128;

     # dns resolver used by forward proxying
     resolver                       8.8.8.8;

     # forward proxy for CONNECT request
     proxy_connect;
     proxy_connect_allow            443 563;
     proxy_connect_connect_timeout  10s;
     proxy_connect_read_timeout     10s;
     proxy_connect_send_timeout     10s;

     # forward proxy for non-CONNECT request
     location / {
         proxy_pass http://$host;
         proxy_set_header Host $host;
     }
 }

}

运行配置后的网络端口监视情况 

5、客户端浏览器配置

在linux下验证代理是否配置成功

curl --proxy ip:port url

6、nginx配置文件标准规范说明

user nobody; # a directive in the 'main' context

events {
    # configuration of connection processing
}

http {
    # Configuration specific to HTTP and affecting all virtual servers  

    server {
        # configuration of HTTP virtual server 1       
        location /one {
            # configuration for processing URIs starting with '/one'
        }
        location /two {
            # configuration for processing URIs starting with '/two'
        }
    } 
    
    server {
        # configuration of HTTP virtual server 2
    }
}

stream {
    # Configuration specific to TCP/UDP and affecting all virtual servers
    server {
        # configuration of TCP virtual server 1 
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值