Nginx

nginx -s reload          ###重新加载配置文件
nginx -s reopen        	 ###重启
nginx -s stop            ###关闭
nginx -s quit            ###等待工作进程处理完成后关闭
nginx -T                 ###查看当前配置
nginx -t                 ###检查配置是否有问题


   

 Nginx的程序架构:
        master/worker
            一个master进程:
                负载加载和分析配置文件、管理worker进程、平滑升级
            一个或多个worker进程
                处理并响应用户请求
            缓存相关的进程:
                cache loader:载入缓存对象
                cache manager:管理缓存对象
                
        特性:异步、事件驱动和非阻塞
            并发请求处理:通过epoll/select
            文件IO:高级IO sendfile,异步,mmap
            
        nginx模块:高度模块化,但其模块早期不支持DSO机制;近期版本支持动态装载和卸载;
            模块分类:
                核心模块:core module
                标准模块:
                    HTTP modules:
                        Standard HTTP modules
                        Optional HTTP modules
                    Mail modules
                    Stream modules:
                        传输层代理
                3rd party modules


主配置文件: /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;                                ###正常运行必备的配置
worker_processes auto;                     ###优化性能相关的配置
error_log /var/log/nginx/error.log;        ###用于调试及定位问题相关的配置
pid /run/nginx.pid;                        

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
          
events {                                    ###事件驱动相关的配置
    worker_connections 1024;
}        
        
http {
    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  /var/log/nginx/access.log  main; 

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }


			主配置文件结构:
				main block:主配置段,也即全局配置段;
					event {
						...
					}:事件驱动相关的配置;
				http {
					...
				}:http/https 协议相关的配置段;
				mail {
					...
				}
				stream {
					...
				}

main配置段 

                正常运行必备的配置:
                    1、user
                        Syntax:    user user [group];
                        Default:    user nobody nobody;
                        Context:    main
                        
                        Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.
                        
                    2、pid /PATH/TO/PID_FILE;
                        指定存储nginx主进程进程号码的文件路径;
                        
                    3、include file | mask;
                        指明包含进来的其它配置文件片断;
                        
                    4、load_module file;
                        指明要装载的动态模块;
                        
                性能优化相关的配置:
                    1、worker_processes number | auto;
                        worker进程的数量;通常应该等于小于当前主机的cpu的物理核心数;
                        auto:当前主机物理CPU核心数;
                        
                    2、worker_cpu_affinity cpumask ...;
                        worker_cpu_affinity auto [cpumask];
                        
                        CPU MASK:
                            00000000:
                            00000001:0号CPU
                            00000010:1号CPU
                            ... ...
                    3、worker_priority number;
                        指定worker进程的nice值,设定worker进程优先级;[-20,20]
                        
                    4、worker_rlimit_nofile number;
                        worker进程所能够打开的文件数量上限;
                        
                调试、定位问题:
                    1、daemon on|off;    
                        是否以守护进程方式运行Nignx;
                        
                    2、master_process on|off;
                        是否以master/worker模型运行nginx;默认为on;
                        
                    3、error_log file [level];
                        
                事件驱动相关的配置:

 events {
                        ...
                    }


                    
                    1、worker_connections number;
                        每个worker进程所能够打开的最大并发连接数数量;
                        
                        worker_processes * worker_connections
                        
                    2、use method;
                        指明并发连接请求的处理方法;
                            
                            use epoll;
                            
                    3、accept_mutex on | off;
                        处理新的连接请求的方法;on意味着由各worker轮流处理新请求,Off意味着每个新请求的到达都会通知所有的worker进程;


http配置段 

