Nginx集群 反向代理和缓存

1. Nginx集群 前端 后台服务许多机器组合在一起,共同完成任务


2.)Nginx除了是一个强大的静态web服务器,也是强大的负载均衡器,通过应用层完成负载均衡,构建成负载均衡集群架构,Nginx的集群本质上是基于反向代理来实现。


3.)upstream server:定义负载均衡的容器 上游服务器,即被代理服务器所连接的后台真实服务器

回顾Nginx的配置

main全局     event 事件    http网站

http {
 ……
server {
 listen
server_name
location {
if {
 }
 }
}


 

 

 


4.)Nginx的代理模块    代替客户端去访问

1)proxy模块ngx_http_proxy模块支持

2)upstream模块ngx_http_upstream模块支持

3)fastcgi模块ngx_http_fastcgi模块支持

5、proxy模块proxy模块(ngx_http_proxy)




1.)反向代理  格式

以proxy模块配置反向代理,格式:

location /uri {                     ---目录
        proxy_pass http://upstream_server:port/[newuri];   #(可代理“/”,也可自己作为web服务器对客户提供服务)
}
代理
server {
    listen IP:PORT;
    server_name web.ujiue.com;
    location / {          --请求的资源
        proxy_pass http://URL;把请求转发给这个真实的服务器
    }
}

 

nginx集群部署springBoot项目 nginx集群架构_nginx

 

 

 


请求代理php的网页都交给10.110处理



10.110为discuz论坛  LAMNP的

 

nginx集群部署springBoot项目 nginx集群架构_html_02

 

 

 

10.120为src电影网站的IP

 

nginx集群部署springBoot项目 nginx集群架构_html_03

 

 

 






1.)Nginx    代理转发给    nginx

vim /etc/nginx/nginx.conf  --修改主配置文件
location / { 
      root   html;          --访问根目录下的html网页 
      index  index.html index.htm;                 --忽略
      proxy_pass http://192.168.10.120:80;           --直接返回10.120下根目录  下的网页
      if (!-e $document_root/ccc/index.html){
      rewrite ^/ccc/(.*)$  /zzz/$1 break;
        }

 

nginx集群部署springBoot项目 nginx集群架构_html_04

 

 

 

其实访问的是10.120下的

 

nginx集群部署springBoot项目 nginx集群架构_nginx_05

 

 

 

nginx集群部署springBoot项目 nginx集群架构_html_06

 

 



2.)Nginx    代理转发给    LAMP

vim /etc/nginx/nginx.conf

location /discuz {
            root   images;
            index  index.html index.htm;
            proxy_pass http://192.168.10.110;
        }

 

nginx集群部署springBoot项目 nginx集群架构_nginx_07

 

 

 


实际访问的10.110下的discuz目录这目录一定要有

cd /var/www/html/

 

nginx集群部署springBoot项目 nginx集群架构_nginx_08

 

 

 


3.)代理区域正则表达式  ip后不能加目录

如果在代理区域使用正则表达式,代理的地址不能有URI目录,否则为语法错误,


     

location ~* \.(jpg|gif|jp?g)$ {                   --请求图片
            root   "/html/txt";--到这个目录下去
            proxy_pass http://192.168.10.120;
            expires 1d;
            valid_referers none blocked 192.168.10.10;
            if ( $invalid_referer )
              {
               rewrite ^/ http://192.168.10.10/ddd/ujiuye.png break;
             }
             index  index.html index.htm;
        }


错误示例  加入目录就不能重启服务

 

nginx集群部署springBoot项目 nginx集群架构_缓存_09

 

 

 

注意:只要能被正则表达式匹配到,都会将请求调度到对应的代理服务器的URI,无需考虑前端URI的资源,只需考虑重写后的结果URI,

该代理方式不会记录源客户端的IP地址,记录代理的IP地址无法完成用户日志的精准分析。因此需要引用$remote_addr这个变量来记录基于源客户端的IP地址的日志。



2.)可记录访问客户端的IP地址的日志  yum安装的方式日志 和apache

查看日志

cd /var/log/nginx/
tail -f access.log这个nginx代理ip的地址,    不会显示客户端d

 

nginx集群部署springBoot项目 nginx集群架构_html_10

 

 

 

nginx集群部署springBoot项目 nginx集群架构_nginx_11

 

 



阿帕奇的httpd网页服务   会显示访问客户端的ip

cd /var/log/httpd/
tail -f access_log

 

nginx集群部署springBoot项目 nginx集群架构_缓存_12

 

 

 




1.Nginx   修改可查看访问的客户端的IP  记录


