Nginx 反向代理工作原理简介与配置详解

Nginx 反向代理工作原理简介与配置详解

测试环境
CentOS 6.8-x86_64
nginx-1.10.0
下载地址:http://nginx.org/en/download.html
安装 nginx
[root@localhost mnt]# tar -xzvf nginx-1.10.0.tar.gz
[root@localhost mnt]# cd nginx-1.10.0
[root@localhost nginx-1.10.0]# ./configure --prefix=/usr/local/ngnix --withpcre=/mnt/pcre-8.36
Configuration summary
+ using PCRE library: /mnt/pcre-8.36
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using system zlib library
nginx path prefix: "/usr/local/ngnix"
nginx binary file: "/usr/local/ngnix/sbin/nginx"
nginx modules path: "/usr/local/ngnix/modules"
nginx configuration prefix: "/usr/local/ngnix/conf"
nginx configuration file: "/usr/local/ngnix/conf/nginx.conf"
nginx pid file: "/usr/local/ngnix/logs/nginx.pid"
nginx error log file: "/usr/local/ngnix/logs/error.log"
nginx http access log file: "/usr/local/ngnix/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
注:
1、编译时,指定了 pcre 安装目录,但是安装出错,解决方法如上,指定源码所在目录
2、如果不指定--with-pcre 选项,会报类似如下的错误
3、需要预先安装 gcc-c++
[root@localhost nginx-1.10.0]# make && make install

启动 ngnix
[root@localhostnginx-1.10.0]#
/usr/local/ngnix/sbin/nginx –c /usr/local/ngnix/conf/nginx.conf
反向代理工作原理
客户端向反向代理发送请求,接着反向代理转发请求至目标服务器,并把获得的内容返回给
客户端
反向代理配置
测试链接:
http://192.168.1.104/zentaopms/www/index.php?
http://192.168.1.104/zentaopms/www/index.php?m=project&f=create
如上,想通过 192.168.1.103 代理服务器访问上述测试链接,具体咋操作呢?如下
编辑所使用的配置文件
[root@localhost nginx-1.10.0]# vim /usr/local/ngnix/conf/nginx.conf
找到“Server”结点,增加入下带背景色内容
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#


error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /zentaopms/www/ {
proxy_pass http://192.168.1.104;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
重新加载配置文件
[root@localhost nginx-1.10.0]# /usr/local/ngnix/sbin/nginx -s reload
访问测试 url
如下,OK
说明:
传递请求给被代理服务器
为了把请求传递给被代理服务器,需要在 location 中指定 proxy_pass 机制。如下
location /some/path/ {
proxy_pass http://www.example.com/link/;
}
proxy_pass 既可以是 ip 地址,也可以是域名,同时还可以指定端口
location ~ \.php {
proxy_pass http://127.0.0.1:8000;
}
注意:如果 proxy_pass 指定的地址携带了 URI,如上例中 /link/,那么这里的 URI 将替换
请求 URI 中匹配 location 参数的部分,比如 请求 /some/path/page.html,将被替换为
http://www.example.com/link/page.html。
另外,如果请求不是发往 http 类型的被代理服务器,则选择如下之一:
fastcgi_pass 传递请求给 FastCGI 服务器
uwsgi_pass 传递请求给 uwsgi 服务器
scgi_pass 传递请求给 SCGI 服务器
memcached_pass 传递请求给 memcached 服务器
请求也可以发往一命名的组服务器,这种请求下,将根据指定方法,在这些服务器之中进行
请求的分发。
传递请求头
默认的,nginx 在被代理请求中定义两个头域:Host 和 Connection,并且清除包含空值的
头域。 Host 被设置为$proxy_host 变量,而 Connection 则被设置为 close。
使用 proxy_set_header 机制可修改默认配置及其它头域的值。可以在 location 中,server
上下文,http 块或者其它更高层级中指定这种机制。
例子:
location /some/path/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8000;
}
如果想阻止某个头域被传递给被代理服务器,可以如下设置头域的值为空
location /some/path/ {
proxy_set_header Accept-Encoding "";
proxy_pass http://localhost:8000;
}
配置缓冲(Buffer)
默认的,Ngnix buffering 来自被代理服务器的响应。Ngnix 在内部 buffering 中存储响应,
直到收到整个响应后才发送给客户端。对于慢客户端来说,buffering 可优化性能,这样,
如果响应从 Nginx 同步传递给客户端,这将会浪费被代理服务器的时间。但是,如果开启
buffering,Nginx 允许被代理服务器快速处理请求,因为 Nginx 会尽可能久的存储来自被
代理服务器的响应,直到客户端下载它们。
使用 proxy_buffering 机制开启或关闭缓冲。默认的,开启缓冲。
proxy_buffers 控制 buffer 大小和分配给请求的 buffer 数量。来自被代理服务器响应中的
第一部分被存储在单一的 buffer 中,该 buffer 的大小由 proxy_buffer_size 设定。该部分
通常包含一个相对较小的响应头,其大小可以设置成比用于存储剩余响应部分 buffer 小。
例子:
location /some/path/ {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_pass http://localhost:8000;
}
如果关闭 buffering,当从被代理服务器接收到响应时,将被同步把响应发往客户端。这对
于想尽快收到请求的快速交互客户端来说。这是其想要的。
例子:关闭 buffering
location /some/path/ {
proxy_buffering off;
proxy_pass http://localhost:8000;
}
这种情况下,nginx 只用 proxy_buffer_size 来存储响应的当前部分。
选择一个出口 IP 地址
如果代理服务器有多个网络接口,有时候需要选择特定的源 ip 地址来连接到代理服务器。
当被代理服务器被设置为只接受来自特定 IP 地址或者 IP 范围的连接请求时,这特别有用。
例子
location /app1/ {
proxy_bind 127.0.0.1;
proxy_pass http://example.com/app1/;
}
location /app2/ {
proxy_bind 127.0.0.2;
proxy_pass http://example.com/app2/;
}
ip 地址也可以是一个变量
例子
location /app3/ {
proxy_bind $server_addr;
proxy_pass http://example.com/app3/;

posted @ 2017-11-27 08:59  李雄  阅读(4019)  评论(0编辑  收藏  举报