我爱编程

第二讲 Nginx模块详解

2018-04-16  本文已影响0人  农民工进城

本章要点

2.1 Nginx配置文件结构

Nginx的配置文件nginx.conf位于其安装目录的conf目录下。
nginx.conf由多个块组成,最外面的块是main,main包含Events和HTTP,HTTP包含upstream和多个Server,Server又包含多个location,如图:


nginxconf.png

main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和 location(URL匹配特定位置的设置)。
● main块设置的指令将影响其他所有设置;
● server块的指令主要用于指定主机和端口;
● upstream指令主要用于负载均衡,设置一系列的后端服务器;
● location块用于匹配网页位置。
这四者之间的关系式:server继承main,location继承server,upstream既不会继承其他设置也不会被继承。
在这四个部分当中,每个部分都包含若干指令,这些指令主要包含Nginx的主模块指令、事件模块指令、HTTP核心模块指令,同时每个部分还可以使用其他HTTP模块指令,例如Http SSL模块、HttpGzip Static模块和Http Addition模块等。
上面提到过nginx配置文件中主要包括六块:main,events,http,server,location,upstream
main块:主要控制nginx子进程的所属用户/用户组、派生子进程数、错误日志位置/级别、pid位置、子进程优先级、进程对应cpu、进程能够打开的文件描述符数目等
events块:控制nginx处理连接的方式
http块:是nginx处理http请求的主要配置模块,大多数配置都在这里面进行
server块:是nginx中主机的配置块,可以配置多个虚拟主机
location块:是server中对应的目录级别的控制块,可以有多个
upstream块:是nginx做反向代理和负载均衡的配置块,可以有多个

2.2 各个模块详解

下面复制一下最原始配置文件ngin.conf中的内容:

#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 {
    worker_connections  1024;
}


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;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            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;
    #    }
    #}


    # 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;
    #    }
    #}

}

2.2.1 main 模块

main.png

2.2.2 events模块

events.png

2.2.3 http模块

http.png

2.2.4 server模块

server.png

2.2.5 upatream 模块
upstream 比较简单,用于处理服务器的负载均衡,实例如下:
先配置upstream 模块

upstream zhaosc { 
      server 192.168.1.100:8080; 
      server 192.168.1.200:8080; 
}

然后,配置location,将 proxy_pass配置为:http:// + upstream名称

location / { 
            root  html; 
            index  index.html index.htm; 
            proxy_pass http://zhaosc; 
}

nginx 负载均衡的算法是轮询,同时还支持权重(weight)机制,如下:

upstream zhaosc{ 
      server 192.168.1.100:8080  weight=5; 
      server 192.168.1.200:8080  weight=10; 
}

ip_hash如下:

upstream zhaosc{ 
      ip_hash; 
      server 192.168.1.100:8080; 
      server 192.168.1.200:8080; 
}

fair: 按后端服务器的响应时间来分配请求。响应时间短的优先分配。
与weight分配策略相似。

 upstream zhaosc{      
      server 192.168.1.100:8080; 
      server 192.168.1.200:8080; 
      fair; 
}

url_hash:按訪问url的hash结果来分配请求,使每一个url定向到同一个后端服务器。后端服务器为缓存时比較有效。
注意:在upstream中加入hash语句。server语句中不能写入weight等其他的參数,hash_method是使用的hash算法。

 upstream zhaosc{ 
      server 192.168.1.100:8080; 
      server 192.168.1.200:8080;  
      hash $request_uri; 
      hash_method crc32; 
}

upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:

upstream zhaosc{ #定义负载均衡设备的Ip及设备状态 
      ip_hash; 
      server 192.168.1.100:9090 down; 
      server 192.168.1.100:8080 weight=2; 
      server 192.168.1.100:6060; 
      server 192.168.1.100:7070 backup; 
}

由于location模块内容比较多,所以重开一篇,下一节详细介绍。

上一篇 下一篇

猜你喜欢

热点阅读