使用proxy_set_header配置方法(向后端服务器传输请求头)来定义remote_addr,

对于apache需要修改日志的格式,对于nginx不用修改日志格式


vim /etc/nginx/nginx.conf

location / {
     root   html;
     index  index.html index.htm;
     proxy_pass http://192.168.10.12:80;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   --请求头
     proxy_set_header  X-Forwarder-For $remote_addr;      --记录访问的ip
     if (!-e $document_root/ccc/index.html){
         rewrite ^/ccc/(.*)$  /ddd/$1 break;
   }
           # allow 192.168.10.10;
           # deny all;
        }

 

nginx集群部署springBoot项目 nginx集群架构_html_13

 

 

 

真机的ip 以看到10.250

 

nginx集群部署springBoot项目 nginx集群架构_html_14

 

 

 



2.)Apache 修改可查看客户端访问真实的ip  记录

vim /etc/httpd/conf/httpd.conf

 

nginx集群部署springBoot项目 nginx集群架构_nginx_15

 

 

 

修改apache日志格式,加入红色变量,并使用相应日志格式纪录日志:

LogFormat "%{X-Forwarder-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

修改为

 

nginx集群部署springBoot项目 nginx集群架构_html_16

 

 

 


定义的日志格式combined

CustomLog "logs/access_log" combined

 

nginx集群部署springBoot项目 nginx集群架构_nginx_17

 

 

 

 

3.Nginx中配置缓存

缓存:缓存数据在内存空间,以key-value键值对进行存储,以hash码进行定位指引

缓存路径的目录proxy_cache_path来定义在httpd上下文。


1.定义缓存路径配置方式    定义格式:

