nginx

nginx是什么

Nginx (engine x) 是一个高性能、轻量级的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,大项公司也在用

nginx能做什么

反向代理

正向代理:页面访问一个网站的时候,给代理服务器发送一个请求并且指向目标服务器,目标服务器进行请求转发和响应返回;通过正向代理,浏览器需要对代理服务器进行设置

反向代理:客户端无需知道目标服务器的ip,只需要将请求发送到反向代理服务器,再经由反向代理服务器转发到真正的目标服务器。

动静分离

为了加快网页的加载速度,将动态资源(jsp,servlet)与静态资源(html,js,css)分别交由对应的服务器进行加载,,分开部署

负载均衡

并发量上升,将请求分发到各个服务器上,即将原先请求集中到单个服务器上的情况改为分发到多个服务器上

nginx常用指令

nginx -v                     # 查看版本

./nginx                      # 启动

ps -ef|grep nginx      # 查看是否启动成功

nginx -s stop            # 关闭

nginx -s reload         # 更改配置之后,重新加载

nginx的配置./conf/nginx.conf

##nginx启动时候,由哪个用户启动
user  nginx;

##值越大并发处理能力也就越大,但是也会收到物理硬件限制
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

##nginx支持的最大连接数
events {
    worker_connections  1024;
}

##反向代理,负载均衡等配置
http {

     ##包含的mime类型
    include       /etc/nginx/mime.types;

     ##默认的mime类型
    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  /var/log/nginx/access.log  main;
    ##开启高效文件传输模式
    sendfile        on;
    ##此选项允许或禁止使用socke的TCP_CORK的选项,跟sendfile一起使用
    #tcp_nopush     on;

    ##设置存活时间
    keepalive_timeout  65;

    ##是否开启gzip压缩传输   
    #gzip  on;

    ##引入其他配置
    include /etc/nginx/conf.d/*.conf;
}

defaut.conf

server {
    ##监听端口
    listen       80;
    listen  [::]:80;
    ##服务器名称
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    
    ##正则表达式,请求转发
    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/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;
    #}
}

nginx反向代理案例

效果:页面输入www.123.com:8090跳转到tomcat的欢迎页

tomcat:http://192.168.136.134:8093/

nginx:http://192.168.136.134:8090/

步骤:

1、浏览器输入www.123.com首先会去本地的hosts文件中找对应的ip,因此在hosts文件需要配置ip和域名的映射

C:\Windows\System32\drivers\etc\hosts

 配置好后就可以通过www.123.com:8090来访问http://192.168.136.134:8090/

 2、修改nginx的配置文件

  server_name  192.168.136.134;


    location / {
        root   /usr/share/nginx/html;
	proxy_pass  http://192.168.136.134:8093;
        index  index.html index.htm;
    }

3、测试

扩展

通过不同的url跳转不同网页的配置

 location ~/admin/ {
	proxy_pass  http://192.168.136.134:8081;
    }
 location ~/user/{
	proxy_pass  http://192.168.136.134:8082;
    }

当访问192.168.136.134:8090/admin/x ,跳转8081端口

当访问192.168.136.134:8090/user/x ,跳转8082端口

负载均衡

访问www.123.com:8090/index.html

负载均衡到

192.168.136.134:8081/index.html、

192.168.136.134:8082/index.html

两台服务器上面

相关配置:

upstream myserver{
    server 192.169.136.134:8081;
    server 192.169.136.134:8082;
    ##负载策略
    ip_hash;
}

    server{
        listen 8090;
        location / {
	        proxy_pass  http://myserver;
            index  index.html index.htm;
        }
    }
    

负载均衡策略

轮训(默认)

根据时间顺序依次访问目标服务器,如果服务器挂了,就剔除

权重(weight)

设置权重,权重越高,分配的请求数越多

ip_hash

对请求的ip进行hash,根据结果进行分配

fair第三方

根据后端响应时间来进行分配,响应时间短的服务器优先考虑分配

动静分离

expires:设置浏览器缓存的过期时间,会取服务器的文件最后更新时间是否发生变化

两种方案:

方案一、专门为静态资源设置一个域名,单独放一个服务器

方案二、通过nginx来区分动静资源

 location /css/ {
        ##指向存放css的目录
	    root /data/;
    }
 location /images/{
        ##指向存放图片的目录
	    root /data/;
    }

nginx的高可用方案

nginx主从集群+keepalive,对外提供一个虚拟nginx ip来访问nginx集群

从服务器怎么判断主服务器挂了

步骤

1、准备两台安装了nginx的服务器

服务器1 nginx访问地址为192.168.136.136:80

服务器2 nginx访问地址为192.168.136.130:80

2、在两台服务器通过yum指令安装keppalived

yum install keepalived -y

使用yum安装软件时报错
...
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
重点关注:libmysqlclient.so.18()(64bit)
解决:
缺乏Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm这个包

# wget http://www.percona.com/redir/downloads/Percona-XtraDB-Cluster/5.5.37-25.10/RPM/rhel6/x86_64/Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm
# rpm -ivh Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm

安装路径:/etc/keepalived

关键配置:/etc/keepalived/keepalived.conf

配置文件

##全局定义
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id 127.0.0.1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

##检测脚本
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"

interval 2	#(检测脚本执行的间隔)

weight 2

}

##虚拟ip设置,
vrrp_instance VI_1 {
	##当前nginx是主节点还是从节点
    state MASTER
	##在哪个网卡绑定虚拟主机
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
	##认证鉴权
    authentication {
        auth_type PASS
        auth_pass 1111
    }
	##虚拟ip地址,可配置多个
    virtual_ipaddress {
        192.168.134.128
    }
}

脚本配置  /usr/local/src/nginx_check.sh

#! /bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];
    #!nginx的启动指令
    then /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];
    	then killall keepalived
    fi
 fi

3、启动nginx和keepalived

sudo systemctl start keepalived.service

如果报错:Configuration file '/etc/keepalived/keepalived.conf' is not a regular non-executable file

把keepalived.conf配置文件的权限设置为644再次重启即可

 原理分析

每次启动nginx时都会启动至少两个进程,一个master,至少一个worker

任务来了之后会进入master,然后master下面的所有worker会去争抢任务,通过反向代理完成请求转发

1、方便进行热部署

2、每个worker是独立的进程,即使某个worker挂了,也不会影响其他worker继续争抢

3、nginx IO多路复用,worker数目=cpu核数一致

master:管理者

worker:实际干活的

woker连接数:一个请求占用多少个worker连接?如果是静态资源是两个连接,如果是动态资源需要访问tomcat,则是占用4个连接

nginx有一个master,有四个woker,每个woker支持最大的连接数1024,支持的最大并发数是多少?

最大连接数:4*1024 

用最大连接数除以2或者除以4即最大的并发数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值