Nginx set of features - reverse proxy, load balancing, caching, static and dynamic separation, smooth upgrade

Reverse Proxy

nginx configuration file

event-driven events {}

httpd {} on httpd configuration-related

define a virtual host server {}

location {} URI access attributes

 

location ~ * \. php $ {do regular expression, all to .php files ending

   fastcgi_pass 127.0.0.1:9000 reverse proxy, go 127.0.0.1:9000 this place needs to request data

 

    }

Reverse Proxy:

proxy_pass: This option directly specify a reverse proxy

proxy_set_header: When a user requests forwarded to the backend server, you can set a unique head!

 

Load Balancing

upstream webserver {     name server group defined       load balancing options for a server module defined outside

           ip_hash; to a request from the same client, always directed to the same server

          server 192.168.2.129  weight=1  max_fails=2     fail_timeout=2;

          Select an internal server error up to a weight of 1 times the duration of each examination

          server 192.168.2.128  weight=1  max_fails=2  fail_timeout=2;  

           server  127.0.0.1:8080  backup;    }    

           When the server all hang above, the emergence of this page!

server {

        listen       80;

        server_name  localhost;

 

location  /  {

             proxy_pass  HTTP: // webserver / ;    reverse proxy, this should be changed upstream group name, refers to all the members of this group of reverse proxy!

            }

       }

Redefine a server, set, when all hang our server appears on this page.

After defined server, the upstream add this server module!

server  {

 listen  8080;

 server_name localhost;

  root /www/a.org;

  index  index.html;

}

 

nginx supports three load balancing algorithms:

     round-robin: weighting default

     ip_hash: The request from the same client, and always directed to the same server

     least_conn: Minimum link

 

Cache

nginx cache consists of two parts:

    Shared memory: storing key data and cache objects

    Disk Space: storing data

 

proxy_cache_path defined buffer space, can not be defined in server {} module,

               levels=1:2:1    缓存目录:子目录级别,最多定义三个级别,每一级别字符最多2个!第一个级别1个字符,第二个级别2个字符。。。。

               keys_zone     存储键的区域,区域大小

               max_size      缓存目录大小

格式:proxy_cache_path   /ngnx/hun    levels=1:2:1    keys_zone=fst:20m    max_size=1G; 

                       缓存目录       缓存

cache_manager :根据最近最少使用算法,将那些此前没有用的缓存给他清除出去。

 

定义完缓存,还需要在location模块中启用它,proxy_cache   缓存名,这里的缓存名就是keys_zone中定义的名字

 

另外的三种缓存:

open_log_cache :日志缓存

open_file_cache

fastcgi_cache

 

动静分离

webDAV :一种基于HTTP1.1协议的通信协议,他扩展了HTTP1.1,使应用程序可直接对web  server 直接读写,并支持文件锁定及解锁,支持文件的版本控制

 

 

#动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstream tdt_wugk 均衡

    location ~ .*\.(php|jsp|cgi)?$

    {

         proxy_set_header Host  $host;

         proxy_set_header X-Real-IP $remote_addr;

         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

         proxy_pass http://tdt_wugk;

    }

#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。

    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$

    {

    root /data/www/wugk;

    #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力

    expires      3d;

    }

 

平滑升级

Nginx方便地帮助我们实现了平滑升级。其原理简单概括,就是:

(1)在不停掉老进程的情况下,启动新进程。

(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。

(3)新进程接受新请求。

(4)老进程处理完所有请求,关闭所有连接后,停止。

操作步骤:

1、使用nginx  -V  查看旧版本./configure的选项

2、使用旧版本的./configure的选项编译新版本,编译时不能make install

3、编译完成之后,把旧版本的可执行文件备份或者改名,

4、新版本编译完成之后,在解压包目录中objects文件夹下有新版本的可执行文件,把它复制到旧版本可执行文件的文件路径下。

5、测试新版本nginx是否正常,使用新版本的可执行文件  nginx   -t  -c   nginx的主配置文件

6、在新版本nginx的解压目录下执行命令升级:make  upgrade

7、升级之后使用:nginx  -v查看版本信息。

 

Guess you like

Origin www.cnblogs.com/linux-s/p/11288905.html