nginx配置(五)

本文详细介绍了Nginx的配置,包括ngx_http_fastcgi_module的缓存设置,如fastcgi_cache_path和fastcgi_cache_key,以及ngx_http_upstream_module的上游服务器组配置和调度算法。还提到了ngx_stream_core_module和ngx_stream_proxy_module,用于TCP和UDP服务的代理。最后简述了源码编译安装tengine-2.1.2的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ngx_http_fastcgi_module

fastcgi_cache_path

  1. path 缓存位置为磁盘上的文件系统路径
  2. levels=levels 缓存目录的层级数量,以及每一级的目录数量
    示例:leves=1:2:2
  3. keys_zone=name:size
    k/v映射的内存空间的名称及大小
  4. inactive=time 非活动时长
  5. fastcgi_cache zone|off;
    调用指定的缓存空间来缓存数据,可用位置:http、server、location
  6. fastcgi_cache_key string;
    定义用作缓存项的key的字符串
    示例:fastcgi_cache_key $request_uri;
  7. fastcgi_cache_methods GET|HEAD|POST …;
    为哪些请求方法使用缓存
  8. fastcgi_cahce_min_users numbers;
    缓存项在inactive定义的非活动时间内至少要被访问到指定的次数方可被认作活动项
  9. fastcgi_keep_conn on|off;
    收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
  10. fastcgi_cache_valid [code …] time;
    不同的响应码各自的缓存时长
  11. 示例:
http {
 	fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s max_size=1g;
}

server {
    listen 80;
    server_name www.fastcgi-cache.com;
    root /data/fastcgi-cache/;
    index index.php index.html;
    location ~*\.php$ {
            root /data/php/;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name;
            include fastcgi_params;
            include fastcgi.conf;
            fastcgi_cache fcgicache;
            fastcgi_cache_key $request_uri;
            fastcgi_cache_valid 200 302 10m;
            fastcgi_cache_valid 301 1h;
            fastcgi_cache_valid any 1m;
            add_header X-Cache $upstream_cache_status;
    }
}

ngx_http_upstream_module

  1. ngx_http_upstream_module模块
    将多个服务器定义成服务器组,而由proxy_pass, fastcgi_pass等指令进行引用

  2. upstream name { … }
    定义后端服务器组,会引入一个新的上下文
    默认调度算法是wrr
    Context: http
    upstream httpdsrvs {
    server …
    server…

    }

  3. 示例

// 定义在 http 语句块中
    upstream websrvs {
        server 192.168.43.27:80 weight=3 down; // 灰度发布
        server 192.168.43.37:80 weight=2;
    }
  // 在虚拟主机中定义
   location / {
        proxy_pass http://websrvs;
    }

注意:
upstream web_srvs {} 不要用 web_srvs,客户端访问不支持,应该用下面这种方式
upstream websrvs {}

  1. server address [parameters];

    address的表示格式:

    1. unix:/PATH/TO/SOME_SOCK_FILE
    2. IP[:PORT]
    3. HOSTNAME[:PORT]

    parameters:

    1. weight=number 权重,默认为1
    2. max_conns 连接后端报务器最大并发活动连接数,1.11.5后支持
    3. max_fails=number 失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用,默认为1
    4. fail_timeout=time 后端服务器标记为不可用状态的连接超时时长,默认10s
    5. backup 将服务器标记为“备用”,即所有服务器均不可用时才启用
    6. down 标记为“不可用”,实现灰度发布
  2. nginx的调度算法

    1. ip_hash 源地址 hash 调度方法
    2. least_conn 最少连接调度算法,当server拥有不同的权重时其为wlc,当所有后端主机连接数相同时,则使用wrr,适用于长连接
    3. hash key [consistent] 基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者组合
      作用:将请求分类,同一类请求将发往同一个upstream server,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用
      hash $request_uri consistent; 相当于 lvs 中的 dh算法
      hash $remote_addr; 相当于 ip_hash
      hash $cookie_ name ; #key为 name 的cookie

