• 2-1 什么是Nginx
  • 2-2 常见的中间件服务
  • 2-3 Nginx的特性_实现优点1
  • 2-4 Nginx特性_实现优点2
  • 2-5 Nginx特性_实现优点3
  • 2-6 Nginx特性_实现优点4
  • 2-7Nginx的快速安装
  • 2-8Nginx的目录和配置语法_Nginx 安装目录
  • 2-9Nginx的目录和配置语法_Nginx编译配置参数;
  • 2-10 Nginx的目录和配置语法_默认语法规则
  • 2-11 Nginx的目录和配置语法_默认配置与默认站点启动
  • 2-12 HTTP请求;
  • 2-13 Nginx日志_Log_format1;
  • 2-14Nginx日志_Log_format2;
  • 2-15 Nginx模块讲解;
  • 2-16 Nginx的模块讲解 _sub_status
  • 2-17 Nginx模块讲解_random_index
  • 2-18 Nginx模块讲解_sub_module
  • 2-19 Nginx模块讲解_sub_module配置演示;
  • 2-20 Nginx的请求限制_配置语法与原理1
  • 2-21 Nginx的请求限制_配置语法与原理2;
  • 2-22 Nginx的请求限制_配置语法与原理3;
  • 2-23 Nginx的访问控制_介绍访问控制的基本方式;
  • 2-24 Nginx的访问控制-access_module配置语法介绍;
  • 2-25 Nginx的访问控制-access_module配置
  • 2-26 Nginx的访问控制-access_module局限性;
  • 2-27 Nginx的访问控制-auth_basic_module配置;
  • 2-28 Nginx的访问控制-auth_basic_module局限性;

2-1 什么是Nginx

1、Nginx是一个开源且高性能、可靠的HTTP中间件、代理服务;(例如淘宝的Tengine基于Nginx的源代码二次开发修改的。)

2、Nginx的中间件架构;

  • 中间件可以有效地避免应用与操作系统的直接调用,使网站更有层次性;

Nginx的官方网站:http://nginx.org/

Nginx的下载地址:http://nginx.org/en/download.html

2-2 常见的中间件服务

1、常见的HTTP服务

  • Apache-HTTPD-Apache基金会;
  • IIS-Microsoft;
  • GWS-Google Web Server(谷歌自用,据说性能也十分优越,例如JDWS,京东的WebServer);

权威网站netcraft:https://www.netcraft.com/

Netcraft公司于1994年底在英国成立,多年来一直致力于互联网市场以及在线安全方面的咨询服务,其中在国际上最具影响力的当属其针对网站服务器,域名解析/主机提供商,以及SSL市场所做的客观严谨的分析研究。

公司官网每月公布的调研数据(Web Server Survey)已成为当今人们了解全球网站数量以及服务器市场分额情况的主要参考依据,时常被诸如华尔街杂志,英国BBC,Slashdot等媒体报道或引用。

2-3 Nginx的特性_实现优点1

1、为什么选择Nginx?

1)原因一:IO多路复用epoll;

多线程处理方式,主动上报机制,效率高!

多个描述符的I/O操作都能在一个线程内并发交替地顺序完成,这就叫I/O多路复用,这里的“复用”是指复用同一个线程;

IO多路复用的实现方式有select、poll、epoll;

2)IO多路复用的模型;

  • select模型;
  • 1)能够监视文件描述符的数量存在最大限制1024

    2)线性扫描效率低下

  • epoll模型;

    1)每当FD就绪,效率更高;

    2)最大连接无限制;

3)可以使用——结合餐厅用餐结束后,结账的场景理解;

  • 用户呼叫老板进行结账,会出现排队现象;
  • 用户主动告诉服务员,我们要结账,服务员带领,效率要高;

2-4 Nginx特性_实现优点2

1、轻量级;

  • 功能模块化(充分考虑性能,减少不必要的功能);
  • 代码模块化;

2、举例:阿里开源的Tengine,就是基于Nginx1.6二次开发的;

Tengine官网:http://tengine.taobao.org/

Tengine下载地址:http://tengine.taobao.org/download.html

  • Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
  • 从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝搜狗等互联网企业。Tengine是社区合作的成果,我们欢迎大家参与其中,贡献自己的力量。

特性

2-5 Nginx特性_实现优点3

1、CPU亲和(Affinity与什么是CPU亲和?

  • 多核心服务器,应用在密集计算型场景;
  • 多个Nginx的work进程绑定CPU核心进行计算,减少性能损耗;

是一种把CPU核心和Nginx工作进程绑定的方式,把每个Worker进程固定在一个CPU上执行,

减少切换CPU的cache miss,获得较好的性能;

2-6 Nginx特性_实现优点4

1、nginx的sendfile机制;

1)Nginx在处理静态文件是非常有优势的(sendfile机制);

2)请求文件时候,要经过操作系统的“内核空间”和“用户空间”,最终到达socket然后传递相应给用户;对于操作系统而言,要经过“内核空间”和“用户空间”,即发生多次的切换操作;我们知道,静态文件不需要经过复杂的逻辑处理,可直接通过“内核空间”传输处理;

3)sendfile正是利用了以上的机制,在Linux2.2内核之后出来的“零拷贝”的传输模式,即将文件只通过“内核空间”进行传递给socket,响应给用户,避免了频繁切换,效率十分高;例如在CDN服务中,处理静态文件效率非常高;

2-7Nginx的快速安装

1、Nginx快速搭建与基本参数使用;

  • Mainline version-开发版;测试、研究学习,企业生产环境不建议使用!
  • Stable Version-稳定版;企业生产环境可以使用;
  • Legacy Version -历史版本,不再建议使用;

Nginx的官方网站:http://nginx.org/

Nginx的下载地址:http://nginx.org/en/download.html

Nginx的历史变动:http://nginx.org/en/CHANGES

用于安全校验的PGB:http://nginx.org/download/nginx-1.13.12.tar.gz.asc

  1. -----BEGIN PGP SIGNATURE-----
  2.  
  3. iQEcBAABAgAGBQJazMZtAAoJEFIKmZOhwFL4ck4H/jktPkExP1M1U/BKmOfK4whT
  4. R4fyPT4JW6Sl8fWP26Ws//b9VwqBsfMgrh+tosS26gFqU/PY17hgD12mfGSKIvSh
  5. DyQZ2G2bD140s3KCHUKbEuKp0JfAm2p+Fj88gys1oWf5Jy+U1YRFQqEA9SYjDGXQ
  6. Do0TEcX34jNkGViDWTFH/WxrVpuO/X0UMqZ5Pi1UprZDXRXmufzt5171llVMrc57
  7. 3+DAYAn+Ve6L+XEYOI4N3YPgPrwZCbO4ZzPw5xdjd7LFRYfnu5eBdPcUAATTmnIb
  8. orxow0q/wHJN+Nze1M+iDK6kEDw/RYQcBfNBR27fra4lW7jXTIoHS1vRK0l2Trg=
  9. =cCkV
  10. -----END PGP SIGNATURE-----

2、CentOS下Yum安装,基于CentOS7或RHEL7安装方式;

