NGINX用处:
1、反向代理:反向代理(ReverseProxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,简单来说就是真实的服务器不能直接被外部网络访问,想要访问必须通过代理。
2、动静分离:运用Nginx的反向代理功能分发请求:所有动态资源的请求交给应用服务器,而静态资源的请求(例如图片、视频、CSS、JavaScript文件等)则直接由Nginx返回到浏览器,这样能大大减轻应用服务器的压力
3、负载均衡:负载均衡也是 Nginx常用的一个功能,当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃。为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力。我们可以建立很多很多服务器,组成一个服务器集群,当用户访问网站时,先访问一个中间服务器,在让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该服务器。如此以来,用户的每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。负载均衡配置一般都需要同时配置反向代理,通过反向代理跳转到负载均衡。
tomcat主要处理servlet业务,Nginx主要处理请求。
实现负载均衡配置:
测试配置:
//测试配置
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream test { #定义负载均衡设备的Ip及设备状态(可以不加)
server **.***.***.**:8080 weight=2;
server **.***.***.**:8081 weight=2;
ip_hash; //负载均衡算法
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
Nginx负载均衡五种算法:
1.round robin(默认):轮询方式,依次将请求分配到各个后台服务器中,默认的负载均衡方式。 适用于后台机器性能一致的情况。 挂掉的机器可以自动从服务列表中剔除。
2.weight:根据权重来分发请求到不同的机器中,指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3. IP_hash
根据请求者ip的hash值将请求发送到后台服务器中,可以保证来自同一ip的请求被打到固定的机器上,可以解决session问题。
例如:
upstream bakend {
ip_hash;
server ***.***.*.**:88;
server ***.***.*.**:80;
}
4.url_hash(第三方)必须安装Nginx的hash软件包
根据请求的url的hash值将请求分到不同的机器中,当后台服务器为缓存的时候效率高。
例如:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法 。
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
5. fair(第三方)必须安装upstream_fair模块
根据后台响应时间来分发请求,响应时间短的分发的请求多。
upstream backend {
server server1;
server server2;
fair;
}
Nginx的配置文件结构:
nginx.conf由多个块组成,最外面的块是main,main包含Events和HTTP,HTTP包含upstream和多个Server,Server又包含多个location:
main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和 location(URL匹配特定位置的设置)。
- main块设置的指令将影响其他所有设置;
- server块的指令主要用于指定主机和端口;
- upstream指令主要用于负载均衡,设置一系列的后端服务器;
- location块用于匹配网页位置。
其他配置讲解参考:https://blog.csdn.net/wangbin_0729/article/details/82109693
nginx实现请求转发
例如有 webmail , webcom 以及 webdefault 三个服务器分别运行在 portmail , portcom , portdefault 端口,要实现从80端口同时访问这三个web服务器,则可以在80端口运行 nginx, 然后将 /mail 下的请求转发到 webmail 服务器, 将 /com下的请求转发到 webcom 服务器, 将其他所有请求转发到 webdefault 服务器。假设服务器域名为example.com,则对应的 nginx http配置如下:
http {
server {
server_name example.com;
location /mail/ {
proxy_pass http://example.com:protmail/;
}
location /com/ {
proxy_pass http://example.com:portcom/main/;
}
location / {
proxy_pass http://example.com:portdefault;
}
}
}
以上的配置会按以下规则转发请求( GET 和 POST 请求都会转发):
将 http://example.com/mail/ 下的请求转发到 http://example.com:portmail/
将 http://example.com/com/ 下的请求转发到 http://example.com:portcom/main/
将其它所有请求转发到 http://example.com:portdefault/
需要注意的是,在以上的配置中,webdefault 的代理服务器设置是没有指定URI的,而 webmail 和 webcom 的代理服务器设置是指定了URI的(分别为 / 和 /main/)。
如果代理服务器地址中是带有URI的,此URI会替换掉 location 所匹配的URI部分。
而如果代理服务器地址中是不带有URI的,则会用完整的请求URL来转发到代理服务器。FTP
官方文档描述:
If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter.
If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).
以上配置的转发示例:
http://example.com/mail/index.html -> http://example.com:portmail/index.html
http://example.com/com/index.html -> http://example.com:portcom/main/index.html
http://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpg
http://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.css
http://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm
server {
listen 80;
autoindex off;
server_name localhost;
charset 'utf-8';
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
# error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
#
location / {
root dist;
try_files $uri $uri/ /index.html;
}
location /sender {
proxy_pass http://localhost:8081/sender;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
NGINX相关命令
ps -ef |grep nginx #查看是否正在运行
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
vi /usr/local/nginx/conf/nginx.conf #修改配置文件
tail -f /usr/local/nginx/logs/ 查看日志
Nginx负载均衡高可用
不停机更新