nginx之负载均衡与ssl

nginx之负载均衡


nginx特点


高性能是因为用c语言编写,编译运行。
高并发是因为使用的nio模型,速度特别快。
低内存占用,C语言的数组,数据比较紧凑,数据传输之间采用了零拷贝。
配置文件简单

nginx与其他服务器的区别

nginx使用的是nio模型,单线程,当有一万个连接过来他会nio的模式去处理,然后单个线程操作
其他服务器使用的是阻塞io,多线程模式,当有一万个连接过来他要开一万个线程

正向代理与反向代理

正向代理:场景客户端无法访问到一个网络资源的时候,需要代理服务器去获取, 请求循序,客户端 请求正向代理服务器  正向代理服务器获取网络资源 返回给客户端

反向代理:场景负载均衡,安全检查,我们不开放应用服务器的ip给别人访问,别人只能访问我nginx来获取我的服务器资源,这个时候我们可以在这里做WAF安全检测,也可以做负载均衡,将打到自己身上的流量按照算法分配到不同的应用服务器上,减轻服务器压力,提高访问速度

在这里插入图片描述
nginx的四种实现方式

轮询:按照配置顺序一个个执行
最小连接数:看那个连接数最少就去哪台服务器
ip_hash:通过ip分配服务器,其他方式都有session共享问题,就是我在A服务登录但是B服务器不知道,然后就又要重新登录了
基于权重:这种我们可以把,服务器性能好的服务器打多点流量

nginx 配置
轮询
在这里插入图片描述
ip_hash
在这里插入图片描述
最少连接
在这里插入图片描述
权重
在这里插入图片描述

events {
    #使用 epoll模型
    use epoll;
    #定义访问流量
    worker_connections  10240;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
    keepalive_timeout  65;

    upstream liuqiyou.com{
   		#最小连接
        least_conn;
        #ip地址
        #ip_hash;
        server 121.199.11.93:3000 weight=2;
        server 121.199.11.93:4000 weight=1;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://liuqiyou.com;
        }

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



ngxin配置ssl的配文件,


#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 {
    use epoll;
    worker_connections  10240;
}


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;

   upstream liuqiyou.com{
   		#最小连接
        least_conn;
        #ip地址
        #ip_hash;
        server 121.199.11.93:3000 weight=2;
        server 121.199.11.93:4000 weight=1;
    }

    server {
        listen       80;
        server_name liuqiyoulike.cn;
    	#rewrite ^(. * )$ https://$host$1 permanent; #将所有HTTP请求通过rewrite指令重定向到HTTPS。

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
        location / {
            proxy_pass http://liuqiyou.com;
	    #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;
    #    }
    #}

server {
    listen 443 ssl;
    #配置HTTPS的默认访问端口为443。
    #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
    #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
    server_name liuqiyoulike.cn; #需要将yourdomain.com替换成证书绑定的域名。
    root html;
    index index.html index.htm;
    ssl_certificate cert/www.liuqiyoulike.cn.pem;  #需要将cert-file-name.pem替换成已上传的证书文件的名称。
    ssl_certificate_key cert/www.liuqiyoulike.cn.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #表示使用的加密套件的类型。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
    ssl_prefer_server_ciphers on;
    location / {
	 proxy_pass http://liuqiyou.com;
        #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;
    #    }
    #}

}

效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值