nginx学习笔记

一个主线程和多个工作进程;
主进程负责读取验证配置文件以及管理维护工作进程;
工作进程采用事件机制高效的负责具体的请求处理,工作进程的数量由配置文件配置或者自适应于服务器的cpu核心数量(worker_process);
nginx及其模块的工作方式由配置文件决定,通常命名为nginx.conf,路径一般为usr/local/nginx/conf,etc/nginx,user/local/etc/nginx

启动、停止、重启nginx
运行可执行文件可启动nginx,一单启动后可执行带-s参数的命令对nginx进行管理如:
语法:nginx -s params
stop:停止nginx
quit:优雅的停止nginx,工作进程会在完成当前服务的请求后退出;
reload:重新加载配置文件,配置文件改变后不会立即生效,需要运行带该参数的命令后才生效,nginx会加载配置文件,然后验证配置文件并尝试使用新的配置,若成功则启动新的工作进程并发送信号给旧的工作进程让其退出,若不成功则回滚配置。旧的工作进程接受到信号后,会停止接受新的请求,并在服务完当前请求后退出;
reopen:重开日志文件;
管理nginx除了nginx命令外,还可以使用unix系统自带的命令,如kill -s quit pid;

配置文件结构
nginx的模块由配置文件中的指令组成,指令分为简单指令和复杂指令。简单指令由名称和参数构成,其间用空白字符隔开,使用分号结尾。复杂指令与简单指令类似,只是分号换为花括号,花括号内部可以有其他指令,称之为环境上下文。
在所有环境上下文之外的指令称为主环境上下文,如events、http。
在#之后的信息为注释。

http配置

http {

    proxy_cache_path filesystem_path key_zone=zoneName:size(k|m) ;

    resolver ip valid=time ipv6=[on|off] ;
    resolver_timeout time ;

    upstream backend {
        round_robin(default and it's not a directive) ;
        least_conn ;
        ip_hash ;
        hash [str|variable or combination] ;
        least_time ;

        # the directive is mandatory for health_checks and on-the-fly reconfiguration of the server group 
        zone zoneName size(k|m) ;

        state /var/lib/nginx/state/upstream_conf_file ;

        server domain|host [backup] [weigth=int(default is 1)] [slow_start=count(s)] [max_conns=count] [fail_timeout=30s(10s is assumed)] [max_fails=3(1 is assumed)] [resolve];
        ...

        queue count timeout=int ;

        sticky cookie cookie_name [expires=count(s|m|h)] [domain=str] [path=str] ;
        sticky route $route_cookie $route_uri ;
        sticky learn create= lookup= zone= timeout=
    }

    match matchName {
        status responseCode ;

        header str or regex ;
        ...

        body str or regex ;
    }

    server {
        # 代理服务监听的端口
        listen host:port default_server(不写 则第一个server为默认的server,当header中的servername匹配不到时,使用默认的server);

        proxy_cache zoneName ;
        proxy_cache_key $host$request_uri ;
        proxy_cache_min_users count ;
        proxy_cache_methods GET HEAD POST ;
        proxy_cache_valid [responseCode...|any] time(m minutes) ;
        proxy_cache_bypass $uri$cookie_nocache ;
        proxy_no_cache (defining parameter in the same way as for the proxy_cache_bypass directive) ;

        # 当有多个server匹配到一个request时 nginx使用header中的servername
        # 可以是全名、可以带星号的通配符、以~开头的正则表达式
        # If several names match the Host header, NGINX Plus selects one by searching for names in the following order and using the first match it finds:
        # Exact name
        # Longest wildcard starting with an asterisk, such as *.example.org
        # Longest wildcard ending with an asterisk, such as mail.*
        # First matching regular expression (in order of appearance in the configuration file)
        server_name [fullname|wildcard|regex] ;

        # i don't know what value about this.
        root path ;

        return responseCode text ;

        # Test the URI against all prefix strings.
        # The = (equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.
        # If the ^~ (caret-tilde) modifier prepends the longest matching prefix string, the regular expressions are not checked.
        # Store the longest matching prefix string.
        # Test the URI against regular expressions.
        # Break on the first matching regular expression and use the corresponding location.
        # If no regular expression matches, use the location corresponding to the stored prefix string.
        location [pathPrefix or regex(should be preceded with ~(case-sensitive) or ~×(case-insensitive)) {
            root [local file system path] ;

            proxy_pass [porxy http server url(like http://localhost:8080/)] upstreamName;
            proxy_set_header headerName headerValue ;
            proxy_buffering [on|off] ;
            proxy_buffers size number ;
            proxy_buffer_size number(k|m) ;
            proxy_bind ipAddress ;

            health_check interval=count fails=count passes=count match=matchName uri=/path;

            gzip [on|off] ;
            gzip_types text/html text/plain ... ;
            gzip_min_length number (20 is assumed) ;
            gzip_proxied no-cache no-store private... ;
            gzip_static [on|off] ;
            gunzip [on|off] ;

            rewrite regex_match regex_replace [last|break] ;

            error_page responseCode [error_page_url] ;
            error_page 404=301 [new_page_url] ;

            fastcgi_pass localhost:9000 ;
            fastcgi_param SCRIPT_FILENAME [] ;
            fastcgi_param QUERY_STRING    $query_string;

            uwsgi_pass uri ;
            scgi_pass uri ;
            memcached_pass uri ;

            open_file_cache_errors [on|off] ;

            index [index.htm...] ;

            try_files $uri ... last_param ;

            sendfile [on|off] ;
            sendfile_max_chunk size(1m) ;

            tcp_nopush [on|off] ;
            tcp_nodelay [on|off] ;

            keepalive_timeout number(ms) ;

            upstream_conf ;
            allow ip ;
            deny all|ip ;

            return responseCode text ;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值