nginx

一.什么是Nginx

1.概念在这里插入图片描述

nginx可以作为web服务器,但更多的时候,我们把它作为网关,因为它具备网关必备的功能:

  • 反向代理
  • 负载均衡
  • 动态路由
  • 请求过滤

2.nginx作为web服务器

Web服务器分2类:

  • web应用服务器,如:
    • tomcat
    • resin
    • jetty
  • web服务器,如:
    • Apache 服务器
    • Nginx
    • IIS

区分:web服务器不能解析jsp等页面,只能处理js、css、html等静态资源。
并发:web服务器的并发能力远高于web应用服务器。

二.nginx操作指令

nginx启动方法
进入sbin目录:执行 ./nginx
nginx修改配置文件不用重启,只需执行:./nginx -s reload
停止nginx:./nginx -s stop

三.nginx配置介绍

1.配置文件位于nginx目录下conf目录里面的nginx.conf

#worker_processes 代表工作进程的数量,通常为计算机的核数,也可以配置成auto
worker_processes  1;

# worker_connections 每个工作进程的最大连接数
events {
    worker_connections  1024;
}

# 表示nginx收到http请求,按下面配置处理
http {
    # 处理所有类型的mimetype
    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配置
    server {
		# listen:表示当前的代理服务器监听的端口,默认的是监听80端口。
        listen       80;
		# server_name:监听的域名
        server_name  manage.leyou.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		# / 代表监听这个域名下的所有请求路径
        location / {
            # 转发到哪个地址
			proxy_pass http://127.0.0.1:9001;
            proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }

        #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;
    #    }
    #}

}

2.nginx代理静态资源

server {
	# listen:表示当前的代理服务器监听的端口,默认的是监听80端口。
       listen       80;
	# server_name:表示监听来自api.leyou.com域名的请求
       server_name  image.leyou.com;
	   # 将请求代理到服务器的 /home/miracle/Downloads/temp 文件夹下
       location / {
		root /home/miracle/Downloads/temp;
       }

       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
   }

访问方式:http://image.leyou.com/1525516441005.png
这个图片的路径为:/home/miracle/Downloads/temp/1525516441005.png

3.nginx相同域名下配置多种路径拦截规则

	server {
		# listen:表示当前的代理服务器监听的端口,默认的是监听80端口。
        listen       80;
		# server_name:表示监听来自api.leyou.com域名的请求
        server_name  api.leyou.com;

        location /api/upload {
			proxy_pass http://127.0.0.1:8082;
            proxy_connect_timeout 600;
			proxy_read_timeout 600;
            # nginx 路径重写   
            # break:重写路径结束后,不再重新匹配路径。last:重写路径结束后,将得到的路径重新进行一些路径匹配
            rewrite "^/api/upluad/(.*)$" /$1 break
        }
        
        location / {
			proxy_pass http://127.0.0.1:10010;
            proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

4.nginx配置请求转发

server {
		# listen:表示当前的代理服务器监听的端口,默认的是监听80端口。
        listen       80;
		# server_name:表示监听来自www.mytest.com域名的请求
        server_name  www.mytest.com;

        location / {
            # 将这个域名 www.mytest.com 的请求,转发的 http://127.0.0.1:8082
			proxy_pass http://127.0.0.1:8082;
            proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
         
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

5.nginx配置负载均衡

upstream  his {
	# 这里两个Server表示负载均衡两个节点,默认转发的算法是轮询,weight=1 设置权重
	server 192.166.32.189:8080 weight=1;
	server 192.166.32.193:8080 weight=1;
}

server {
	# 表示当前的代理服务器监听的端口
    listen       8090; 
    # 表示当前的代理服务器监听的ip						
    server_name  192.166.32.193;		

    #charset koi8-r;
    #access_log  logs/phis3_access.log  main;
    #access_log  /dev/null;
    #windows close log
    access_log  nul;

    location / {
    	# 将请求转发到 upstream  his 
		proxy_pass  http://his;
	} 
}

四.nginx高可用

1.什么是负载均衡高可用

  • nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重。
  • 为了屏蔽负载均衡服务器的宕机,需要建立一个备份机。主服务器和备份机上都运行高可用(High Availability)监控程序,通过传送诸如“I am alive”这样的信息来监控对方的运行状况。当备份机不能在一定的时间内收到这样的信息时,它就接管主服务器的服务IP并继续提供负载均衡服务;当备份管理器又从主管理器收到“I am alive”这样的信息时,它就释放服务IP地址,这样的主服务器就开始再次提供负载均衡服务。

2.keepalived+nginx实现主备

(1)什么是keepalived
  • keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。
  • Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
(2)keepalived工作原理
  • keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。
  • 虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(VIP = Virtual IP Address,虚拟IP地址,该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到VRRP包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
  • keepalived主要有三个模块,分别是core、check和VRRP。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。VRRP模块是来实现VRRP协议的。
(3)keepalived+nginx实现主备过程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值