nginx 配置参数说明和实验

nginx.org

实验版本: 1.10.2

相关命令:

# nginx -t //检查配置文件格式

#nginx -s reload //重新载入配置文件

实验:

  • 主配文件大概组成

    主配置文件的设定 /etc/nginx/nginx.conf

    events{..} //事件驱动相关

    http{..} //网站服务相关

  • 全局配置段解读与实验:

    user nginx; // 运行nginx 的用户

    worker_processes auto; //worker响应进程的数量;全局配置优化点;绑定CPU不切换

    error_log /var/log/nginx/error.log; //错误日志存储路径

    pid /var/run/nginx.pid; //PID 的路径

    include /usr/share/nginx/modules/*.conf; //子配置文件的路径

    实验1.cpu 与进程绑定优化 #ab -n 100000 -c 10 http://172.18.48.61/index.html

    未绑定:

    nginx 配置参数说明和实验

    绑定后:

    nginx 配置参数说明和实验

    8核示例:

    nginx 配置参数说明和实验

    官方说明:自动绑定

    The special value auto (1.9.10) allows binding worker processes automatically to available CPUs:

    worker_processes auto;
    worker_cpu_affinity auto;

    实验二:nice值设定

    worker_priority -10;

    nginx 配置参数说明和实验

    实验三: 限定每个worker可打开的文件数// 稍稍比并发数多就可以

    Changes the limit on the largest size of a core file (RLIMIT_CORE) for worker processes. Used to increase the limit without restarting the main process.

    Syntax: worker_rlimit_nofile number;

    Default:        —

    Context:        main

  • 调试 定位问题:
    • daemon on|off //正常使用时 centos6 on; centos7 off( 7 有system 来守护)
    • nginx 配置参数说明和实验
    • error_log file [level] // 需要指明;错误日志是全局的

  • 事件驱动 events{}
    • worker_connections # //单worker 连接的数量
    • use method :// 指明并发连接的请求处理方法:
      • 示例 use epoll;
    • accept_mutex on|off //on 意为着来请求时各worker轮流处理;off 意味着随机分发

      nginx 配置参数说明和实验

  • http 服务的相关设置:
    • server{…};//具体一个虚拟主机的设置
      • 基本设置:
        • listen IP:PORT //设定监听的地址和端口

          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 // 默认虚拟主机
            • backlog=# //后续队列长度
            • rcvbuf=# //接收缓存大小
            • sndbuf=# // 发送缓存大小
        • server_name name…; //指明虚拟主机的名字
          • 精确指明: // www.zhlznana.com
          • 模0糊0
          • *匹配指明: // *.zhlznana.com
          • 正则表达式指明 // ~^www.zhlzna+.com$

            注:匹配规则机制:

            精确匹配 > *左侧匹配 > *右侧匹配 > * 正则表达式匹配

        • tcp_nodelay on|off
          • 是否开启 nodelay 选项;// 小资源是否攒一起发送
          • 只适用用keep alive
        • sendfile on|off
          • 是否开启 send file机制。//内核直接转发
      • 定义路径相关配置//指定文件夹配置

        root path;

        • 示例:
          • alias 定义时 //location 后面的字段末尾不要有/
          • nginx 配置参数说明和实验

            结果:

            nginx 配置参数说明和实验

            root定义时

            nginx 配置参数说明和实验

            nginx 配置参数说明和实验

            一个server中多个locate时://location 不包含域名部分

            nginx 配置参数说明和实验

            nginx 配置参数说明和实验

            nginx 配置参数说明和实验

            nginx 配置参数说明和实验

        多个location示例:

        location / {

        root /data/www;

        index index.html;

        }

        location ~* .(jpg|png|jpeg|gif)$ {

        root /data/images/;

        }

        错误页面重定向: 也可以重定向到某个location /xxxx

        • 基本模板:error_page code … [=[response]] uri;
        • 示例:
          • nginx 配置参数说明和实验

          • nginx 配置参数说明和实验
          • 重定向到
          • nginx 配置参数说明和实验

        try_files: 按次序检查文件

      • 客户端的响应操作的配置
        • keepalive_timeout timeout [header_timeout];

          设定保持连接的超时时长,0表示禁止长连接;默认为75s;75太长减小

          • 示例: //实验为5秒后自动断开

            nginx 配置参数说明和实验

        • keepalive_requests number; //与上面的时间 任何一个满足即断开
          • 示例:
            • nginx 配置参数说明和实验
            • 请求两次即关闭
            • nginx 配置参数说明和实验
        • keepalive_disable none | browser …; //这个选项很少适用了

        • send_timeout time; //发送响应报文超时后断开时间

          向客户端发送响应报文的超时时长,此处,是指两次写操作之间的间隔时长;

        • client_body_buffer_size size; // 如果客户端提交的报文大与设定值

          用于接收客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置;

        • client_body_temp_path path [level1 [level2 [level3]]]; //与上一条配合适用
          • 存储机制为缓存机制;

            示例: client_body_temp_path /var/tmp/client_body 1 2 2;

            意义:缓存路径为 /var/tmp/client_body 16进制基于md5校验码生成

            1:一位16进制表示1级子目录

            2:两位16进制表示二级子目录

            2:两位16进制表示三级子目录

        • limit_rate rate; //限制用户速率;例如百度云盘

          限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;

        • limit_except method … { … } //对某IP限定某请求方法

          limit_except GET {

          allow 192.168.1.0/32;

          deny all;

          }

          注: 这里限定是除了GET 和 HEAD 之外的method

      • 服务器端文件的读取优化配置:
        • aio on | off | threads[=pool]; //默认开启的

          是否启用aio功能

        • directio size | off;

          在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;

        • 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 所限制的次数即为非活动项

        • open_file_cache_valid time;// 检查频率

          缓存项有效性的检查频率;默认为60s;

        • open_file_cache_min_uses number; //界定访问低于多少次

          在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被归类为活动项

        • open_file_cache_errors on | off; //

          是否缓存查找时发生错误的文件一类的信息;

      • 模块的具体控制:

        ngx_http_access_module模块:基于IP访问控制功能

        allow address | CIDR | unix: | all;

        deny address | CIDR | unix: | all;

        示例:

        nginx 配置参数说明和实验

        /bbs 这个location 允许172.18.0.0/16 段的访问,但200这个地址不能访问;

        其他的进制访问

        实验:

        nginx 配置参数说明和实验

        本机电脑ip:为148.68

        nginx 配置参数说明和实验

        148.99 这个虚拟机可以访问:

        nginx 配置参数说明和实验

        取消限制:

        nginx 配置参数说明和实验

        访问可OK了

        nginx 配置参数说明和实验

        • ngx_http_auth_basic_module模块 //基于用户访问控制,使用basic机制进行用户认证

          auth_basic string | off; //显示提示内容

          auth_basic_user_file file; //密码保存的文件路径

          注:需要安装httpd-tools

          # htpasswd :命令

          配置文件:

          nginx 配置参数说明和实验

          • 创建用户密码文件:

            # htpasswd -c -m /app/.nginxpasswd zhlz //-c 第一次创建时使用;文件名与配置文件中的指向一致

            访问测试:

            nginx 配置参数说明和实验

        • ngx_http_stub_status_module模块// 用于查看当前网站情况;

          配置示例:

          location /basic_status {

          stub_status;

          }

          nginx 配置参数说明和实验

          测试连接:

          nginx 配置参数说明和实验

          各参数说明:

          Active connections: 活动状态的连接数;

          accepts:已经接受的客户端请求的总数;

          handled:已经处理完成的客户端请求的总数;

          requests:客户端发来的总的请求数;

          Reading:处于读取客户端请求报文首部的连接的连接数;

          Writing:处于向客户端发送响应报文过程中的连接数;

          Waiting:处于等待客户端发出请求的空闲连接数;

        • log_format name string …; //定义日志格式:

          acces_log /app/zhlz/access_log nana;

          访问日志文件路径,格式及相关的缓冲的配置;

          buffer=size //缓冲日志条目数量

          flush=time

          nginx 相关变量说明: 点我点我

          相关配置文件//注:日志格式一定设定在http 下

          nginx 配置参数说明和实验

          nginx 配置参数说明和实验

          相关文件夹设定nginx的权限:

          setfacl -R -m u:nginx:rwx zhlz

          测试:

          nginx 配置参数说明和实验

        • open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

          open_log_file_cache off; //设置日志缓存文件打开的数量

          注:这里缓存的时日志文件的指向

          max:缓存的最大文件描述符数量;

          min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项;

          inactive:非活动时长;

          valid:验正缓存中各缓存项是否为活动项的时间间隔;

  • ngx_http_gzip_module // nginx gzip压缩模块;节约带宽

    常用指令: 官方指南

gzip on|off

http,server,location,if in location

gzip_buffers number size

http, server, location

gzip_proxied

http,server,location

gzip_min_length length

http,server,location

nginx 配置参数说明和实验

nginx 配置参数说明和实验

nginx 配置参数说明和实验

:nginx 做代理服务器时的设置// any 表示只要符合本代理设置的压缩type 则后端发来的任何信息均压缩

实验步骤:

1.设定配置文件

gzip on;

2.测试连接:

未开启压缩时:

nginx 配置参数说明和实验

开启压缩时:

nginx 配置参数说明和实验

设置最小压缩长度:

gzip_min_length 64;:

编辑资源文件:

#echo asdfsafsafsadfsdafsdfsdafsadfdsa > /app/zhlz/message.html

连接结果://没有被压缩

nginx 配置参数说明和实验

其他配置:

nginx 配置参数说明和实验

  • ngx_http_ssl_module // 加密模块

ssl on|off

ssl 的开关

http,server

ssl_certificate file

证书的路径

http, server

ssl_certificate_key file

私钥地址

http, server

ssl_session_cache off | none | [builtin[:size]] [shared:name:size];

会话是否缓存

http server

builtin:worker私有会话
shared:worker共享会话

实验:

生成证书过程略…

修改配置文件://注:listen 端口的修改

nginx 配置参数说明和实验

链接实验:

nginx 配置参数说明和实验

nginx 配置参数说明和实验

  • ngx_http_rewrite_module //旧资源变更过需要重写请求URL

    官方指南 点它

rewrite regex replacement [flag]

重写请求URI
基于正则表达式

server,location,if

rewrite_log on|off

是否开启日志

http,server,location,if

//写入错误日志

flag:

last:替换完成后重新检查location (默认)

break:跳出可能出现的死循环

redirect:返回客户端新的URL (302)

nginx 配置参数说明和实验

permanent

实验示例:

1.编写配置文件://最好定义在server下,因为break以后要从跳出点向下搜寻location

nginx 配置参数说明和实验

2.测试链接:

nginx 配置参数说明和实验

3.测试用curl 命令实验//全部被重定向未2.html 里的值

nginx 配置参数说明和实验

redirect的测试://设置404错误的重定向

配置文件:

nginx 配置参数说明和实验

链接测试:

nginx 配置参数说明和实验

定位到:

nginx 配置参数说明和实验

查看报文信息://前三个的定向

nginx 配置参数说明和实验

  • ngx_http_referer_module //nginx 跳转过来的url识别;防盗链

valid_referers none |blocked|server_names|string…

server,location

可以接受的来源

Specifies the “Referer” request header field values that will cause the embedded $invalid_referer variable to be set to an empty string. Otherwise, the variable will be set to “1“. Search for a match is case-insensitive.

$invalid_referer: 如果来源符合要求,则这个内嵌变量则为0 或者空串,否则则被设定为1

none: 直接键入的URL 非跳转过来的

blocked: 经过网关或代理处理过的,无法识别的

server_names :指定域名,可以通配符*

~ 可以正则表达式:

实验示例:

示例:valid_referers none blocked server_names *.zhlzna.com ~.baidu. ~.google.;

配置文件:

valid_referers none blocked server_names *.zhlzna.com;

if ($invalid_referer){

return 302 http://www.zhlzna.com; //这里是重定向跳转的页面

}

测试链接://利用curl 可以指定跳转来源的选项来测验:

#curl -e http://www.baidu.com http://www.zhlzna.com

nginx 配置参数说明和实验

  • ngx_http_proxy_module //代理相关

    官方说明: 点我

proxy_pass URL;

指向被代理服务器

location if in location limit_except

基本写法

proxy_set_header field value

修改代理发出的请求头部信息

依赖于扩展头部的自定义

如果不设定,则默认使用:

proxy_set_header Host $proxy_host;
proxy_set_header Connection close;

实验1:proxy_pass 的用法;

注意代理与被代理服务器的网段号:

这个语句不能在server和http中使用

配置文件://只要是CURL 来访问的就跳转

nginx 配置参数说明和实验

链接实验:

nginx 配置参数说明和实验

nginx 配置参数说明和实验

注意

nginx 配置参数说明和实验

配置示例

nginx 配置参数说明和实验

链接测验

nginx 配置参数说明和实验

实验2:

代理指向域名的示例:

nginx 配置参数说明和实验

链接测试

nginx 配置参数说明和实验

实验3: nginx +nginx 组合 客户端真实IP的传递

参考网络文章: 点我

代理服务器配置文件: //传递各种参数

50 location ~* .jpg$ {

52

53 proxy_pass http://192.168.0.3;

54 proxy_set_header X-Real-Ip $remote_addr;

55 proxy_set_header X-Forwarded-For $remote_addr;

56 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

57 proxy_set_header Host $host;

58 proxy_redirect off;

59

61

62 }

服务器端配置文件:

23 set_real_ip_from 192.168.0.0/16;

24 real_ip_header X-Real-Ip;

日志格式文件

‘$remote_addr //这个参数就会自动变为代理服务器传递过来的真实IP

链接示例的日志文件://这里的IP 已经改为真正客户端的IP

nginx 配置参数说明和实验

代理服务器的缓存设置//基本常用的

proxy_cache zone |off

开关缓存 zone为缓存设定的名字

http, server, location

proxy_cache_key string

设定缓存的键值

http server location

proxy_cache_path path [levels=levels] [use_temp_path=on|off]keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

设定缓存的存储路径;

缓存内存的映射地址:大小

http

proxy_cache_valid [code …] time

设定各种状态响应码的缓存时间

http server location

缓存实验:

代理端设置:

59 proxy_cache zhlzcache;

60 proxy_cache_key $request_uri;

61 proxy_cache_valid 200 301 302 10m;

62 proxy_cache_valid any 1m;

代理端的全局配置:

proxy_cache_path /app/nginxcache levels=2:2:1 keys_zone=zhlzcache:10m; //需要在http 段设置

压力测试:

1.没有缓存时

nginx 配置参数说明和实验

2.有缓存时:

nginx 配置参数说明和实验

被代理端有问题时代理服务器如何处理

proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 |http_503 | http_504 | http_403 | http_404 | http_429 | off …;

定义哪些错误时启用缓存响应;off都不用

http,server, location

定义哪些错误时启用缓存响应;默认为 off 都不用

实验:

配置:

nginx 配置参数说明和实验

测试:先访问

nginx 配置参数说明和实验

将服务器文件改名

nginx 配置参数说明和实验

再次刷新服务器://出来啦

nginx 配置参数说明和实验

服务器端响应报文的首部信息处理:

proxy_hide_header field

http, server, location

额外指定隐藏某些首部

注:By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-…” from the response of a proxied server to a client.

设定代理与服务器之间的链接时间:

proxy_connect_timeout time;

http, server, location

注:默认为60S ,但不能超过75秒

基于域名的转发实验:

在服务器端添加虚拟主机:

1 server {

2 listen 80;

3 server_name www.yaoxiaona.com;

4 sendfile on;

5 root /app/yaoxiaona;

6 }

在代理服务器端设置响应的虚拟主机名: //摘要

server {

6 listen 80;

7 server_name www.yaoxiaona.com;

proxy_pass http://www.yaoxiaona.com;

链接测试:

nginx 配置参数说明和实验

nginx 配置参数说明和实验

首部专门处理模块

首部的X-Via 参考确认

nginx 配置参数说明和实验

实验: 编辑首部// 添加代理IP:

修改配置:

add_header X-Proxy $server_addr;

测试链接:

未填加代理IP时:

nginx 配置参数说明和实验

填加代理IP时:

nginx 配置参数说明和实验

实验:创建NP 组合://这里以本机组合示例

CENTOS7 为例: yum install php-fpm

编辑配置文件:

#vim /etc/php-fpm.d/www.conf

listen = 0.0.0.0:9000 //修改监听IP端口;监听所有9000端口

listen.allowed_clients = 127.0.0.1 //允许主机链接设置

39 user = nginx //与什么类型的服务器绑定就指定什么名字 ;apache

40 ; RPM: Keep a group allowed to write in log dir.

41 group = nginx

;pm.max_requests = 500 //每个子进程处理多少个请求

121 pm.status_path = /pm_status //记录该PHP状态

133 ping.path = /pm_ping //探测接口

138 ping.response = pong //回应探测

下面的需要注意后期可能用到

nginx 配置参数说明和实验

启动服务

# systemctl start php-fpm.service

创建目录:

#mkdir /app/php-fpm/shop -pv

创建测试文件

#vim index.php

配置代理端:的相关配置文件

50 location ~ .php$ {

51 root html;

52 fastcgi_pass 192.168.0.4:9000;

53 fastcgi_index index.php;

54 fastcgi_param SCRIPT_FILENAME /app/php-fpm/shop/$fastcgi_script_name;

55 include fastcgi_params;

56 }

重启nginx 服务并测试链接:

nginx 配置参数说明和实验

测试在服

1 <?php

2 #phpinfo();

3 $conn = mysql_connect(‘127.0.0.1′,’myuser’,’mypass’);

4 if ($conn)

5 echo “OK”;

6 else

7 echo “lala”;

8 ?>

测试链接;

nginx 配置参数说明和实验

配置查看状态和ping 的配置文件:

location ~* ^/(pm_status|pm_ping)$ { //注意跟PHP服务配置文件的名字一致

30 include fastcgi_params;

31 fastcgi_pass 192.168.0.4:9000;

32 fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

33

34 }

测验链接:

nginx 配置参数说明和实验

nginx 配置参数说明和实验

fastcgi_cache //配置类似于静态资源缓存

fastcgi 的缓存 //类似与之前的缓存 ; 都需要压力测试

示例:

nginx 配置参数说明和实验

压测截图:

nginx 配置参数说明和实验

提升性能,保持连接

nginx 配置参数说明和实验

备注

请求首部变量:

Client-Ip

客户端首部

Host

接收请求的主机名+端口

From

客户EMAIL

Referer

当前URI的URL

UA-OS

客户端的OS信息

User-Agent

客户的请求程序

响应首部变量

Age

响应持续时间

Retry-After

资源不可用,下次重试

Server

服务器名称 版本

Title

对于HTML文档,即标题

Warning

警告报文

原创文章,作者:zhoulz,如若转载,请注明出处:http://www.178linux.com/75327

(0)
上一篇 2017-05-12 20:44
下一篇 2017-05-13 17:48

相关推荐

  • N_28包管理器(rpm)及前端管理工具(yum)

    1、简述rpm与yum命令的常见选项,并举例 rpm–>RPM package manager 是一种用于redhat发行版的打包及安装管理工具,现在成为linux领域包管理器的行业标准,包名以.rpm为后缀。 用法: rpm [options] PACKAGE_FILE 常用选项: -i:安装rmp包 -v:显示安装过程 -vv:更加详细…

    Linux干货 2018-01-01
  • 【推荐】Linux 简单部署LAMP

    Linux 简单部署LAMP: 实验环境: 1、主机系统Centos6.7_X86_64 2、DNS&CA:192.168.3.10 3、LAMP安装方式为rpm 4、主机IP:192.168.3.11提供web站点:     http://wp.neolinux.com     &nbsp…

    Linux干货 2016-06-22
  • 二.Linux博客-2016年7月21日screen、echo

    格式说明: 操作 概念 命令 说明及举例 二.screen、echo internet 因特网ethernet 以太网ctrl+alt+F1 图形界面ctrl+alt+F2-F6 字符界面 命令:who am i/who/who me i/tty/df/ifconfig/w/ halt 关机 reboot&n…

    Linux干货 2016-08-24
  • 马哥教育网络班21期+第2周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     cp复制,mv移动,rm删除     1、cp:             1,用法: cp [OPTIO…

    Linux干货 2016-07-12
  • Linux第四周学习博客作业

    对第四周学习的内容进行总结

    Linux干货 2017-12-23
  • Net-25第5周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; grep -E "^[[:space:]]+" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; grep&nbsp…

    Linux干货 2017-01-03

评论列表(2条)