proxy_cache_path PATH levels=numbers keys_zone=name:size [interactive=time] [max_size=#k|M|G]

定义这段缓存的名字 名字大小

定义详解:

PATH:缓存路径

Level:级层目录

numbers:目录名称的字符量

Keys_zone:设置缓存区的名称和内存缓存空间大小

Inactive:设置数据多长时间没有被访问将被删除

max_size设置硬盘缓存空间大小

例如:

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;


缓存目录在/data/nginx/cache下允许出现二级子目录,一级子目录名称为一个字符,二级子目录为两个字符。

Keys_zone键存储的名称为one,大小为10M。注意:缓存目录属主属组必须是nginx



2.缓存文件示例:

/cache/nginx/b/02/c649288d058e4b67c2b30bfbbcf5502b

路径/cache/nginx/ 缓存目录在b允许出现二级子目录,一级子目录名称为一个b字符,二级子目录为两个字符。02        缓存信息   b为一级目录   02为二级目录




在http段中定义缓存,

定义完可以在http、server、location 中使用proxy_cache key_name(刚刚定义缓存的名字)调用。

3.定义缓存的其他属性

格式:proxy_cache_min_uses number;设置缓存的最小使用次数,默认为

例如proxy_cache_min_uses 5;

1. 设置客户端那些请求方法的响应将被缓存

格式:proxy_cache_methods GET |HEAD POST...

默认为GET和HEAD只要是这两种   就缓存


2. NGINX在刷新来自服务器的内容时使用GET请求  代理服务器向真实服务器发送请求  使用的方法       使用get方法  或者不使用get

proxy_cache_revalidate  on | off



3. 代理服务器 跟后台服务器 交互发生错误情况下,使用过期的缓存内容响应请求

代理服务器缓存过期,向后台服务器发送请求  期间有访问会  返回这个过期的缓存  直到新的缓存内容 完成   

proxy_cache_use_stale error timeout | invalid_header | http_500 ......

错误   超时    无效头         其余状态码


4. 添加代理服务器的地址  到首部

add_header X-Via $server_addr;


5. 添加  缓存命中状态到报文首部       命中状态

add_header X_cache_hit $upstream_cache_status;


6.对于 指定响应码的网页  设置缓存时间

proxy_cache_valid 响应码 time;


proxy_cache_valid 200 1d;

只要响应码为


4.配置缓存nginx服务端

1.首先在http配置段中定义缓存:

vim /etc/nginx/nginx.conf


proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m max_size=500M;

定义路径目录层级关系第一层是一个字符 子目录是两个字符名字大小硬盘空间最大

 

nginx集群部署springBoot项目 nginx集群架构_nginx_18

 

 

 



2.然后建立指定的路径

mkdir -p /date/nginx/cache
chown nginx:nginx /date/nginx/cache/
nginx  -s reload

 

nginx集群部署springBoot项目 nginx集群架构_html_19

 

 

 


3.然后在location中在定义 使用:

vim /etc/nginx/nginx.conf

proxy_cache one; 使用定义的缓存  名字为one的缓存
proxy_cache_valid 200 1d;响应码为200  缓存一天
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;其他响应码缓存一分钟
proxy_cache_revalidate on;内容刷新使用
proxy_cache_use_stale error timeout http_500 http_502 http_504;  发生错误等 返回缓存内容
add_header X_cache_hit $upstream_cache_status;  命中状态


 

nginx集群部署springBoot项目 nginx集群架构_html_20

 

 

 



在浏览器查看时,可使用f12,f5查看,注意:后端的真实服务器为apache时,没有x_cache_hit,后端服务器为nginx时有x_cache_hit.

 

nginx集群部署springBoot项目 nginx集群架构_nginx_21

 

 

 


5.upstream负载均衡模块

注意:只能定义在http配置段中, 配置负载均衡时,要将缓存注解,否则可能影响测试效果。


配置语法:

upstream name {  #定义一个负载均衡器,名字为name
[ip_hash] ;   指定负载均衡算法
server backend1.example.com [weight=5];(权重)   每个server表示一台主机
server 127.0.0.1:8080    max_fails=3 fail_timeout=30s;(表示如果请求转发错误3次,间隔30s再检查一次)
server backup1.example.com   backup|down;(backup:备份节点,如果需要可以顶上;down:永久下线)
}

 

nginx集群部署springBoot项目 nginx集群架构_html_22

 

 

 


在http{}配置段中:



upstream webservers{
server 192.168.10.120 max_fails=3 fail_timeout=30s weight=1;
server 192.168.10.110 max_fails=3 fail_timeout=30s weight=1;
server 192.168.10.100 backup;          --备份
}





定义webservers网站负载均衡,包括有192.168.10.120、192.168.10.110、192.168.10.100

100为备份。


定义upsteam后,在proxy_pass中需要改成upsteam命名的名称

      

location / {
            root   html;
            index  index.html index.htm;
            #proxy_cache one;     #缓存设置都要注释
            #proxy_cache_valid 200 1d;
            #proxy_cache_valid 301 302 1m;
            #proxy_cache_valid any 1m;
            #proxy_cache_revalidate on;
            #proxy_cache_use_stale error timeout http_500 http_502 http_504;
            #add_header X_cache_hit $upstream_cache_status;
            proxy_pass http://webservers;
            #proxy_pass http://192.168.10.12:80;
            #proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header  X-Forwarded-For $remote_addr;
            if (!-e $document_root/ccc/index.html){
                    rewrite ^/ccc/(.*)$  /ddd/$1 break;
            }
        }

 

nginx集群部署springBoot项目 nginx集群架构_缓存_23

 

 

 



为了记录真实客户端的IP地址,要修改apache的日志文件格式:

LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog "logs/access_log" combined


 

nginx集群部署springBoot项目 nginx集群架构_缓存_24

 

 

 

nginx集群部署springBoot项目 nginx集群架构_缓存_25

 

 







6.Nginx的调度算法:

rr:轮询,轮流分配请

wrr:加权轮询,参考权重轮分配请求(如:定义upsteam_server默认算法就是wrr)

Ip_hash: 源地址hash使用hash算法对源ip地址计算hash值,一样的hash值将请求送到相同服务器,实现session绑定

least_conn:最少连接调度算法


7.fastcgi模块(ngx_http_fastcgi)

Nginx默认无法以模块的方式连接php,但是可使用Fastcgi实现代理,将对php页面的请求转发给fastcgi,再轮发给9000端口进行处理。可以以fpm的方式调度fastcgi请求,同时定义PHP的fpm工作模式,进行参数设定即可。


fastcgi_connect_timeout time;(在http   server   location中定义)


例如:

fastcgi_connect_timeout 60s;   #通常不要超过75s


vim /etc/nginx/nginx.conf

 location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name; #可用$document_root变量
     include        fastcgi_params;
        }

 

nginx集群部署springBoot项目 nginx集群架构_html_26

 

 

 

nginx集群部署springBoot项目 nginx集群架构_缓存_27

 

 





fastcgi模块对应常见配置


fastcgi_pass     将fastcgi请求代理给指定的主机

fastcgi_pass address;(定义在location或者if location配置段中)

例如:

fastcgi_pass localhost:9000;
fastcgi_pass 127.0.01:9000;



fastcgi_index    基于fastcgi请求的默认索引页

例如:

fastcgi_index index.php

fastcgi_param    向后端传递的参数
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name; #可使用$document_root变量

include        fastcgi_params;fastcgi请求超时时长

生活是一种感受也是一种积累