稳定版本的介绍:http://nginx.org/en/linux_packages.html#stable

  1. Pre-Built Packages for Stable version
  2. To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
  3.  
  4. [nginx]
  5. name=nginx repo
  6. baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
  7. gpgcheck=0
  8. enabled=1
  9. #注意下方的提示说明,依据Linux发行版是RHEL或者CentOS以及大版本的数值,来指定OSRELEASE的值为6或者7以及centos还是rhel
  10. Replace OS with rhel or centos”, depending on the distribution used, and OSRELEASE with 6 or 7”, for 6.x or 7.x versions, respectively.
  • vim /etc/yum.repos.d/nginx.repo#新增一个nginx的repo文件,内容填写nginx的yum源信息;

repo文件详解:https://www.cnblogs.com/nineep/p/6795692.html

  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/7/$basearch/#注意此处的centos以及大版本号7;
  4. gpgcheck=0
  5. enabled=1
  • yum list|grep nginx
  • yum install -y nginx

3、nginx 安装成功的标致

  • nginx -v#查看版本号;
  • nginx -V#查看Nginx的编译安装参数;

nginx快速安装的操作命令:

  1. [BEGIN] 2018/5/26 星期六 11:30:59
  2. [root@iZqmo9i3j77p7eZ opt]# cd /etc/yum.repos.d/
  3. [root@iZqmo9i3j77p7eZ yum.repos.d]# ls
  4. CentOS-Base.repo epel.repo
  5. [root@iZqmo9i3j77p7eZ yum.repos.d]# ll
  6. total 8
  7. -rw-r--r-- 1 root root 1047 May 26 11:18 CentOS-Base.repo
  8. -rw-r--r-- 1 root root 331 May 26 11:18 epel.repo
  9. [root@iZqmo9i3j77p7eZ yum.repos.d]# vim nginx.repo
  10. total 8
  11. -rw-r--r-- 1 root root 1047 May 26 11:18 CentOS-Base.repo
  12. -rw-r--r-- 1 root root 331 May 26 11:18 epel.repo
  13. [root@iZqmo9i3j77p7eZ yum.repos.d]# vim nginx.repo
  14. [root@iZqmo9i3j77p7eZ yum.repos.d]# yum list|grep nginx
  15. collectd-nginx.x86_64 5.8.0-3.el7 epel
  16. munin-nginx.noarch 2.0.33-1.el7 epel
  17. nextcloud-nginx.noarch 10.0.4-2.el7 epel
  18. nginx.x86_64 1:1.14.0-1.el7_4.ngx nginx
  19. nginx-all-modules.noarch 1:1.12.2-2.el7 epel
  20. nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
  21. nginx-debuginfo.x86_64 1:1.14.0-1.el7_4.ngx nginx
  22. nginx-filesystem.noarch 1:1.12.2-2.el7 epel
  23. nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 epel
  24. nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7 epel
  25. nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 epel
  26. nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 epel
  27. nginx-mod-mail.x86_64 1:1.12.2-2.el7 epel
  28. nginx-mod-stream.x86_64 1:1.12.2-2.el7 epel
  29. nginx-module-geoip.x86_64 1:1.14.0-1.el7_4.ngx nginx
  30. nginx-module-geoip-debuginfo.x86_64 1:1.14.0-1.el7_4.ngx nginx
  31. nginx-module-image-filter.x86_64 1:1.14.0-1.el7_4.ngx nginx
  32. nginx-module-image-filter-debuginfo.x86_64
  33. 1:1.14.0-1.el7_4.ngx nginx
  34. nginx-module-njs.x86_64 1:1.14.0.0.2.0-1.el7_4.ngx nginx
  35. nginx-module-njs-debuginfo.x86_64 1:1.14.0.0.2.0-1.el7_4.ngx nginx
  36. nginx-module-perl.x86_64 1:1.14.0-1.el7_4.ngx nginx
  37. nginx-module-perl-debuginfo.x86_64 1:1.14.0-1.el7_4.ngx nginx
  38. nginx-module-xslt.x86_64 1:1.14.0-1.el7_4.ngx nginx
  39. nginx-module-xslt-debuginfo.x86_64 1:1.14.0-1.el7_4.ngx nginx
  40. nginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx
  41. owncloud-nginx.noarch 9.1.5-1.el7 epel
  42. pcp-pmda-nginx.x86_64 3.12.2-5.el7 base
  43. python2-certbot-nginx.noarch 0.24.0-1.el7 epel
  44. [root@iZqmo9i3j77p7eZ yum.repos.d]# yum install -y nginx
  45. Loaded plugins: fastestmirror
  46. Loading mirror speeds from cached hostfile
  47. * base: mirrors.aliyun.com
  48. * epel: mirrors.aliyun.com
  49. * extras: mirrors.aliyun.com
  50. * updates: mirrors.aliyun.com
  51. Resolving Dependencies
  52. --> Running transaction check
  53. ---> Package nginx.x86_64 1:1.14.0-1.el7_4.ngx will be installed
  54. --> Finished Dependency Resolution
  55.  
  56. Dependencies Resolved
  57.  
  58. ========================================================================================================================================
  59. Package Arch Version Repository Size
  60. ========================================================================================================================================
  61. Installing:
  62. nginx x86_64 1:1.14.0-1.el7_4.ngx nginx 750 k
  63.  
  64. Transaction Summary
  65. ========================================================================================================================================
  66. Install 1 Package
  67.  
  68. Total download size: 750 k
  69. Installed size: 2.6 M
  70. Downloading packages:
  71. nginx-1.14.0-1.el7_4.ngx.x86_64.rpm | 750 kB 00:00:02
  72. Running transaction check
  73. Running transaction test
  74. Transaction test succeeded
  75. Running transaction
  76. Installing : 1:nginx-1.14.0-1.el7_4.ngx.x86_64 1/1
  77. ----------------------------------------------------------------------
  78.  
  79. Thanks for using nginx!
  80.  
  81. Please find the official documentation for nginx here:
  82. * http://nginx.org/en/docs/
  83.  
  84. Please subscribe to nginx-announce mailing list to get
  85. the most important news about nginx:
  86. * http://nginx.org/en/support.html
  87.  
  88. Commercial subscriptions for nginx are available on:
  89. * http://nginx.com/products/
  90.  
  91. ----------------------------------------------------------------------
  92. Verifying : 1:nginx-1.14.0-1.el7_4.ngx.x86_64 1/1
  93.  
  94. Installed:
  95. nginx.x86_64 1:1.14.0-1.el7_4.ngx
  96.  
  97. Complete!
  98. [root@iZqmo9i3j77p7eZ yum.repos.d]# nginx -v
  99. nginx version: nginx/1.14.0
  100. [root@iZqmo9i3j77p7eZ yum.repos.d]# nginx -V
  101. nginx version: nginx/1.14.0
  102. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  103. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  104. TLS SNI support enabled
  105. configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

nginx快速安装