示例:

upstream webservs {
        server 192.168.43.17:80 weight=3 ;
        server 192.168.43.37:80 weight=2;
        server 127.0.0.1:8080 backup;
        #ip_hash;
        #least_conn;
        #hash $remote_addr;
        #hash $request_uri;
        #hash $cookie_sessionid; // 同一cookie 调度到同一台服务器上,根据带有的sessionid的不同来区分客户端
        hash $cookie_sessionid;
        }

 curl -b sessionid=123456 http://www.fastcgi.com  // 指定 cookie来进行测试

keepalive 连接数N;
为每个worker进程保留的空闲的长连接数量,可节约nginx端口,并减少连接管理的消耗

一致性hash算法
hash key consistent;

在这里插入图片描述
如果有一台 varnish 挂了或再增加一台varnish,则这个算法又得重新计算,付出的代价太大了,于是有了下面的算法:一致性 hash 算法
在这里插入图片描述

ngx_stream_core_module

ngx_stream_core_module模块
模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器

  1. listen
    listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind][ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

ngx_stream_proxy_module

ngx_stream_proxy_module模块可实现代理基于TCP,UDP (1.9.13), UNIX-domain sockets的数据流

  1. proxy_pass address; 指定后端服务器地址
  2. proxy_timeout timeout;
    无数据传输时,保持连接状态的超时时长,默认为10m
  3. proxy_connect_timeout time;
    设置nginx与被代理的服务器尝试建立连接的超时时长,默认为60s
  4. 示例:
    在这里插入图片描述
    proxy
// main语句块中
stream {
    upstream mariadbsrvs {
        server 192.168.43.27:3306;
        server 192.168.43.37:3306;
    }
    server {
        listen 192.168.43.7:3306;
        proxy_pass mariadbsrvs;
        proxy_timeout 60s;
        proxy_connect_timeout 10s;
    }
}

mariadb1

mysql -e "create database if not exsits db27;grant all on db27.* to test@'192.168.43.7' identified by 'test';flush privileges;"

mariadb2

mysql -e "create database if not exists db37;grant all on db37.* to test@'192.168.43.7' identified by 'test';flush privileges;"

client

while :;do mysql -utest -ptest -h 192.168.43.7 -e "show databases;";sleep 1;done

在这里插入图片描述
proxy

stream {

    upstream redissrvs {
        server 192.168.43.27:6379;
        server 192.168.43.37:6379;
    }
    server {
        listen 192.168.43.7:6379;
        proxy_pass redissrvs;
    }

}

redis1

yum --enablerepo=epel install redis -y
vim /etc/redis.conf
	bind 192.168.43.27
systemctl start redis

redis-cli -h 192.168.43.27
192.168.43.27:6379> set name peng
192.168.43.27:6379> exit

redis2

yum --enablerepo=epel install redis -y
vim /etc/redis.conf
	bind 192.168.43.37
systemctl start redis

redis-cli -h 192.168.43.37
192.168.43.27:6379> set name xue
192.168.43.27:6379> exit

client

yum --enablerepo=epel install redis -y
while :;do redis-cli -h 192.168.43.7 get name;sleep 1;done

源码编译安装 tengine-2.1.2

tar xvf tengine-2.1.2.tar.gz
yum install pcre-devel openssl-devel zlib-devel gcc -y
cd tengine-2.1.2/
./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_concat_module=shared
make dso_install  // 安装动态模块
ls /apps/nginx/modules/ // 查看动态模块
ls /apps/nginx/modules/ngx_http_concat_module.so

vim /apps/nginx/conf/nginx.conf    // 配置文件中指定加载动态模块
	dso {
        load ngx_http_concat_module.so;
	}
ln -s /apps/nginx/sbin/nginx /usr/sbin/tengine
tengine
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值