Ngxin安装、反向代理、负载均衡学习笔记

    xiaoxiao2021-03-26  32

    1 什么是Nginx

    Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

    2 特点

    2.1 反向代理

    我们经常使用正向代理,即用户手动设置代理服务器的ip和端口号,通过代理服务器去请求目标服务器,目标服务器会认为请求者为代理服务器,VPN就是这个意思。 反向代理是用来代理服务器的,用户不需要设置,用户认为访问的就是目标服务器。

    2.2 负载均衡

    原理就是数据流量分摊到多个服务器上执行,减轻每台服务器的压力,多台服务器共同完成工作任务,从而提高了数据吞吐量。

    3 安装Nginx

    官网下载nginx源码包解压缩 tar -zxvf nginx.zip 编译、安装 ./configue && make && make install启动 ./usr/local/bin/nginx访问localhost看到欢迎界面

    4 Nginx命令

    启动 nginx强制关闭 nginx -s stop正常关闭 nginx -s quit动态加载配置文件 nginx -s reload

    5 Nginx配置

    proxy_pass 配置代理 upstream 配置集群

    worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_tomcat{ server 127.0.0.1:8080; server 127.0.0.1:8081; #ip_hash; } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://server_tomcat; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

    当访问localhost时,由于没有指定负载均衡算法,所以默认为轮询,浏览器每次请求都会被轮流转发到8080和8081两个端口。 可能出现防火墙问题,开放80端口 /sbin/iptables -I INPUT -p tcp –dport 80 -j ACCEPT /etc/rc.d/init.d/iptables save

    6 Session共享问题

    使用Nginx反向代理集群会引出Session共享问题,有两种解决方案。

    方案一:(只在windows下有效)

    web服务器解决(广播机制)注意:tomcat下性能低修改两个地方 修改tomcat的server.xml支持共享。 将引擎标签下的 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 注释去掉.修改项目的配置文件 web.xml中添加一个节点<distributable/>

    方案二:

    可以将session的id放入redis中

    方案三:

    保证一个ip永远访问的是同一台服务器,就不存在session共享问题了。在nginx配置文件中添加 ip_hash;
    转载请注明原文地址: https://ju.6miu.com/read-662868.html

    最新回复(0)