2-8Nginx的目录和配置语法_Nginx 安装目录

  • 安装目录——rpm -ql nginx
  1. etc/logrotate.d/nginx#Nginx日志轮转,用于logrotate服务的日志切割,利用了Linux系统的logrotate;
  2. /etc/nginx
  3. /etc/nginx/conf.d
  4. /etc/nginx/nginx.conf#Nginx的主配置文件,nginx启动时候会主要读取该文件;
  5. /etc/nginx/conf.d/default.conf#Nginx的默认配置文件,主配置文件中include包含,conf.d目录下内容;
  6. /etc/nginx/fastcgi_params
  7. /etc/nginx/scgi_params
  8. /etc/nginx/uwsgi_params#以上3处,cgi配置,fastcgi配置;
  9. /etc/nginx/koi-utf
  10. /etc/nginx/koi-win
  11. /etc/nginx/win-utf#以上3处,用于nginx编码转换的映射转化文件,很少用,了解即可!
  12. /etc/nginx/mime.types#设置http协议的Content-Type与拓展名的对应关系;应用场景:当Nginx无法识别某种文件类型的时候,编辑该配置文件进行添加;
  13. /etc/sysconfig/nginx #Nginx服务的启动管理的终端命令;如systemctl start nginx;stop nginx;reload nginx;
  14. /etc/sysconfig/nginx-debug#用于nginx启动的调试;
  15. /usr/lib/systemd/system/nginx-debug.service
  16. /usr/lib/systemd/system/nginx.service#以上4处,用于配置出CentOS7.X系统守护进程管理器管理方式,基于systemd;
  17. /etc/nginx/modules#Nginx模块目录
  18. /usr/lib64/nginx#Nginx模块目录
  19. /usr/lib64/nginx/modules#Nginx模块目录
  20. /usr/libexec/initscripts/legacy-actions/nginx
  21. /usr/libexec/initscripts/legacy-actions/nginx/check-reload
  22. /usr/libexec/initscripts/legacy-actions/nginx/upgrade
  23. /usr/sbin/nginx#Nginx服务的启动管理的终端命令;
  24. /usr/sbin/nginx-debug#Nginx服务的启动管理的终端命令;调试模式
  25. /usr/share/doc/nginx-1.12.2
  26. /usr/share/doc/nginx-1.12.2/COPYRIGHT
  27. /usr/share/man/man8/nginx.8.gz#以上3处,Nginx的手册和帮助文件;
  28. /usr/share/nginx
  29. /usr/share/nginx/html
  30. /usr/share/nginx/html/50x.html
  31. /usr/share/nginx/html/index.html
  32. /var/cache/nginx#Nginx的缓存目录;
  33. /var/log/nginx#nginx的日志目录;如access.log、error.log

命令代码:

  1. [BEGIN] 2018/5/26 星期六 12:44:38
  2. [root@iZqmo9i3j77p7eZ yum.repos.d]# rpm -ql nginx
  3. /etc/logrotate.d/nginx
  4. /etc/nginx
  5. /etc/nginx/conf.d
  6. /etc/nginx/conf.d/default.conf
  7. /etc/nginx/fastcgi_params
  8. /etc/nginx/koi-utf
  9. /etc/nginx/koi-win
  10. /etc/nginx/mime.types
  11. /etc/nginx/modules
  12. /etc/nginx/nginx.conf
  13. /etc/nginx/scgi_params
  14. /etc/nginx/uwsgi_params
  15. /etc/nginx/win-utf
  16. /etc/sysconfig/nginx
  17. /etc/sysconfig/nginx-debug
  18. /usr/lib/systemd/system/nginx-debug.service
  19. /usr/lib/systemd/system/nginx.service
  20. /usr/lib64/nginx
  21. /usr/lib64/nginx/modules
  22. /usr/libexec/initscripts/legacy-actions/nginx
  23. /usr/libexec/initscripts/legacy-actions/nginx/check-reload
  24. /usr/libexec/initscripts/legacy-actions/nginx/upgrade
  25. /usr/sbin/nginx
  26. /usr/sbin/nginx-debug
  27. /usr/share/doc/nginx-1.14.0
  28. /usr/share/doc/nginx-1.14.0/COPYRIGHT
  29. /usr/share/man/man8/nginx.8.gz
  30. /usr/share/nginx
  31. /usr/share/nginx/html
  32. /usr/share/nginx/html/50x.html
  33. /usr/share/nginx/html/index.html
  34. /var/cache/nginx
  35. /var/log/nginx

nginx目录结构

2-9 Nginx的目录和配置语法_Nginx编译配置参数;

1、Nginx -V#查看编译参数命令;

  1. [root@iZqmo9i3j77p7eZ ~]# nginx -V
  2.  
  3. nginx version: nginx/1.12.2
  4.  
  5. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  6.  
  7. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  8.  
  9. TLS SNI support enabled
  10.  
  11. configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules
  12. --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log
  13. --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp
  14. --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
  15. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp
  16. --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module
  17. --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module
  18. --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module
  19. --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module
  20. --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module
  21. --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
  22. --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

2、--with 的编译参数特别说明(代表官网模块);

3、安装编译参数详解;

  1. nginx的主目录;
  2. --prefix=/etc/nginx#nginx的主目录;
  3.  
  4. --sbin-path=/usr/sbin/nginx#nginx的执行命令目录;
  5.  
  6. --modules-path=/usr/lib64/nginx/modules#nginx的模块目录;
  7.  
  8. --conf-path=/etc/nginx/nginx.conf#nginx的配置文件目录;
  9.  
  10. --error-log-path=/var/log/nginx/error.log#nginx的错误日志目录;
  11.  
  12. --http-log-path=/var/log/nginx/access.log#nginx的访问日志目录;
  13.  
  14. --pid-path=/var/run/nginx.pid#nginx的pid进程文件路径;
  15.  
  16. --lock-path=/var/run/nginx.lock#Nginx锁文件目录;
  17.  
  18. 执行对应模块时,Nginx所保留的临时性文件;带有temp后缀字样的文件;
  19. --http-client-body-temp-path=/var/cache/nginx/client_temp
  20.  
  21. --http-proxy-temp-path=/var/cache/nginx/proxy_temp
  22.  
  23. --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
  24.  
  25. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
  26.  
  27. --http-scgi-temp-path=/var/cache/nginx/scgi_temp
  28.  
  29. 处于服务器安全性的考虑,设定Nginx进程启动的用户和组用户;不建议使用root用户;
  30. --user=nginx
  31. --group=nginx
  32.  
  33. --with-cc-opt=parameters#设置额外的参数将被添加到CFLAGS变量;
  34. --with-ld-opt=parameters#设置附加的参数,链接系统库,如pcre库;

2-10 Nginx的目录和配置语法_默认配置语法

