0
点赞
收藏
分享

微信扫一扫

Nginx的主要特点


                                                                              Nginx的主要特点:

1.反向代理:internet的连接请求先交给代理服务器,然后讲请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上的请求连接客户端,此时代理服务器对外就表现为一个反向代理的服务器,简单的来说真实的服务器不能被直接外部网络访问,所以需要一台代理服务器,而代理服务器能够被外部网络访问的同时又跟真实服务器在同一个网络环境,当然也可能是同一个服务器,端口不同而已

一个例子实现反向代理的代码

server {

listen 80;

server_name localhost; client_max_body_size 1024M;

location / { proxy_pass http://localhost:8080;

proxy_set_header Host $host:$server_port; }

}

保存配置文件启动nginx,当我们访问localhost的时候,就相当于访问localhost:8080了

2.负载均衡:负载均衡就是分摊到多个操作单元上进行执行,web服务器,ftp服务器,等关键应用和任务服务器等,从而共同完成任务,当2台以上的服务器请求时,根据规则随机的将请求分发到指定的服务器上处理,负载均衡配置一般都需要同时配置反向代理,通过反向代理跳转到负载均衡,nginx目前注册自带3种负载均衡策略,2种常用的第三方策略

2.1 rr

简单配置

    upstream test {

        server localhost:8080;

        server localhost:8081;

    }

    server {

        listen       81;                                                         

        server_name  localhost;                                               

        client_max_body_size 1024M;

        location / {

            proxy_pass http://test;

            proxy_set_header Host $host:$server_port;

        }

    }

负载均衡的核心代码为

    upstream test {

        server localhost:8080;

        server localhost:8081;

    }

比如:配置2台服务器,实际一台,端口不一样,而8081的服务器是不存在的,访问不到的,但我们访问http://localhost的时候,也不会有问题,会默认跳转到8080,具体是因为neginx会自动判断服务器的状态,如果服务器不能访问了,就不会跳转到这个服务器,所以也避免了一台服务器挂了影响使用的情况,由于neginx默认rr策略,所以我们不需要其他更多的设置

2.2:权重:指定轮询的机利,weight和访问比率成正比,用于后端服务器性能不均的情况

upstream test{

server localhost:8080 weight=9;

server localhost:8081 weight=1;

那么10次一般只会有一次访问到8081,而有9次会访问到8080

这边简单介绍一下:

ip_hash,fair,url-hash,

3.HTTP服务器:nginx本身也是一个静态资源的服务器,当只有静态资源的时候,就可以使用nginx来做服务器,同时现在也很流行动静分离,就可以通过nginx来实现,首先看看nginx做静态资源服务器

例子:

server {

        listen       80;                                                         

        server_name  localhost;                                               

        client_max_body_size 1024M;

        location / {

               root   e:\wwwroot;

               index  index.html;

           }

    }

4.动静分离:动静分离是让动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就根据静态资源的特点,来做缓存,这就是网站静态化处理的核心思路

upstream test{

 server localhost:8080;

server localhost:8081;

 }

server {

listen 80;

server_name localhost; location / { root e:\wwwroot; index index.html;

}

 # 所有静态请求都由nginx处理,存放目录为html

location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { root e:\wwwroot; }

# 所有动态请求都转发给tomcat处理

 location ~ \.(jsp|do)$ { proxy_pass http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root e:\wwwroot; } }

5.正向代理:

一个位于客户端和原始服务器,之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求指定目标,然后代理向原始服务器转交请求并将获得的内容返回客户端。

例子

resolver 114.114.114.114 8.8.8.8;

server {

 resolver_timeout 5s;

listen 81;

access_log e:\wwwroot\proxy.access.log;

 error_log e:\wwwroot\proxy.error.log;

 location / { proxy_pass http://$host$request_uri;

 }

 }





举报

相关推荐

0 条评论