http {
					... ...
					server {
						...
						server_name
						root
						location [OPERATOR] /uri/ {
							...
						}
					}
					server {
						...
					}
				}
				

                    1、server { ... }
                        配置一个虚拟主机;
                        

                        server {
                            listen address[:PORT]|PORT;
                            server_name SERVER_NAME;
                            root /PATH/TO/DOCUMENT_ROOT;                            
                        }


                    2、listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
                          listen address[:port] [default_server] [ssl] [http2 | spdy]  [backlog=number] [rcvbuf=size] [sndbuf=size]
                        
                        default_server:设定为默认虚拟主机;
                        ssl:限制仅能够通过ssl连接提供服务;
                        backlog=number:后援队列长度;
                        rcvbuf=size:接收缓冲区大小;
                        sndbuf=size:发送缓冲区大小;
                        
                    3、server_name name ...;
                        指明虚拟主机的主机名称;后可跟多个由空白字符分隔的字符串;
                            支持*通配任意长度的任意字符;server_name *.magedu.com  www.magedu.*
                            支持~起始的字符做正则表达式模式匹配;server_name ~^www\d+\.magedu\.com$
                            
                        匹配机制:
                            (1) 首先是字符串精确匹配;
                            (2) 左侧*通配符;
                            (3) 右侧*通配符;
                            (4) 正则表达式;


                定义路径的配置:                   

                    4、root path; 
                        设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径;可用的位置:http, server, location, if in location;
                        
                    5、location [ = | ~ | ~* | ^~ ] uri { ... }
                        Sets configuration depending on a request URI. 
                        
                        在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置;
                        
                        =:对URI做精确匹配;
                        ~:对URI做正则表达式模式匹配,区分字符大小写;
                        ~*:对URI做正则表达式模式匹配,不区分字符大小写;
                        ^~:对URI的左半部分做匹配检查,不区分字符大小写;
                        不带符号:匹配起始于此uri的所有的url;
                        
                        匹配优先级:=, ^~, ~/~*,不带符号;

                        
                        root /vhosts/www/htdocs/
                            http://www.magedu.com/index.html --> /vhosts/www/htdocs/index.html                          
                    6、alias path;
                        定义路径别名,文档映射的另一种机制;仅能用于location上下文;
                        
                        注意:location中使用root指令和alias指令的意义不同;
                            (a) root,给定的路径对应于location中的/uri/左侧的/;
                            (b) alias,给定的路径对应于location中的/uri/右侧的/;

                    7、index file ...;
                        默认资源;http, server, location;
                        
                    8、error_page code ... [=[response]] uri;
                        Defines the URI that will be shown for the specified errors. 


                定义客户端请求的配置

                    9、keepalive_timeout timeout [header_timeout];
                        设定保持连接的超时时长,0表示禁止长连接;默认为75s;
                        
                    10、keepalive_requests number;
                        在一次长连接上所允许请求的资源的最大数量,默认为100; 
                        
                    11、keepalive_disable none | browser ...;
                        对哪种浏览器禁用长连接;
                        
                    12、send_timeout time;
                        向客户端发送响应报文的超时时长,此处,是指两次写操作之间的间隔时长;
                        
                    13、client_body_buffer_size size;
                        用于接收客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置;
                        
                    14、client_body_temp_path path [level1 [level2 [level3]]];
                        设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量;
                        
                            16进制的数字;
                            
                            client_body_temp_path   /var/tmp/client_body  2 1 1 
                                1:表示用一位16进制数字表示一级子目录;0-f
                                2:表示用2位16进程数字表示二级子目录:00-ff
                                2:表示用2位16进程数字表示三级子目录:00-ff


                对客户端进行限制的配置: 

                    15、limit_rate rate;
                        限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;
                        
                    16、limit_except method ... { ... }
                        限制对指定的请求方法之外的其它方法的使用客户端;
                        
                        limit_except GET {
                            allow 192.168.1.0/24;
                            deny  all;
                        } 


                 文件操作优化的配置 

                   17、aio on | off | threads[=pool];
                        是否启用aio功能;
                        
                   18、directio size | off;
                        在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;
                        
                    19、open_file_cache off;
                        open_file_cache max=N [inactive=time];
                            nginx可以缓存以下三种信息:
                                (1) 文件的描述符、文件大小和最近一次的修改时间;
                                (2) 打开的目录结构;
                                (3) 没有找到的或者没有权限访问的文件的相关信息;
                            
                            max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;
                            
                            inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项;
                            
                    20、open_file_cache_valid time;
                        缓存项有效性的检查频率;默认为60s; 
                        
                    21、open_file_cache_min_uses number;
                        在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被归类为活动项;
                        
                    22、open_file_cache_errors on | off;
                        是否缓存查找时发生错误的文件一类的信息; 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金牌收租佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值