基于nginx模块ngx_http_auth_basic_module实现,在编译安装nginx的时候须要添加编译参数--with-http_stub_status_module,不然配置完成以后监测会是提示语法错误。javascript
查看是否加载了ngx_http_auth_basic_module模块 [root@CentOS7 ~]#/apps/nginx/sbin/nginx -V nginx version: nginx/1.14.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-http_perl_module
[root@CentOS7 ~]#vim /apps/nginx/conf/nginx.conf location /nginx_status { stub_status; allow 192.168.36.0/24; deny all; } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问测试php
[root@CentOS-Test ~]#curl 192.168.36.104/nginx_status Active connections: 1 server accepts handled requests 124 124 223 # 这三个数字分别对应accepts,handled,requests三个值 Reading: 0 Writing: 1 Waiting: 0 Active connections: 当前处于活动状态的客户端链接数,包括链接等待空闲链接数。 accepts:统计总值,Nginx自启动后已经接受的客户端请求的总数。 handled:统计总值,Nginx自启动后已经处理完成的客户端请求的总数,一般等于accepts,除非有因 worker_connections限制等被拒绝的链接。 requests:统计总值,Nginx自启动后客户端发来的总的请求数。 Reading:当前状态,正在读取客户端请求报文首部的链接的链接数。 Writing:当前状态,正在向客户端发送响应报文过程当中的链接数。 Waiting:当前状态,正在等待客户端发出请求的空闲链接数,开启 keep-alive的状况下,这个值等于active – (reading+writing),
添加第三方模块:echo-nginx-module [root@CentOS7 ~]#yum install git -y [root@CentOS7 ~]#git clone https://github.com/openresty/echo-nginx-module.git [root@CentOS7 ~]#cd nginx-1.14.2/ [root@CentOS7 nginx-1.14.2]#./configure \ # 从新编译安装 > --prefix=/apps/nginx \ > --user=nginx --group=nginx \ > --with-http_ssl_module \ > --with-http_v2_module \ > --with-http_realip_module \ > --with-http_stub_status_module \ > --with-http_gzip_static_module \ > --with-pcre \ > --with-stream \ > --with-stream_ssl_module \ > --with-stream_realip_module \ > --with-http_perl_module \ > --add-module=/root/echo-nginx-module # 添加echo模块 [root@CentOS7 nginx-1.14.2]#make && make install # make安装
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location /main { index index.html; default_type text/html; echo_reset_timer; echo_location /sub1; echo_location /sub2; echo "took $echo_timer_elapsed sec for total."; } location /sub1 { echo_sleep 1; echo sub1; } location /sub2 { echo_sleep 1; echo sub2; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问测试css
[root@CentOS7 ~]#curl www.darius.com/main sub1 sub2 took 2.008 sec for total.
nginx的变量能够在配置文件中引用,做为功能判断或者日志等场景使用,变量能够分为内置变量和自定义变量,内置变量是由nginx模块自带,经过变量能够获取到众多的与客户端访问相关的值。html
$remote_addr; # 存放了客户端的地址,注意是客户端的公网IP,也就是一家人访问一个网站,则会显示为路由器的公网IP。
$args; # 变量中存放了URL中的指令,例如http://www.darius.com/main/index.do? id=20190221&partner=search中的id=20190221&partner=search
$document_root; # 保存了针对当前资源的请求的系统根目录,如/apps/nginx/html
$document_uri; # 保存了当前请求中不包含指令的URI,注意是不包含请求的指令,好比 http://www.darius.com/main/index.do?id=20190221&partner=search会被定义为/main/index.do
$host; #存放了请求的host名称。
$http_user_agent; # 客户端浏览器的详细信息
$http_cookie; # 客户端的cookie信息
limit_rate 10240; echo $limit_rate; # 若是nginx服务器使用limit_rate配置了显示网络速率,则会显示,若是没有设置, 则显示0
$remote_port; # 客户端请求Nginx服务器时随机打开的端口,这是每一个客户端本身的端口
$remote_user; # 已经通过Auth Basic Module验证的用户名
$request_body_file; # 作反向代理时发给后端服务器的本地资源的名称
$request_method; # 请求资源的方式,GET/PUT/DELETE等
$request_filename; # 当前请求的资源文件的路径名称,由root或alias指令与URI请求生成的文件绝对路径,如/apps/nginx/html/main/index.html
$request_uri; # 包含请求参数的原始URI,不包含主机名,如:/main/index.do?id=20190221&partner=search
$scheme; # 请求的协议,如ftp,https,http等
$server_protocol; # 保存了客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr; # 保存了服务器的IP地址
$server_name; # 请求的服务器的主机名
$server_port; # 请求的服务器的端口号
假如须要自定义变量名称和值,使用指令set $variable value;,则方法以下: set $name magedu; echo $name; set $my_port $server_port; echo $my_port; echo "$server_name:$server_port";
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location /main { index index.html; default_type text/html; echo $request_uri; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload [root@CentOS7 ~]#curl www.darius.com/main /main [root@CentOS7 ~]#curl www.darius.com/main/xxx /main/xxx
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location /main { index index.html; default_type text/html; set $name Darius; echo $name; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload [root@CentOS7 ~]#curl www.darius.com/main Darius
访问日志是记录客户端即用户的具体请求内容信息,全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和记录日志的level,所以有着本质的区别,并且Nginx的错误日志通常只有一个,可是访问日志能够在不一样server中定义多个,定义一个日志须要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容。java
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 logs/access.log main;
[root@CentOS7 ~]#vim /apps/nginx/conf/nginx.conf log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; access_log /apps/nginx/logs/access_json.log access_json;
从新加载nginx并访问测试日志格式node
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload [root@CentOS7 ~]#tail -f /apps/nginx/logs/access_json.log {"@timestamp":"2019-05-30T18:58:23+08:00","host":"192.168.36.104","clientip":"192.168.36.110","size":15,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.36.104","uri":"/index.html","domain":"192.168.36.104","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"200"}
[root@CentOS7 logs]#cat nginx_json.py #!/usr/bin/env python #coding:utf-8 status_200= [] status_404= [] with open("access_json.log") as f: for line in f.readlines(): line = eval(line) if line.get("status") == "200": status_200.append(line.get) elif line.get("status") == "404": status_404.append(line.get) else: print("状态码 ERROR") f.close() print "状态码200的有--:",len(status_200) print "状态码404的有--:",len(status_404) # 保存日志文件到指定路径并进测试: [root@CentOS7 ~]# python nginx_json.py .... 状态码200的有--: 403428 状态码404的有--: 125712
Nginx支持对指定类型的文件进行压缩而后再传输给客户端,并且压缩还能够设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于下降出口带宽的利用率,下降企业的IT支出,不过会占用相应的CPU资源。Nginx对文件的压缩功能是依赖于模块ngx_http_gzip_modulepython
# 启用或禁用gzip压缩,默认关闭 gzip on | off; # 压缩比由低到高从1到9,默认为1 gzip_comp_level level; # 禁用IE6 gzip功能 gzip_disable "MSIE [1-6]\."; # gzip压缩的最小文件,小于设置值的文件将不会压缩 gzip_min_length 1k; # 启用压缩功能时,协议的最小版本,默认HTTP/1.1 gzip_http_version 1.0 | 1.1; # 指定Nginx服务须要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k; gzip_buffers number size; # 指明仅对哪些类型的资源执行压缩操做;默认为gzip_types text/html,不用显示指定,不然出错 gzip_types mime-type ...; # 若是启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding” gzip_vary on | off;
配置文件修改nginx
gzip on; gzip_comp_level 5; gzip_min_length 1; gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问测试git
[root@CentOS-Test ~]#curl --head --compressed http://www.darius.com/test1.html HTTP/1.1 200 OK Server: nginx Date: Thu, 30 May 2019 11:26:49 GMT Content-Type: text/html Last-Modified: Thu, 30 May 2019 11:26:31 GMT Connection: keep-alive Vary: Accept-Encoding ETag: W/"5cefbde7-720" Content-Encoding: gzip
Web网站的登陆页面都是使用https加密传输的,加密数据以保障数据的安全,HTTPS可以加密信息,以避免敏感信息被第三方获取,因此不少银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议,HTTPS实际上是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会经过TLS进行加密,因此传输的数据都是加密后的数据。github
nginx 的https 功能基于模块ngx_http_ssl_module实现,所以若是是编译安装的nginx要使用参数ngx_http_ssl_module开启ssl功能,可是做为nginx的核心功能,yum安装的nginx默认就是开启的,编译安装的nginx须要指定编译参数--with-http_ssl_module开启
ssl on | off; # 为指定的虚拟主机配置是否启用ssl功能,此功能在1.15.0废弃,使用listen [ssl]替代。 ssl_certificate /path/to/file; # 当前虚拟主机使用使用的公钥文件,通常是crt文件 ssl_certificate_key /path/to/file; # 当前虚拟主机使用的私钥文件,通常是key文件 ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; # 支持ssl协议版本,早期为ssl如今是TSL,默认为后三个 ssl_session_cache off | none | [builtin[:size]] [shared:name:size]; # 配置ssl缓存 off: 关闭缓存 none: 通知客户端支持ssl session cache,但实际不支持 builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有 [shared:name:size]:在各worker之间使用一个共享的缓存,须要定义一个缓存名称和缓存空间大小,一兆能够存储4000个会话信息,多个虚拟主机可使用相同的缓存名称。 ssl_session_timeout time; # 客户端链接能够复用ssl session cache中缓存的有效时长,默认5m
# 建立自签名CA证书 [root@CentOS7 ~]#cd /apps/nginx/ [root@CentOS7 nginx]#mkdir certs [root@CentOS7 nginx]# cd certs/ [root@CentOS7 certs]#openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt # 自签名CA证书 Generating a 4096 bit RSA private key .............................................................................................................................................................................................................................................................................................................................................++ ........................................................................................++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN # 国家代码 State or Province Name (full name) []:BeiJing # 省份 Locality Name (eg, city) [Default City]:BeiJing # 城市名称 Organization Name (eg, company) [Default Company Ltd]:magedu.com # 公司名称 Organizational Unit Name (eg, section) []:magedu # 部门 Common Name (eg, your name or your server's hostname) []:M36 # 通用名称 Email Address []: # 邮箱 [root@CentOS7 certs]#ll ca.crt -rw-r--r-- 1 root root 2009 5月 30 19:34 ca.crt # 建立自定义额key和csr文件 [root@CentOS7 certs]#openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.darius.com.key -out www.darius.com.csr Generating a 4096 bit RSA private key ............++ ..........................++ writing new private key to 'www.darius.com.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BeiJing Locality Name (eg, city) [Default City]:BeiJing Organization Name (eg, company) [Default Company Ltd]:magedu.com Organizational Unit Name (eg, section) []:magedu Common Name (eg, your name or your server's hostname) []:M36 Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: [root@CentOS7 certs]#ll 总用量 16 -rw-r--r-- 1 root root 2009 5月 30 19:34 ca.crt -rw-r--r-- 1 root root 3272 5月 30 19:34 ca.key -rw-r--r-- 1 root root 1695 5月 30 19:38 www.darius.com.csr -rw-r--r-- 1 root root 3272 5月 30 19:38 www.darius.com.key # 证书签发 [root@CentOS7 certs]#openssl x509 -req -days 3650 -in www.darius.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.darius.com.crt Signature ok subject=/C=CN/ST=BeiJing/L=BeiJing/O=magedu.com/OU=magedu/CN=M36 Getting CA Private Key 验证证书内容 [root@CentOS7 certs]#openssl x509 -in www.darius.com.crt -noout -text Certificate: Data: Version: 1 (0x0) Serial Number: fe:15:2c:1a:9d:a5:df:f5 Signature Algorithm: sha256WithRSAEncryption Issuer: C=CN, ST=BeiJing, L=BeiJing, O=magedu.com, OU=magedu, CN=M36 Validity Not Before: May 30 11:42:02 2019 GMT Not After : May 27 11:42:02 2029 GMT Subject: C=CN, ST=BeiJing, L=BeiJing, O=magedu.com, OU=magedu, CN=M36 Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (4096 bit)
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; listen 443 ssl; ssl_certificate /apps/nginx/certs/www.darius.com.crt; ssl_certificate_key /apps/nginx/certs/www.darius.com.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location / { index index.html; root /data/nginx/html/pc; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问测试