locate nginx.conf
那如何确定nginx当前生效的是哪个nginx.conf呢,很简单使用nginx -T命令即可查看当前生效的nginx.conf,如下:可以看到我当前生效的是 /etc/nginx/nginx.conf这个文件(我是使用的 systemctl start nginx.service命令启动的,未指定用哪个文件启动,所以可以看出默认使用的是 /etc/nginx/nginx.conf这个配置文件)
另外还有一个就是nginx的日志,我发现我的nginx日志就不是在 /usr/local/nginx/logs/这个目录下,而是放到了/var/log/nginx/ 这个目录下了(ps:log文件的存放和我的nginx.conf文件中的 access_log配置有关系)。如下演示:
各模块的功能作用如下描述:
- 全局模块: 配置影响nginx全局的指令,比如运行nginx的用户名,nginx进程pid存放路径,日志存放路径,配置文件引入,worker进程数等。
- events块: 配置影响nginx服务器或与用户的网络连接。比如每个进程的最大连接数,选取哪种事件驱动模型(select/poll epoll或者是其他等等nginx支持的)来处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
- http块: 可以嵌套多个server,配置代理,缓存,日志格式定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
- server块: 配置虚拟主机的相关参数比如域名端口等等,一个http中可以有多个server。
- location块: 配置url路由规则
- upstream块: 配置上游服务器的地址以及负载均衡策略和重试策略等等
(注意上边的 proxy_pass http://mybackendserver/ 后边这个斜线加和不加区别挺大的,加的话不会拼接/backend
, 而不加的话会拼接 /backend
,这一点我们在15.5.3小节会讲到,这里留意下就好)
15.5.3、proxy_pass 中的斜线与root和 alias的相似之处
在我们上边的负载均衡以及动静锋利等等演示中,可以看到我们的proxy_pass的配置基本上都是这么配的:
proxy_pass http://mybackendserver/
这里有个东西和root与alias的规则很像,所以我们在这里也提一下,就是 http://mybackendserver/ 后边这个斜线 /,如果你不写 / 则会将location的url拼接到路径后边,如果你写了则不会,下边我们演示下这样更直观些。修改nginx.conf如下:
interface 这个location不会拼接 interface到 http://mybackendserver/ 后边
interface2 这个location 会拼接 interface2到 http://mybackendserver/ 后边(从而导致接口404)
看出来了吗,proxy_pass 值后边加斜线和不加斜线 ,区别是很大的。不加斜线 有点像root(会拼接),加了斜线 有点像alias (不会进行 拼接)。要牢记这个事情。
如果是root,会把请求url的 ip/域名+port
替换为root指定的目录,访问资源
如果是alias,会把请求url的ip/域名+port+匹配到的路径
替换为alias指定的目录,访问资源
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# log_format debug '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#
log_format debug ' $remote_user [$time_local] $http_x_Forwarded_for $remote_addr $request '
'$http_x_forwarded_for '
'$upstream_addr '
'ups_resp_time: $upstream_response_time '
'request_time: $request_time';
access_log /var/log/nginx/access.log debug;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
upstream mybackendserver {
server 172.30.128.64:8081 fail_timeout=60s max_fails=3; # 60秒内 如果请求某一个应用失败3次,则认为该应用宕机 时间到后再有请求进来继续尝试连接宕机应用且仅尝试 1 次,如果还是失败,则继续等待 60 秒...以此循环,直到恢复
server 172.30.128.64:8082;
server 172.30.128.64:8083 backup; # 设置8083位备机
}
# 开启/关闭 压缩机制
gzip on;
# 根据文件类型选择 是否开启压缩机制
gzip_types text/plain application/javascript text/css application/xml text/javascript image/jpeg image/jpg image/gif image/png application/json;
# 设置压缩级别,越高资源消耗越大越耗时,但压缩效果越好
gzip_comp_level 9;
# 设置是否携带Vary:Accept-Encoding 的响应头
gzip_vary on;
# 处理压缩请求的 缓冲区数量和大小
gzip_buffers 32 64k;
# 对于不支持压缩功能的客户端请求 不开启压缩机制
gzip_disable "MSIE [1-6]\."; # 比如低版本的IE浏览器不支持压缩
# 设置压缩功能所支持的HTTP最低版本
gzip_http_version 1.1;
# 设置触发压缩的最小阈值
gzip_min_length 2k;
# off/any/expired/no-cache/no-store/private/no_last_modified/no_etag/auth 根据不同配置对后端服务器的响应结果进行压缩
gzip_proxied any;
# 指定缓存存放目录为/usr/local/nginx/test/nginx_cache_storage,并设置缓存名称为mycache,大小为64m, 1天未被访问过的缓存将自动清除,磁盘中缓存的最大容量为1gb
proxy_cache_path /usr/local/nginx/test/nginx_cache_storage levels=1:2 keys_zone=mycache:64m inactive=1d max_size=1g;
# 对请求速率限流
#limit_req_zone $binary_remote_addr zone=myRateLimit:10m rate=5r/s;
# 对请求连接数限流
limit_conn_zone $binary_remote_addr zone=myConnLimit:10m;
# --------------------HTTP 演示 配置---------------------
server {
listen 80 default;
charset utf-8;
server_name www.hzznb-xzll.xyz hzznb-xzll.xyz;
#location /static/ {
# root /usr/local/nginx/test; # /usr/local/nginx/test/static/xxx.jpg
#}
location /static { # 注意一般 alias的 url都不带后边的/
alias /usr/local/nginx/test/; # 使用alias时 这里的目录最后边一定要加/ 否则就404
}
# 测试autoindex效果
location /book/ {
root /usr/local/nginx/test;
autoindex on; # 打开 autoindex,,可选参数有 on | off
autoindex_format html; # 以html的方式进行格式化,可选参数有 html | json | xml
autoindex_exact_size on; # 修改为off,会以KB、MB、GB显示文件大小,默认为on以bytes显示出⽂件的确切⼤⼩
autoindex_localtime off; # 显示⽂件时间 GMT格式
}
# 临时重定向
location /temp_redir {
rewrite ^/(.*) 百度一下,你就知道 redirect;
}
# 永久重定向
location /forever_redir {
rewrite ^/(.*) 百度一下,你就知道 permanent;
}
# rewrite last规则测试
location /1 {
rewrite /1/(.*) /2/$1 last;
}
location /2 {
rewrite /2/(.*) /3/$1 last;
}
location /3 {
alias '/usr/local/nginx/test/static/';
index location_last_test.html;
}
}
# --------------------HTTP配置---------------------
server {
listen 80;
charset utf-8;
#server_name www.xxxadminsystem.com;
server_name www.hzznbc-xzll.xyz hzznbc-xzll.xyz;
# 重定向,会显示跳转的地址server_name,如果访问的地址没有匹配会默认使用第一个,即www.likeong.icu
return 301 https://$server_name$request_uri;
# # 指定 username 参数中只要有字母 就不走nginx缓存
# if ($arg_username ~ [a-z]) {
# set $cache_name "no cache";
# }
# # 前端页面资源
# location /page {
# alias '/usr/local/nginx/test/static/';
# index index_page.html;
# allow all;
# }
# # 后端服务
# location /interface {
# proxy_pass http://mybackendserver/;
# # 使用名为 mycache 的缓存空间
# proxy_cache mycache;
# # 对于200 206 状态码的数据缓存2分钟
# proxy_cache_valid 200 206 1m;
# # 定义生成缓存键的规则(请求的url+参数作为缓存key)
# proxy_cache_key $host$uri$is_args$args;
# # 资源至少被重复访问2次后再加入缓存
# proxy_cache_min_uses 3;
# # 出现重复请求时,只让其中一个去后端读数据,其他的从缓存中读取
# proxy_cache_lock on;
# # 上面的锁 超时时间为4s,超过4s未获取数据,其他请求直接去后端
# proxy_cache_lock_timeout 4s;
# # 对于请求参数中有字母的 不走nginx缓存
# proxy_no_cache $cache_name; # 判断该变量是否有值,如果有值则不进行缓存,没有值则进行缓存
# # 在响应头中添加一个缓存是否命中的状态(便于调试)
# add_header Cache-status $upstream_cache_status;
# limit_conn myConnLimit 12;
# limit_req zone=myRateLimit burst=5 nodelay;
# limit_req_status 520;
# limit_req_log_level info;
#}
}
# --------------------HTTPS 配置---------------------
server {
#SSL 默认访问端口号为 443
listen 443 ssl;
#填写绑定证书的域名
server_name www.hzznb-xzll.xyz hzznb-xzll.xyz;
#请填写证书文件的相对路径或绝对路径
ssl_certificate /usr/local/nginx/certificate/hzznb-xzll.xyz_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key /usr/local/nginx/certificate/hzznb-xzll.xyz.key;
#停止通信时,加密会话的有效期,在该时间段内不需要重新交换密钥
ssl_session_timeout 5m;
#服务器支持的TLS版本
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
#开启由服务器决定采用的密码套件
ssl_prefer_server_ciphers on;
# 指定 username 参数中只要有字母 就不走nginx缓存
if ($arg_username ~ [a-z]) {
set $cache_name "no cache";
}
# 前端页面资源
location /page {
alias '/usr/local/nginx/test/static/';
index index_page.html;
allow all;
}
# 后端服务
location /interface {
proxy_pass http://mybackendserver/;
# 指定哪些错误状态才执行 重试
proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404;
# 使用名为 mycache 的缓存空间
proxy_cache mycache;
# 对于200 206 状态码的数据缓存2分钟
proxy_cache_valid 200 206 1m;
# 定义生成缓存键的规则(请求的url+参数作为缓存key)
proxy_cache_key $host$uri$is_args$args;
# 资源至少被重复访问2次后再加入缓存
proxy_cache_min_uses 3;
# 出现重复请求时,只让其中一个去后端读数据,其他的从缓存中读取
proxy_cache_lock on;
# 上面的锁 超时时间为4s,超过4s未获取数据,其他请求直接去后端
proxy_cache_lock_timeout 4s;
# 对于请求参数中有字母的 不走nginx缓存
proxy_no_cache $cache_name; # 判断该变量是否有值,如果有值则不进行缓存,没有值则进行缓存
# 在响应头中添加一个缓存是否命中的状态(便于调试)
add_header Cache-status $upstream_cache_status;
limit_conn myConnLimit 12;
# limit_req zone=myRateLimit burst=5 nodelay;
# limit_req_status 520;
# limit_req_log_level info;
}
# 后端服务
location /interface2 {
proxy_pass http://mybackendserver;
# 使用名为 mycache 的缓存空间
proxy_cache mycache;
# 对于200 206 状态码的数据缓存2分钟
proxy_cache_valid 200 206 1m;
# 定义生成缓存键的规则(请求的url+参数作为缓存key)
proxy_cache_key $host$uri$is_args$args;
# 资源至少被重复访问2次后再加入缓存
proxy_cache_min_uses 3;
# 出现重复请求时,只让其中一个去后端读数据,其他的从缓存中读取
proxy_cache_lock on;
# 上面的锁 超时时间为4s,超过4s未获取数据,其他请求直接去后端
proxy_cache_lock_timeout 4s;
# 对于请求参数中有字母的 不走nginx缓存
proxy_no_cache $cache_name; # 判断该变量是否有值,如果有值则不进行缓存,没有值则进行缓存
# 在响应头中添加一个缓存是否命中的状态(便于调试)
add_header Cache-status $upstream_cache_status;
limit_conn myConnLimit 12;
# limit_req zone=myRateLimit burst=5 nodelay;
# limit_req_status 520;
# limit_req_log_level info;
}
}
# include /etc/nginx/conf.d/*.conf;
}
一、nginx
nginx:是一个高性能的http和反向代理服务器,特点是占用内存少,并发能力强。 专门为性能优化而开发,性能是其重要考量,实现上非常注重性能,能够经受住高负载的考验,有报告表名能支持50000并发量。
反向代理:客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到代理服务器由反向代理服务器去选择目标服务器获取数据后再返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真是服务器ip地址。
二、linux安装nginx
pcre-8.37.tar.gz
openssl-1.01t.tar.gz
zlib-1.2.8.tar.gz
nginx-1.11.1.tar.gz
第一步 pcre-config --version
安装方式:wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.7.tar.gz
wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
解压文件 ./configure完成后回到pcre目录制定make ,再执行make install
第二步,安装openssl
第三步,安装zlib
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
第四步 安装nginx
解压nginx-xx.tar.gz包
进入目录执行./configure
make && make install
安装成功后再usr中有个local/neginx ,在sbin有启动脚本
三、nginx常用命令
/usr/local/nginx/sbin
查看nginx版本
./neginx -v
启动nginx
./neginx
./nginx -c /nginx.conf
关闭nginx
./nginx -s stop
重新加载nginx
./neginx -s reload
nginx -t 编译
/******************
四、nginx.conf配置文件组成
(1)全局快
从配置文件开始到enents块之间,主要会设置一些影响nginx服务整体运行的配置
指令,比如:worker_processes:1 值越大代表支持的并发数越大
(2) events块
设计的指令主要nginx服务器与用户网络连接
比如:worker_connections:1024 支持的最大连接数
(3)http块
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
http包括http全局块和server块
五、nginx配置实例
location 指令说明:
该指令用于匹配url
语法如下
localhost [= | ~ | ~* | ^~] url {}
1 = : 用于不含有正则表达式的uri前面,要去请求字符串与uri严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2 ~ : 用于表示uri包含正则表达式并且区分大小写
3 ~* :用于表示uri包含正则表达式,并且不区分大小写
4 ^~ : 用于不含有正则表达式的uri前面,要去nginx服务器找到标识uri和请求字符串匹配度最高的localhost后。立即使用该localhost·处理请求,而不再使用location块中正则的rui和请求字符串做匹配。
注意,如果rui中包含正则表达式则必须有~ 或者~*标识。
反向代理,动静分离,负载均衡,高可用
1 反向代理-1
实现效果:打开浏览器输入地址www.123.com,跳转到linux的tomcat首页
2 反向代理-2
实现效果:使用nginx反向代理,根据访问路径跳转不同端口的服务器中,nginx监听9001端口
打开http://192.168.163.131:9001/deu 直接跳转到127.0.0.1:8080
打开http://138.168.163.131:9001/vod 直接跳转到127.0.0.1:8081
准备两个tomcat服务器,一个端口8080 ,一个端口8081
然后8080webapps创建文件夹edu下面有a.html,,8081的webapps下面有vod文件夹下面a.html,,8081的webapps下面有vod有a.html
找到nginx的配置文件配置:/usr/location/nginx/conf/nginx.conf
3 负载均衡
浏览器中输入http://192.168.163.131/deu/a.html,负载均衡效果拍平均8080和8081端口
1准备两台tomcat 8080 8081
2在webapps找那个创建edu文件夹,然后创建a.html文件用于测试。
3nginx中配置负载
随着互联网信息技的爆发性增长,负载均衡已经不在是一个很遥远陌生的词汇,顾名思义负载均衡就是将负载分摊道不同的服务单元。
既可保证服务的可用又可以保证服务的响应足够快,给用户很好的体验。快速增长的访问量和数据流量催生了各式各样的负载均衡铲平。
很多专业的负载均衡硬件提供了很好地功能。但价格不菲,这使得聚在均衡软件大受欢迎。
nginx就是其中一个。在linux下有nginx ,LVS , hparoxy等服务可以提供聚在均衡服务。而且ngin提供几种分配方式(策略)
1 轮询(默认)
每个请求按照时间顺序逐一分配到不同的后端服务器,如果服务器down掉,自动剔除。
2 weight
weight代表权,重默认为1,权重越高分配的客户端越多
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均衡的情况,如
upstream server_pool{
server 192.168.163.131:8080 weight=10;
server 192.168.163.132:8081 weight=10;
}
3 ip_hash
每个请求按照ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题,如果rui中包含正则表达式则必须有~
upstream server_pool {
ip_hash;
server 192.168.5.21:80;
server 192.168.5.22:80;
}
4 fair(第三方)
按后端服务器响应时间分配请求,响应时间越短有限分配
upstream server_pool{
server 192.168.5.21:80;
server 192.168.5.22:80;
fair;
}
4 动静分离
nginx动静分离就是把动态跟静态请求分开,不能理解为只是单纯的动态页面和静态页面物理分离。严格意义上说应该动态请求跟静态请求分开,可以理解为使用nginx处理静态页面,tomcat处理动态页面。 动静分离从目前实现上来说有两种:
一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案。
另一种是将动态和动态文件混合在一起发布。通过nginx来分开。通过location指定不同的后缀名实现不同的请求和转发。通过expires参数设置可以使浏览器缓存过期时间。减少与服务器之前的请求和流量。具体expires定义: 给一个资源设定一个过期时间,也就是说无需去服务端验证直接通过浏览器本身 确定是否过期即可。所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新文件,不建议使用expires来缓存),比如这里设置3d,表示3天之内访问这个url 发送一个请求,比如服务器该文件最后更新时间没有变化。则不会从服务器抓取。返回状态码304, 如果有修改,则直接从服务器重新下载,返回状态码200;
准备工作:
1 在linux系统准备静态资源,用于一会访问。
2 www image
3 修改配置文件
4 访问:http://192.168.163.131/image/%E5%AF%92%E7%83%9F%E6%9F%94.jpg
http://192.168.163.131/www/a.html
5 高可用
1准备工作:
1两台nginx服务器
2需要keepalived
3需要虚拟ip
2配置高可用准备工作:
(1)需要两台服务器
(2)在两台服务器安装nginx
(3)在两台服务器安装keepalived
3 在两台服务器安装keepalived
(1)使用yum命令安装
yum install keepalived -y
(2)安装之后再etc目录keepalived目录有文件keepalived.conf配置文件
4 完成高可用配置(主从配置)
修改etc/keepalived修改配置文件
在 usr/local/src添加检测脚本
/etc/hosts文件配置
5 启动nginx ,启动keepalived
./nginx
systemctl start keepalived.server
通过访问虚拟ip然后访问到实际的nginx的ip,然后转发到服务器ip
页面输入192.168.163.50
ip a看地址
配置文件:keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.163.131
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port{
script "/usr/local/src/nginx_check.sh"
interval 2 # 检查脚本执行间隔时间
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上将MASTER改为BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主备机器的必须一致
priority 90 #主备机器获取不同的优先级,主机大,备机小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.163.50 #vrrp h虚拟地址
# 192.168.200.16
# 192.168.200.17
# 192.168.200.18
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
脚本文件:
#!/bin/bash
A=`ps -C nginx -no-header | wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
killall keepalived
fi
fi
6 正向代理
六 nginx原理
首先,对于每个worker进程来说,独立的进程,不需要加锁,所以省去了锁带来的开销。同时在变成以及问题查找时候也会更加方便。其次,采用独立的进程,可以让相互之间不会影响,一个进程退出后其他的进程还在工作,服务不会中断,master进程则很快启动新的worker进程。当然,worker进程异常退出肯定程序有bug,异常退出会导致单前worker上所有的请求失败,不过不会影响所有的进程。所以降低了风险。
nginx采用redis类似的io多路复用机制,每个woker都是独立的进程。但是每个进程里只有一个主线程。通过异步非阻塞的方式来处理请求。即使千万个请求也没问题。没给worker线程可以把一个cup性能发挥到极致。所以worker数和服务器cup数相等最为适宜。设置少了浪费cup,设置多了cup频繁切换上下文带来损耗。
1 一个master和多个worker的好处
(1) 可以使用nginx -s reload 热部署
(2)每个worker是独立的进程,如果其中一个worker出现问题,其他woker还 可以继续争抢。实现请求过程不会服务中断。
2需要设置多少个worker
worker数和服务器cup数相等最为适宜
worker processes 4
# work绑定cup(4 work板顶4cpu)
worker cpu affinity 0001 0010 0100 1000
4 连接数worker_connection
表示每个worker进程所能建立连接的最大值。所以一个nginx能建立的最大连接数。应该是worker_connection * worker proceses.当然这里说的是最大连接数。对于http请求本地资源来说。能够支持的最大并发数是除以2 或者4.
发送请求占用woker的几个连接数:2或者4