Nginx 学习笔记1--在web服务中的使用配置

2018-08-11  本文已影响0人  Net夜风
I/O模型:

阻塞型、非阻塞型、复用型、信号驱动型、异步

Nginx的程序架构:

nginx的安装配置:

Nginx(2)

配置指令:

一. 正常运行必备的配置:
  1. user:定义工作进程使用的用户和组,如果省略组,则使用名称等于用户名的组。
  1. pid /PATH/TO/PID_FILE:指定存储nginx主进程进程号码的文件路径。
  2. include file | mask :指明包含进来的其他配置文件片段。
  3. load_module file:指明要装载的动态模块。
二. 性能优化相关的配置:
  1. worker_processes number | auto;
  1. worker_cpu_affinity cpumask ...
  1. worker_priority number: 指定worker进程的nice值,设定worker进程的优先级;
  1. worker_rlimit_nofile number: worker进程能够打开的文件数量上限;
示例:
    [root@localhost ]# cd /etc/nginx/conf.d
    [root@localhost conf.d]# vim ../nginx.conf
        #在main配置文件中添加如下内容
        worker_cpu_affinity auto;
        worker_priority -5;
        worker_rlimit_nofile 65536;
    [root@localhost conf.d]# nginx -s reload
    [root@localhost conf.d]# watch 'ps axo comm,pid,psr | grep nginx'
        Every 0.5s: ps axo comm,pid,psr |grep nginx                                                     Sun Aug  5 21:04:58 2018

        nginx             5007   0
        nginx            10423   0
        nginx            10424   1
        nginx            10425   2
        nginx            10426   3
    #使用ab压测www.ilinux.io/index.html
    [root@localhost ~]# ab -n 100000 -c 100 http://192.168.43.125/index.html
    [root@localhost conf.d]# ps axo comm,pid,psr,ni | grep nginx
        nginx             5007   0   0
        nginx            10423   0  -5
        nginx            10424   1  -5
        nginx            10425   2  -5
        nginx            10426   3  -5
三.调试、定位问题:
  1. daemon on | off:是否以守护进程方式运行Nginx;
  2. master_process on | off: 是否以master/worker模型运行nginx; 默认为on;
  3. error_log file [level];定义错误日志路径与级别;
四.事件驱动相关的配置:
event {
    ...
}
  1. worker_connections number:每个worker进程所能打开的最大并发连接数量;
  1. use method:指明并发连接请求的处理方法
  1. accept——mutex on | off:处理新的连接请求的方法

http协议相关的配置

一. 与套接字相关的配置
  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] ;

  1. server_name name ...; 指明虚拟主机的名称,后可根多个由空白字符分割的字符串;
  1. tcp_nodelay on | off;
  1. tcp_nopush on | off
  1. sendfile on | off;
二. 定义路径相关的配置:
  1. root path
  1. location [ = | ~ | ~* | ^~ ] uri { ... }
sky.png

...

  1. alias path;
dice.png

...

  1. index file ...;
  1. error_page code ... [=[response]] uri;

使用另外的主机随便访问不存在的页面测试如下图

shaX.png
三. 定义客户端请求的相关配置
  1. keepalive_timeout timeout [header_timeout];
  1. keepalive_requests number;
  1. keepalive_disable none | browser ...;
  1. send_timeout time;
  1. client_body_temp_path path [level1 [level2 [level3]]];
四. 对客户端进行限制的相关配置:
  1. limit_rate rate;
  1. limit_except method ... {...}
五. 文件操作优化的配置
  1. aio on | off |threads[=pool];
  1. directio size | off;
  1. open_file_cache off;
    open_file_cache max=N [inactive=time];
  1. open_file_cache_valid time;
  1. open_file_cache_min_uses number;
  1. open_file_cache_errors on|off;
六. ngx_http_access_module模块:实现基于ip的访问控制功能
  1. allow address | CIDR | unix:| all;
  2. deny address | CIDR | unix | all;
七. ngx_http_auth_basic_module模块:实现基于用户的访问控制,使用basic机制进行用户认证;
  1. auth_basic string | off;
  1. auth_basic_user_file file;

使用另外主机测试访问:


auth_basic.png
八. ngx_http_stub_status_module模块:用于输出nginx的基本状态信息
  1. stub_status;

    配置示例:
         [root@localhost ~]# vim /etc/nginx/conf.d/vhost1.conf 
             location ~* ^/(admin|login) {
                auth_basic "admin area or login url";
                auth_basic_user_file /etc/nginx/.ngxpasswd;
                stub_status;
            }
         [root@localhost ~]# nginx -s reload
    

访问测试:


status.png
九. ngx_http_log_module模块:用于以指定的格式写入请求日志
  1. log_format name string ...;
  1. access_log path [format [buffer=size] [gzip=[level1]] [flush=time][if=condition]];
    access_log off;
  1. open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
    open_log_file_cache off;
十. ngx_http_gzip_module:用gzip格式压缩响应;
  1. gzip on | off;
  1. gzip_comp_level level;
  1. gzip_disable regex ...;
  1. gzip_min_length length;
  1. gzip_buffers number size;
  1. gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
  1. gzip_types mime-type ...;
十一. ngx_http_ssl_module模块:
  1. ssl on | off;
  1. ssl_certificate file;
  1. ssl_certificate_key file;
  1. ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1][TLSv1.2]
  1. ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
  1. ssl_session_timeout time;

使用外部主机访问测试:


ssl1.png 丧失了.png
十二. ngx_http_rewrite_module模块:

-将用户请求的URI基于regex所描述的模式进行检查,然后完成替换;

  1. rewrite regex replacement [flag]
    将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI;
  1. return
  1. rewrite_log on |off;
  1. if (condition) {...}
  1. set $variable value

访问测试.png能否重写为.jpg

day.png
十三. ngx_http_referer_module模块:
  1. valid_referers none | blocked | server_names | string ...;
    • 定义referer首部的合法引用用值;

                none:请求报文首部没有referer首部;
                blocked:请求报文的referer首部没有值;
                server_names:参数,其可以有值作为主机名或主机名模式;
                    arbitrary_string:直接字符串,但可使用*作通配符;
                    regular expression:被指定的正则表达式模式匹配到的字符串;要使用~打头,例如 ~.*\.magedu\.com;
                    $invalid_referer : 模块内置变量,非法引用,只要没被valid_referers定义匹配到的就是非法引用
      

...

    配置示例:
                valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;
                
                if($invalid_referer) {
                    return http://www.magedu.com/invalid.jpg;
                }
上一篇 下一篇

猜你喜欢

热点阅读