1、主配置文件nginx.conf讲解;

  1. [root@iZqmo9i3j77p7eZ nginx]# cat nginx.conf
  2. user nginx;
  3. worker_processes 1;
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';
  15. access_log /var/log/nginx/access.log main;
  16. sendfile on;
  17. #tcp_nopush on;
  18. keepalive_timeout 65;
  19. #gzip on;
  20.  
  21. include /etc/nginx/conf.d/*.conf;#注意此处的包含关系;
  22. }
  23. [root@iZqmo9i3j77p7eZ nginx]#

2、nginx.default.conf详解;

  1. [root@iZqmo9i3j77p7eZ conf.d]# cat default.conf
  2.  
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. #charset koi8-r;
  7. #access_log /var/log/nginx/host.access.log main;
  8. location / {
  9. root /usr/share/nginx/html;
  10. index index.html index.htm;
  11. }
  12. #error_page 404 /404.html;
  13.  
  14. # redirect server error pages to the static page /50x.html
  15. #
  16. error_page 500 502 503 504 /50x.html;
  17.  
  18. location = /50x.html {
  19.  
  20. root /usr/share/nginx/html;
  21. }
  22.  
  23. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  24.  
  25. #
  26. #location ~ \.php$ {
  27.  
  28. # proxy_pass http://127.0.0.1;
  29. #}
  30.  
  31. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  32. #
  33. #location ~ \.php$ {
  34.  
  35. # root html;
  36.  
  37. # fastcgi_pass 127.0.0.1:9000;
  38.  
  39. # fastcgi_index index.php;
  40.  
  41. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  42.  
  43. # include fastcgi_params;
  44.  
  45. #}
  46.  
  47. # deny access to .htaccess files, if Apache's document root
  48.  
  49. # concurs with nginx's one
  50.  
  51. #
  52.  
  53. #location ~ /\.ht {
  54.  
  55. # deny all;
  56.  
  57. #}
  58.  
  59. }

1)user#设置nginx服务的系统使用用户,即Linux系统中的用户;

2)worker_processes#工作进程数,一般和CPU核数保持一致即可,开启CPU亲和!增大连接数的并发处理能力;

3)error_log#定义nginx的错误日志,绝对路径;

4)pid#nginx #服务启动时候的nginx.pid文件,绝对路径;

5)events区块;

  • worker_connections#每个进程允许的最大连接数,最大调整到65535;企业应用场景,必须要调节的;
  • use#工作进程的内核模型,如select、poll或epoll;

6)http区块;

  • 每一个server设置独立的站点;
  • location,控制每一层访问的路径,配置默认访问的路径定义的位置配置;
  • error_page,定义错误页面,友好提示,提醒错误;
  • location中的root,不是计算机root用户,而是表示文件存储的“根路径”
  • 一个http区块可以存在多个server;
  • 一个server可以存在多个location;

2-11 Nginx的目录和配置语法_默认配置与默认站点启动

1、nginx.conf配置文件;

  1. [root@iZqmo9i3j77p7eZ conf.d]# cat ../nginx.conf
  2. user nginx;
  3. worker_processes 1;
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11.  
  12. default_type application/octet-stream;
  13.  
  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15.  
  16. '$status $body_bytes_sent "$http_referer" '
  17.  
  18. '"$http_user_agent" "$http_x_forwarded_for"';
  19.  
  20. access_log /var/log/nginx/access.log main;
  21.  
  22. sendfile on;
  23. #tcp_nopush on;
  24. keepalive_timeout 65;
  25. #gzip on;
  26. include /etc/nginx/conf.d/*.conf;
  27. }

其中,

  • log_format#定义日志类型;
  • access_log#定义访问日志;
  • sendfile#默认打开on状态;
  • keepalive_timeout#设置客户端与服务器的超时时间;存在默认值65s;

2、conf.d/default.conf配置文件;

  1. [root@iZqmo9i3j77p7eZ backup]# cat default.conf
  2. server {
  3. listen 80;#监听端口;
  4. server_name localhost;#域名地址;
  5.  
  6. #charset koi8-r;
  7. #access_log /var/log/nginx/host.access.log main;
  8.  
  9. location / {
  10. root /usr/share/nginx/html;#此处是Linux服务器的绝对路径;
  11. index index.html index.htm;#HTML是一个静态文件,不需要重启Nginx服务;
  12. }
  13.  
  14. #error_page 404 /404.html;
  15.  
  16. # redirect server error pages to the static page /50x.html
  17. #
  18. error_page 500 502 503 504 404 /50x.html;#新增了404错误状态码,定义错误页面,修改后需要重启Nginx服务systemctl restart nginx.service
  19. location = /50x.html {
  20. root /usr/share/nginx/html;
  21. }
  22.  
  23. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  24. #
  25. #location ~ \.php$ {
  26. # proxy_pass http://127.0.0.1;
  27. #}
  28.  
  29. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  30. #
  31. #location ~ \.php$ {
  32. # root html;
  33. # fastcgi_pass 127.0.0.1:9000;
  34. # fastcgi_index index.php;
  35. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  36. # include fastcgi_params;
  37. #}
  38.  
  39. # deny access to .htaccess files, if Apache's document root
  40. # concurs with nginx's one
  41. #
  42. #location ~ /\.ht {
  43. # deny all;
  44. #}
  45. }
  46.  
  47. [root@iZqmo9i3j77p7eZ backup]#

3、/usr/share/nginx/html/index.html;

  1. [root@iZqmo9i3j77p7eZ html]# cat index.html
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx!</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx! tqtl911@163.com</h1>#添加了文件内容tqtl911@163.com
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required.</p>
  18.  
  19. <p>For online documentation and support please refer to
  20. <a href="http://nginx.org/">nginx.org</a>.<br/>
  21. Commercial support is available at
  22. <a href="http://nginx.com/">nginx.com</a>.</p>
  23.  
  24. <p><em>Thank you for using nginx.</em></p>
  25. </body>
  26. </html>
  27. [root@iZqmo9i3j77p7eZ html]#

4、/usr/share/nginx/html/50x.html;#返回错误页面;

  1. [root@iZqmo9i3j77p7eZ html]# cat 50x.html
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Error</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>An error occurred. tqtl add this error!</h1>
  16. <p>Sorry, the page you are looking for is currently unavailable.<br/>
  17. Please try again later.</p>
  18. <p>If you are the system administrator of this resource then you should check
  19. the <a href="http://nginx.org/r/error_log">error log</a> for details.</p>
  20. <p><em>Faithfully yours, nginx.</em></p>
  21. </body>
  22. </html>
  1. [BEGIN] 2018/5/26 星期六 12:58:17
  2. [root@iZqmo9i3j77p7eZ yum.repos.d]# nginx -v
  3. nginx version: nginx/1.14.0
  4. [root@iZqmo9i3j77p7eZ yum.repos.d]# nginx -V
  5. nginx version: nginx/1.14.0
  6. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  7. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  8. TLS SNI support enabled
  9. configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  10. [root@iZqmo9i3j77p7eZ yum.repos.d]# cd /etc/nginx/
  11. [root@iZqmo9i3j77p7eZ nginx]# ll
  12. total 40
  13. drwxr-xr-x 2 root root 4096 May 26 12:36 conf.d
  14. -rw-r--r-- 1 root root 1007 Apr 17 23:48 fastcgi_params
  15. -rw-r--r-- 1 root root 2837 Apr 17 23:48 koi-utf
  16. -rw-r--r-- 1 root root 2223 Apr 17 23:48 koi-win
  17. -rw-r--r-- 1 root root 5170 Apr 17 23:48 mime.types
  18. lrwxrwxrwx 1 root root 29 May 26 12:36 modules -> ../../usr/lib64/nginx/modules
  19. -rw-r--r-- 1 root root 643 Apr 17 23:46 nginx.conf
  20. -rw-r--r-- 1 root root 636 Apr 17 23:48 scgi_params
  21. -rw-r--r-- 1 root root 664 Apr 17 23:48 uwsgi_params
  22. -rw-r--r-- 1 root root 3610 Apr 17 23:48 win-utf
  23. [root@iZqmo9i3j77p7eZ nginx]# vim nginx.conf
  24. [root@iZqmo9i3j77p7eZ nginx]# cat nginx.conf
  25.  
  26. user nginx;
  27. worker_processes 1;
  28.  
  29. error_log /var/log/nginx/error.log warn;
  30. pid /var/run/nginx.pid;
  31.  
  32. events {
  33. worker_connections 1024;
  34. }
  35.  
  36. http {
  37. include /etc/nginx/mime.types;
  38. default_type application/octet-stream;
  39.  
  40. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  41. '$status $body_bytes_sent "$http_referer" '
  42. '"$http_user_agent" "$http_x_forwarded_for"';
  43.  
  44. access_log /var/log/nginx/access.log main;
  45.  
  46. sendfile on;
  47. #tcp_nopush on;
  48.  
  49. keepalive_timeout 65;
  50.  
  51. #gzip on;
  52.  
  53. include /etc/nginx/conf.d/*.conf;
  54. }
  55. [root@iZqmo9i3j77p7eZ nginx]# cd /usr/share/nginx/html/
  56. [root@iZqmo9i3j77p7eZ html]# ls
  57. 50x.html index.html
  58. [root@iZqmo9i3j77p7eZ html]# vim index.html
  59. [root@iZqmo9i3j77p7eZ html]# systemctl status nginx
  60. ● nginx.service - nginx - high performance web server
  61. Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  62. Active: inactive (dead)
  63. Docs: http://nginx.org/en/docs/
  64. [root@iZqmo9i3j77p7eZ html]# systemctl start nginx
  65. [root@iZqmo9i3j77p7eZ html]# systemctl status nginx
  66. ● nginx.service - nginx - high performance web server
  67. Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  68. Active: active (running) since Sat 2018-05-26 14:11:31 CST; 1s ago
  69. Docs: http://nginx.org/en/docs/
  70. Process: 20491 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  71. Main PID: 20492 (nginx)
  72. CGroup: /system.slice/nginx.service
  73. ├─20492 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  74. └─20493 nginx: worker process
  75.  
  76. May 26 14:11:31 iZqmo9i3j77p7eZ systemd[1]: Starting nginx - high performance web server...
  77. May 26 14:11:31 iZqmo9i3j77p7eZ systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
  78. May 26 14:11:31 iZqmo9i3j77p7eZ systemd[1]: Started nginx - high performance web server.
  79. [root@iZqmo9i3j77p7eZ html]# vim /etc/nginx/conf.d/default.conf
  80. [root@iZqmo9i3j77p7eZ html]# cd /usr/share/nginx/
  81. [root@iZqmo9i3j77p7eZ nginx]# ls
  82. html
  83. [root@iZqmo9i3j77p7eZ nginx]# cd html/
  84. [root@iZqmo9i3j77p7eZ html]# ll
  85. total 8
  86. -rw-r--r-- 1 root root 537 Apr 17 23:48 50x.html
  87. -rw-r--r-- 1 root root 628 May 26 14:11 index.html
  88. [root@iZqmo9i3j77p7eZ html]# vim 50x.html
  89. -rw-r--r-- 1 root root 628 May 26 14:11 index.html
  90. [root@iZqmo9i3j77p7eZ html]# vim 50x.html
  91. [root@iZqmo9i3j77p7eZ html]# cat index.html
  92. <!DOCTYPE html>
  93. <html>
  94. <head>
  95. <title>Welcome to nginx!</title>
  96. <style>
  97. body {
  98. width: 35em;
  99. margin: 0 auto;
  100. font-family: Tahoma, Verdana, Arial, sans-serif;
  101. }
  102. </style>
  103. </head>
  104. <body>
  105. <h1>Welcome to nginx! tqtl911@163.com</h1>
  106. <p>If you see this page, the nginx web server is successfully installed and
  107. working. Further configuration is required.</p>
  108.  
  109. <p>For online documentation and support please refer to
  110. <a href="http://nginx.org/">nginx.org</a>.<br/>
  111. Commercial support is available at
  112. <a href="http://nginx.com/">nginx.com</a>.</p>
  113.  
  114. <p><em>Thank you for using nginx.</em></p>
  115. </body>
  116. </html>
  117. [root@iZqmo9i3j77p7eZ html]# cat 50x.html
  118. <!DOCTYPE html>
  119. <html>
  120. <head>
  121. <title>Error</title>
  122. <style>
  123. body {
  124. width: 35em;
  125. margin: 0 auto;
  126. font-family: Tahoma, Verdana, Arial, sans-serif;
  127. }
  128. </style>
  129. </head>
  130. <body>
  131. <h1>An error occurred. tqtl add this error!</h1>
  132. <p>Sorry, the page you are looking for is currently unavailable.<br/>
  133. Please try again later.</p>
  134. <p>If you are the system administrator of this resource then you should check
  135. the <a href="http://nginx.org/r/error_log">error log</a> for details.</p>
  136. <p><em>Faithfully yours, nginx.</em></p>
  137. </body>
  138. </html>
  139. [root@iZqmo9i3j77p7eZ html]# nginx -t
  140. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  141. nginx: configuration file /etc/nginx/nginx.conf test is successful
  142. [root@iZqmo9i3j77p7eZ html]# systemctl reload nginx
  143. [root@iZqmo9i3j77p7eZ html]# systemctl status nginx
  144. ● nginx.service - nginx - high performance web server
  145. Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  146. Active: active (running) since Sat 2018-05-26 14:11:31 CST; 6min ago
  147. Docs: http://nginx.org/en/docs/
  148. Process: 20509 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
  149. Process: 20491 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  150. Main PID: 20492 (nginx)
  151. CGroup: /system.slice/nginx.service
  152. ├─20492 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  153. └─20510 nginx: worker process
  154.  
  155. May 26 14:11:31 iZqmo9i3j77p7eZ systemd[1]: Starting nginx - high performance web server...
  156. May 26 14:11:31 iZqmo9i3j77p7eZ systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
  157. May 26 14:11:31 iZqmo9i3j77p7eZ systemd[1]: Started nginx - high performance web server.
  158. May 26 14:17:54 iZqmo9i3j77p7eZ systemd[1]: Reloaded nginx - high performance web server.
  159. [root@iZqmo9i3j77p7eZ html]# pwd
  160. /usr/share/nginx/html

Nginx的目录和配置语法_默认配置语法

2-12 HTTP请求;

1、客户端<--->服务端(request与response请求)

2、curl -v http://www.jd.com >/dev/null

[root@iZqmo9i3j77p7eZ ~]# curl -v http://www.jd.com >/dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to www.jd.com port 80 (#0)
* Trying 106.39.178.1...
* Connected to www.jd.com (106.39.178.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.jd.com
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: JDWS/2.0
< Date: Wed, 13 Jun 2018 11:20:10 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
< Location: https://www.jd.com/
< Strict-Transport-Security: max-age=360
<
{ [data not shown]
100 157 100 157 0 0 17367 0 --:--:-- --:--:-- --:--:-- 19625
* Connection #0 to host www.jd.com left intact
[root@iZqmo9i3j77p7eZ ~]#

3、window或Mac下使用浏览器开发者工具进行查看!F12

2-13 Nginx日志_Log_format1;

1、主配置文件nginx.conf中日志相关;

  1. [root@iZqmo9i3j77p7eZ nginx]# cat nginx.conf
  2.  
  3. user nginx;
  4. worker_processes 1;
  5.  
  6. error_log /var/log/nginx/error.log warn;#warn是错误日志的级别;
  7. pid /var/run/nginx.pid;
  8.  
  9. events {
  10. worker_connections 1024;
  11. }
  12.  
  13. http {
  14. include /etc/nginx/mime.types;
  15. default_type application/octet-stream;
  16.  
  17. log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
  18. '$status $body_bytes_sent "$http_referer" '
  19. '"$http_user_agent" "$http_x_forwarded_for"';
  20.  
  21. access_log /var/log/nginx/access.log main;#mainlog_foramt的名称,Nginx的变量,定义了日志输出的形式
  22.  
  23. sendfile on;
  24. #tcp_nopush on;
  25.  
  26. keepalive_timeout 65;
  27.  
  28. #gzip on;
  29.  
  30. include /etc/nginx/conf.d/*.conf;
  31. }
  32. [root@iZqmo9i3j77p7eZ nginx]#

2、/var/log/nginx/error.log#错误日志;

3、/var/log/nginx/access.log#访问日志;

4、Nginx变量;

  1. [root@iZqmo9i3j77p7eZ nginx]# curl -v www.imooc.com/
  2. * About to connect() to www.imooc.com port 80 (#0)
  3. * Trying 117.121.101.40...
  4. * Connected to www.imooc.com (117.121.101.40) port 80 (#0)
  5. > GET / HTTP/1.1
  6. > User-Agent: curl/7.29.0
  7. > Host: www.imooc.com
  8. > Accept: */*
  9. >
  10. < HTTP/1.1 301 Moved Permanently
  11. < Server: nginx
  12. < Date: Thu, 22 Mar 2018 03:01:41 GMT
  13. < Content-Type: text/html
  14. < Content-Length: 178
  15. < Connection: keep-alive
  16. < Location: https://www.imooc.com
  17. <
  18. <html>
  19. <head><title>301 Moved Permanently</title></head>
  20. <body bgcolor="white">
  21. <center><h1>301 Moved Permanently</h1></center>
  22. <hr><center>nginx</center>
  23. </body>
  24. </html>
  25. * Connection #0 to host www.imooc.com left intact
  26. [root@iZqmo9i3j77p7eZ nginx]#

Nginx的内置变量:http://nginx.org/en/docs/syslog.html

5、依赖于log_format模块;

  1. [root@iZqmo9i3j77p7eZ nginx]# cat nginx.conf
  2.  
  3. user nginx;
  4. worker_processes 1;
  5.  
  6. error_log /var/log/nginx/error.log warn;
  7. pid /var/run/nginx.pid;
  8.  
  9. events {
  10. worker_connections 1024;
  11. }
  12.  
  13. http {
  14. include /etc/nginx/mime.types;
  15. default_type application/octet-stream;
  16.  
  17. log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
  18. '$status $body_bytes_sent "$http_referer" '
  19. '"$http_user_agent" "$http_x_forwarded_for"';
  20.  
  21. access_log /var/log/nginx/access.log main;
  22.  
  23. sendfile on;
  24. #tcp_nopush on;
  25.  
  26. keepalive_timeout 65;
  27.  
  28. #gzip on;
  29.  
  30. include /etc/nginx/conf.d/*.conf;
  31. }
  32. [root@iZqmo9i3j77p7eZ nginx]#

6、Nginx语法检查#nginx -t -c /etc/nginx/nginx.conf #或者nignx -tc

7、Nginx重启服务#nginx -s reload -c /etc/nginx/nginx.conf#systemctl reload nginx

2-14Nginx日志_Log_format2;

1、log_format;

  1. log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
  2. '$status $body_bytes_sent "$http_referer" '#referer表示上一级
  3. '"$http_user_agent" #表示客户端内容"$http_x_forwarded_for"';#记录每一级用户的信息
  4.  
  5. access_log /var/log/nginx/access.log main;#此处的mainlog_format是一一对应的;

2、HTTP请求变量 -arg_PARAMETER 、http_HEADER、sent_http_HEADER ;

3、内置变量-Nginx内置的变量;

4、自定义变量-用户自行定义的变量;

2-15 Nginx模块讲解

1、查看Nginx官网模块;

  1. [root@iZqmo9i3j77p7eZ nginx]# nginx -V
  2. nginx version: nginx/1.12.2
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  4. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  5. TLS SNI support enabled
  6. configure arguments:
  7. --prefix(前缀)=/etc/nginx
  8. --sbin-path=/usr/sbin/nginx
  9. --modules-path=/usr/lib64/nginx/modules
  10. --conf-path=/etc/nginx/nginx.conf
  11. --error-log-path=/var/log/nginx/error.log
  12. --http-log-path=/var/log/nginx/access.log
  13. --pid-path=/var/run/nginx.pid
  14. --lock-path=/var/run/nginx.lock
  15. --http-client-body-temp-path=/var/cache/nginx/client_temp
  16. --http-proxy-temp-path=/var/cache/nginx/proxy_temp
  17. --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
  18. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
  19. --http-scgi-temp-path=/var/cache/nginx/scgi_temp
  20. --user=nginx --group=nginx
  21. --with-compat(兼容) --with-file-aio
  22. --with-threads --with-http_addition_module
  23. --with-http_auth_request_module
  24. --with-http_dav_module
  25. --with-http_flv_module
  26. --with-http_gunzip_module
  27. --with-http_gzip_static_module
  28. --with-http_mp4_module
  29. --with-http_random_index_module
  30. --with-http_realip_module
  31. --with-http_secure_link_module
  32. --with-http_slice_module
  33. --with-http_ssl_module
  34. --with-http_stub_status_module
  35. --with-http_sub_module
  36. --with-http_v2_module
  37. --with-mail --with-mail_ssl_module
  38. --with-stream
  39. --with-stream_realip_module
  40. --with-stream_ssl_module
  41. --with-stream_ssl_preread_module
  42. --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
  43. --param(参数)=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
  44. --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  45. [root@iZqmo9i3j77p7eZ nginx]#

2、第三方模块;(第三方公司开发的模块,如FastDFS中的nginx相关模块)

2-16 Nginx的模块讲解 _sub_status

1、 --with-http_stub_status_module #展示Nginx的客户端状态

2、配置语法;

Syntax:stub_status;#末尾分号是一定要有的;

Default:-

Context:server,location

3、配置

  1. server {
  2.  
  3. listen 80;
  4.  
  5. server_name localhost;
  6.  
  7. #charset koi8-r;
  8.  
  9. #access_log /var/log/nginx/host.access.log main;
  10.  
  11. location /mystatus{
  12.  
  13. stub_status;
  14. }
  15. location / {
  16.  
  17. root /usr/share/nginx/html;
  18.  
  19. index index.html index.htm;
  20.  
  21. }
  22.  
  23. #error_page 404 /404.html;
  24. # redirect server error pages to the static page /50x.html
  25. #
  26. error_page 500 502 503 504 404 /50x.html;
  27.  
  28. location = /50x.html {
  29.  
  30. root /usr/share/nginx/html;
  31. }
  32. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  33. #
  34. #location ~ \.php$ {
  35.  
  36. # proxy_pass http://127.0.0.1;
  37.  
  38. #}
  39. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:900
  40. 0
  41. #
  42. #location ~ \.php$ {
  43. # root html;
  44. # fastcgi_pass 127.0.0.1:9000;
  45. # fastcgi_index index.php;
  46. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  47. # include fastcgi_params;
  48. #}
  49. # deny access to .htaccess files, if Apache's document root
  50. # concurs with nginx's one
  51. #
  52. #location ~ /\.ht {
  53. # deny all;
  54.  
  55. #}
  56. }

4、访问验证:http://47.95.234.20/mystatus

日志输出:

Active connections: 3

server accepts handled requests

135 135 278

Reading: 0 Writing: 1 Waiting: 2

2-17 Nginx模块讲解_random_index

1、--with-http_random_index_module#目录中选择一个随机主页;

2、随机展示主页;

3、配置语法:

Syntax:random_index on|off;
Default:random_index off;
Context:location

4、注意事项

  • 修改完之后重新reload nginx服务;
  • 隐藏文件不会被加载显示,如:.1.html

2-18 Nginx模块讲解_sub_module

1、--with-http_sub_module模块#HTTP内容替换

2、应用场景:多个虚拟主机的内容变更替换;

3、配置语法:

  1. 1、对字符串进行替换,Syntaxsub_filter string(要替换的内容) replacement(被替换的内容);
  2.  
  3. Default:-
  4.  
  5. Context:http,server,location#可以加载的位置区块
  6.  
  7. 2Syntaxsub_filter_last_modified on|off
  8.  
  9. Default:sub_filter_last_modified off;
  10.  
  11. Context:http,server,location
  12.  
  13. 3Syntaxsub_filter_once on|off#匹配所有html代码中的第一个还是最后一个所有字符串
  14.  
  15. Default:sub_filter_once off;
  16.  
  17. Context:http,server,location

2-19 Nginx模块讲解_sub_module配置演示;

1、vim /etc/nginx/conf.d/default.conf

  1. location / {
  2.  
  3. root /opt/app/code;#根路径
  4.  
  5. index index.html index.html
  6.  
  7. sub_filter '<a>imooc' '<a>IMOOC';
  8.  
  9. sub_filer_once on;#注意NginxSQL一样,默认结尾加分号;
  10.  
  11. }#完成指定字符串替换的配置语法;

3、完成内容替换后,先清理浏览器缓存或者新建立窗口,强制刷新页面;

4、单一替换以及全局替换参数配置说明;

2-20 Nginx的请求限制_配置语法与原理1

1、连接频率限制=limit_conn_module

2、请求频率限制-limit_req_module

3、HTTP协议连接与请求;

  • HTTP协议是建立在TCP协议的基础之上;
  • 要完成一次HTTP的请求,先要完成TCP/IP的三次握手
  • 仅进行TCP的“三次握手”,然后进行HTTP的“请求”与“相应”,接着保持长连接和keepalive;
  • HTTP请求建立在一次TCP连接基础之上;
  • 一次TCP请求至少产生一次HTTP请求;

4、配置语法:

  1. Syntaxlimit_conn_zone key zone=name:size;
  2.  
  3. Default:——
  4.  
  5. Context:http
  6.  
  7. Syntax:limit_conn zone number;
  8.  
  9. Default:-
  10.  
  11. Context:http,server,location

2-21 Nginx的请求限制_配置语法与原理2;

1、请求限制;

2、语法规则:

  1. Syntaxlimit_req_zone key zone=name:size rate=rate#此处有速率;
  2.  
  3. Default:-
  4.  
  5. Context:http
  6.  
  7. Syntax:limit_req zone=name [burst=number][nodelay];
  8.  
  9. Default:-
  10.  
  11. Context:http,server,location

3、请求限制测试-apache下ab压力测试工具:

  • ab -n 50 -c 20 http://www.jd.com/
  1. [root@iZqmo9i3j77p7eZ conf.d]# ab -n 50 -c 20 http://www.jd.com/
  2.  
  3. This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
  4. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  5. Licensed to The Apache Software Foundation, http://www.apache.org/
  6. Benchmarking(标记,确定基准点) www.jd.com (be patient).....done
  7. Server Software: JDWS/2.0
  8. Server Hostname: www.jd.com
  9. Server Port: 80
  10.  
  11. Document Path: /
  12.  
  13. Document Length: 157 bytes
  14.  
  15. Concurrency Level: 20
  16.  
  17. Time taken for tests: 0.019 seconds
  18.  
  19. Complete requests: 50
  20.  
  21. Failed requests: 0
  22.  
  23. Write errors: 0
  24.  
  25. Non-2xx responses: 50
  26.  
  27. Total transferred: 19100 bytes
  28.  
  29. HTML transferred: 7850 bytes
  30.  
  31. Requests per second: 2646.90 [#/sec] (mean)
  32.  
  33. Time per request: 7.556 [ms] (mean)
  34.  
  35. Time per request: 0.378 [ms] (mean, across all concurrent requests)
  36.  
  37. Transfer rate: 987.42 [Kbytes/sec] received
  38.  
  39. Connection Times (ms)
  40.  
  41. min mean[+/-sd] median max
  42.  
  43. Connect: 2 2 0.4 2 3
  44.  
  45. Processing: 2 3 0.4 2 4
  46.  
  47. Waiting: 2 2 0.4 2 4
  48.  
  49. Total: 4 5 0.8 5 7
  50.  
  51. ERROR: The median and mean for the processing time are more than twice the standard
  52.  
  53. deviation apart. These results are NOT reliable.
  54.  
  55. Percentage of the requests served within a certain time (ms)
  56.  
  57. 50% 5
  58.  
  59. 66% 5
  60.  
  61. 75% 6
  62.  
  63. 80% 6
  64.  
  65. 90% 6
  66.  
  67. 95% 6
  68.  
  69. 98% 7
  70.  
  71. 99% 7
  72.  
  73. 100% 7 (longest request)

2-22 Nginx的请求限制_配置语法与原理3;

1、

2、通过访问日志及错误日志进行判断配置是否生效;

2-23 Nginx的访问控制_介绍访问控制的基本方式;

1、基于IP的访问控制-http_access_module;

2、基于用户的信任登录-http_auth_basic_module;

2-24 Nginx的访问控制-access_module配置语法介绍;

1、语法规则:

  1. Syntaxdeny address |CIDR|unix:|all;
  2.  
  3. Default:-
  4.  
  5. Context:http,server,location,limit_except
  6.  
  7. Syntaxallow address |CIDR(可变长子网掩码,即网段)|unix:|all;
  8.  
  9. Default:-
  10.  
  11. Context:http,server,location,limit_except

2-25 Nginx的访问控制-access_module配置;

1、配置举例;

  1. [root@iZqmo9i3j77p7eZ conf.d]# cat access_mode.conf
  2.  
  3. server {
  4.  
  5. listen 80;
  6.  
  7. server_name localhost;
  8.  
  9. #charset koi8-r;
  10.  
  11. #access_log /var/log/nginx/host.access.log main;
  12.  
  13. location / {
  14.  
  15. root /opt/app/code;
  16.  
  17. index index.html index.htm;
  18.  
  19. }
  20.  
  21. location ~ ^/ {
  22.  
  23. root /opt/app/code;
  24.  
  25. deny 117.119.97.51;
  26.  
  27. allow all;
  28.  
  29. index index.html index.htm;
  30.  
  31. }
  32.  
  33. #error_page 404 /404.html;
  34.  
  35. # redirect server error pages to the static page /50x.html
  36.  
  37. #
  38. error_page 500 502 503 504 /50x.html;
  39.  
  40. location = /50x.html {
  41.  
  42. root /usr/share/nginx/html;
  43.  
  44. }
  45.  
  46. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  47.  
  48. #
  49.  
  50. #location ~ \.php$ {
  51.  
  52. # proxy_pass http://127.0.0.1;
  53.  
  54. #}
  55.  
  56. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  57.  
  58. #
  59.  
  60. #location ~ \.php$ {
  61.  
  62. # root html;
  63.  
  64. # fastcgi_pass 127.0.0.1:9000;
  65.  
  66. # fastcgi_index index.php;
  67.  
  68. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  69.  
  70. # include fastcgi_params;
  71.  
  72. #}
  73. # deny access to .htaccess files, if Apache's document root
  74. # concurs with nginx's one
  75. #
  76. #location ~ /\.ht {
  77. # deny all;
  78.  
  79. #}
  80.  
  81. }

2-26 Nginx的访问控制-access_module局限性;

1、

2、

3、

4、

5、x_forwarded_for会解决http_access_module的局限性问题,但对协议有要求

  • 结合geo模块做
  • 通过HTTP自定义变量传递

2-27 Nginx的访问控制-auth_basic_module配置;

1、

2、 htpasswd -c ./user_passwd cuixiaozhao#htpasswd工具属于httpd-tools

3、配置举例

  1. [root@iZqmo9i3j77p7eZ conf.d]# cat auth_modele.conf
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. #charset koi8-r;
  6. #access_log /var/log/nginx/host.access.log main;
  7. location / {
  8. root /opt/app/code;
  9. index index.html index.htm;
  10. }
  11. location ~ ^/ {
  12. root /opt/app/code;
  13. auth_basic "cuixiaozhao";
  14. auth_basic_user_file /etc/nginx/user_passwd;
  15. allow all;
  16. index index.html index.htm;
  17. }
  18. #error_page 404 /404.html;
  19. # redirect server error pages to the static page /50x.html
  20. #
  21. error_page 500 502 503 504 /50x.html;
  22. location = /50x.html {
  23. root /usr/share/nginx/html;
  24. }
  25.  
  26. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  27. #
  28. #location ~ \.php$ {
  29. # proxy_pass http://127.0.0.1;
  30. #}
  31. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  32. #
  33. #location ~ \.php$ {
  34. # root html;
  35. # fastcgi_pass 127.0.0.1:9000;
  36. # fastcgi_index index.php;
  37. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  38. # include fastcgi_params;
  39. #}
  40. # deny access to .htaccess files, if Apache's document root
  41. # concurs with nginx's one
  42. #
  43. #location ~ /\.ht {
  44. # deny all;
  45. #}
  46. }

4、访问限制截图

2-28 Nginx的访问控制-auth_basic_module(基本认证模块)局限性;

1、用户信息依赖文件方式,效率低下;

2、操作管理机械,效率低下;

3、解决方案:

  • Nginx结合LUA实现高效验证;
  • Nginx和LDAP打通,利用nginx-auth-ldap模块;

Nginx从入门到放弃-第2章 基础篇的更多相关文章

  1. Nginx从入门到放弃-第3章 场景实战篇

    3-1场景实践篇内容介绍: 3-2 Nginx作为静态资源Web服务_静态资源类型: 3-3 Nginx作为静态资源web服务_CDN(内容分发网络)场景: 3-4 Nginx作为静态资源Web服务_ ...

  2. Nginx从入门到放弃-第5章 Nginx架构篇

    5-1 Nginx常见问题_架构篇介绍 5-2 Nginx常见问题_多个server中虚拟主机读取的优先级 5-3 Nginx常见问题_多个location匹配的优先级1 5-4 Nginx常见问题_ ...

  3. Nginx从入门到放弃-第4章 深度学习篇

    4-1 Nginx动静分离_动静分离场景演示 4-2 Nginx动静分离_动静分离场景演示1 4-3 Nginx的动静分离_动静分离场景演示2 4-4 Rewrite规则_rewrite规则的作用 4 ...

  4. php从入门到放弃系列-02.php基础语法

    php从入门到放弃系列-02.php基础语法 一.学习语法,从hello world开始 PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器&qu ...

  5. 深度学习入门者的Python快速教程 - 基础篇

      5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5.1.1 Python简史 Python是一门解释型的高级编程语言,特点是简单明 ...

  6. 前端开发工程师 - 03.DOM编程艺术 - 第1章.基础篇(下)

    第1章.基础篇(下) Abstract: 数据通信.数据存储.动画.音频与视频.canvas.BOM.表单操作.列表操作 数据通信(HTTP协议) HTTP事务: 客户端向服务器端发送HTTP请求报文 ...

  7. 前端开发工程师 - 03.DOM编程艺术 - 第1章.基础篇(上)

    第1章.基础篇(上) Abstract:文档树.节点操作.属性操作.样式操作.事件 DOM (Document Object Model) - 文档对象模型 以对象的方式来表示对应的html,它有一系 ...

  8. 前端开发工程师 - 02.JavaScript程序设计 - 第1章.基础篇

    第1章--基础篇 JS介绍 html 网页的内容:css 网页的样式:javascript 网页的行为 i.e. hello world <!DOCTYPE html> <html& ...

  9. WPF从入门到放弃系列第二章 XAML

    本文是作者学习WPF从入门到放弃过程中的一些总结,主要内容都是对学习过程中拜读的文章的整理归纳. 参考资料 XAML 概述 (WPF):https://msdn.microsoft.com/zh-cn ...

随机推荐

  1. 如何快速构建CMBD系统-glpi

    一.CMBD系统构建步骤 起初,开发这套CMBD系统是为了帮助朋友公司简化设备统计操作,以代替人工入库方式.举个例子,单位发放笔记本,或者设备更换了硬盘,都需要人工签到,手动输入统计,安装了CMBD系 ...

  2. 【开发小结】Two Steps from Deadline

    进度条可以救我也可以杀死我 # START 2018年4月17日晚我测试了11组四则运算的UI,每个exe程序生成的每一道题都有恐怖的倒计时.PSP表格清晰的记录了开发过程中消耗的时间,但是在结对作业 ...

  3. Redis相关注意事项

    本文介绍了五个使用Redis使用时的注意事项.如果你在使用或者考虑使用Redis,你可以学习一下下面的一些建议,避免遇到以下提到的问题. 一.配置相关注意事项 1.涉及到内存的单位注意添加 b 1k ...

  4. sql优化实战:从1353秒到135秒(删除索引+修改数据+重建索引)

    最近在优化日结存储过程,日结存储过程中大概包含了20多个存储过程. 发现其有一个存储过程代码有问题,进一步发现结存的数据中有一个 日期字段business_date 是有问题的,这个字段对应的类型是v ...

  5. Python之时间表示

    Python的time模块中提供了丰富的关于时间操作方法,可以利用这些方法来完成这个需求. time.time() :获取当前时间戳 time.ctime(): 当前时间的字符串形式 time.loc ...

  6. 融云SDK触达用户数破20亿 王者风范双倍展现

    11月1日,融云SDK触达用户数突破20亿,业务增长速度及用户覆盖量再创即时通讯云领域新高.自去年11月10日公布SDK触达用户数破10亿以来,融云仅用了一年时间,便取得了触达用户数翻倍的成绩,迅猛的 ...

  7. js倒计时小插件(兼容大部分浏览器)

    精确到天的倒计时 <script language="JavaScript"> <!-- // (c) Henryk Gajewski var urodz= ne ...

  8. 任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del

    任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del

  9. openstack nova fail to create vm

    2019-05-13 14:43:27.017 47547 ERROR nova.compute.manager [req-3f1af0ed-c342-4cf3-8e76-6963053a5227 8 ...

  10. Python——数据类型

    如果是C语言,Java使用一个变量之前需要声明,数字,字符,布尔等都有特定的声明方式,前端中常用的js中都要使用var,而python中直接用就行了 比如: 虽然是这样使用,但其实是当你给一个变量赋值 ...