以下案例均以编译安装为基础:
第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数–add-module=PATH指定路径添加
官方文档:https://github.com/openr esty/echo-nginx-module
[root@s2 src]# yum install git -y [root@s2 src]# git clone https://github.com/openresty/echo-nginx-module.git #克隆远程版本库 [root@s2 src]# cd nginx-1.12.2/ [root@s2 src]# ./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=/usr/local/src/echo-nginx-module [root@s2 src]# make && make install [root@s2 pc]# vim /apps/nginx/conf/conf.d/pc.conf location /main { index index.html; default_type text/html; echo "hello world,main-->"; 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@s2 pc]# /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 #重启nginx访问测试: [root@s2 pc]# systemctl restart nginx [root@s2 pc]# curl http://www.a.net/main hello world,main--> sub1 sub2 took 2.010 sec for total.定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容。
Nginx 的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,生产环境中通常将nginx日志转换为json日志,然后配合使用ELK做日志收集-统计-分析。
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并访问测试日志格式 {"@timestamp":"2019-02- 22T08:55:32+08:00","host":"192.168.7.102","clientip":"192.168.0.1","size":162,"respo nsetime":0.000,"upstreamtime":"-","upstreamhost":"- ","http_host":"www.a.net","uri":"/favicon.ico","domain":"www.a.net","xff": "-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0","status":"404"}官方文档: https://nginx.org/en/docs/http/ngx_ http_gzip_module.html
[root@s2 pc]# cp /apps/nginx/logs/access.log /data/nginx/html/pc/test.html #大文件测试压缩情况 [root@s2 pc]# echo "test1" > /data/nginx/html/pc/test1.html #小于1k的文件测试是否会压缩 [root@s2 pc]# vim /apps/nginx/conf/nginx.conf gzip on; #启用gzip压缩,默认关闭 gzip_comp_level 5; #压缩比由低到高从1到9,默认为1 gzip_min_length 1k; #gzip压缩的最小文件,小于设置值的文件将不会压缩 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_types text/html,不用显示指定,否则出错 gzip_vary on; #如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding” #重启Nginx并访问测试: curl --head --compressed URI Encoding: gzip #压缩传输官方文档: https://nginx.org/en/docs/http/ngx_http_ssl_ module.html
server { listen 80; listen 443 ssl; #当前虚拟主机使用使用的公钥文件,一般是crt文件 ssl_certificate /apps/nginx/certs/www.a.net.crt; #当前虚拟主机使用的私钥文件,一般是key文件 ssl_certificate_key /apps/nginx/certs/www.a.net.key; #配置ssl缓存 ssl_session_cache shared:sslcache:20m; #在各worker之间使用一个共享的缓存,缓存名称sslcache,缓存空间大小20m #客户端连接可以复用ssl session cache中缓存的有效时长,默认5m ssl_session_timeout 10m; location / { root "/data/nginx/html/mobile"; } } #重启Nginx并访问验证官方网站:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite rewrite主要是针对用户请求的URL或者是URI做具体处理,可配置在server、location、if中,编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一。
重要区别:永久重定向会缓存缓存域名解析记录(A记录),但是临时重定向不会缓存。 要求:因业务需要,将访问源域名http:a.com的请求永久重定向到https:a.com。
location / { root /data/nginx/html/pc; index index.html; if ($scheme = http ){ #未加条件判断,会导致死循环 rewrite / https:a.com permanent; #rewrite / https:a.com redirect; } #重启Nginx并访问测试:curl -L -k -i https://www.a.com要求:当用户访问到公司网站的时输入了一个错误的URL,可以将用户重定向至官网首页。
location / { root /data/nginx/html/pc; index index.html; if (!-f $request_filename) { #return 404 "linux35"; rewrite (.*) http://www.a.com/index.html; } #重启Nginx并访问测试:官方网站:https://nginx.org/en/docs/http/ngx_http_referer_module.html #valid_referers
[root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf location /images { root /data/nginx/html/pc; index index.html; valid_referers none blocked server_names *.example.com example.* www.example.org/galleries/ ~\.google\.; if ($invalid_referer) { return 403; } } #定义防盗链: location ^~ /images { root /data/nginx; index index.html; valid_referers none blocked server_names *.a.com www.a.* api.online.test/v1/hostlist ~\.google\. ~\.baidu\.; #定义有效的referer if ($invalid_referer) { #假如是使用其他的无效的referer访问: return 403; #返回状态码403 } } #重启Nginx并访问测试,验证是否提前状态码403官方文档:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 反向代理也叫reverse proxy,指的是代理外网用户的请求到内部的指定web服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。 环境准备: 192.168.7.102 #Nginx 代理服务器 192.168.7.103 #后端web A,Apache部署 192.168.7.104 #后端web B,Apache部署
官方网站:https://nginx.org/en/docs/ http/ngx_http_headers_module.html
location /web { proxy_pass http://192.168.7.103:80/; proxy_set_header clientip $remote_addr; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; add_header X-Via $server_addr; add_header X-Cache $upstream_cache_status; add_header X-Accel $server_name; } #验证头部信息:curl -I +UIR官方文档: https://nginx.org/en/docs/http/ngx_http_upstr eam_module.html
#自定义一组服务器,配置在http内 upstream webserver { #hash $request_uri consistent; #基于用户请求的uri做hash #ip_hash; #源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持 #least_conn; #最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器 #配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。 server 192.168.7.103:80 weight=1 fail_timeout=5s max_fails=3; #设置权重,默认为1。#给当前server设置最大活动链接数,默认为0表示没有限制。#对后端服务器连续监测失败多少次就标记为不可用。 server 192.168.7.104:80 weight=1 fail_timeout=5s max_fails=3; server 192.168.7.101:80 weight=1 fail_timeout=5s max_fails=3 backup; #设置为备份服务器,当所有服务器不可用时将重新启用次服务器。 } server { listen 80; server_name www.magedu.net; location / { index index.html index.php; root /data/nginx/html/pc; } location /web { index index.html; proxy_pass http://webserver/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP到报文头 部 } } #重启Nginx 并访问测试 [root@s2 ~]# curl http://www.magedu.net/web web1 192.168.7.103 [root@s2 ~]# curl http://www.magedu.net/web web2 192.168.7.104 #关闭192.168.7.103和192.168.7.104,测试nginx backup服务器可用性: [root@s4 ~]# while true;do curl http://www.magedu.net/web;sleep 1;done #客户端IP透传: #后端web服务器配置 1、Apache: [root@s4 ~]# vim /etc/httpd/conf/httpd.conf LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User- Agent}i\"" combined #重启apache访问web界面并验证apache日志: 192.168.7.104 192.168.7.102 - - [05/Mar/2019:00:36:03 +0800] "GET / HTTP/1.0" 200 19 "-" "curl/7.29.0" 192.168.0.1 192.168.7.102 - - [05/Mar/2019:00:40:46 +0800] "GET / HTTP/1.0" 200 19 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" 2、Nginx: [root@s1 conf.d]# cat /apps/nginx/conf/nginx.conf "$http_x_forwarded_for"' #默认日志格式就有此配置 重启nginx访问web界面并验证日志格式: 192.168.7.102 - - [04/Mar/2019:16:33:24 +0800] "GET // HTTP/1.0" 200 24 "-" "curl/7.29.0" "192.168.7.104" 192.168.7.102 - - [04/Mar/2019:16:40:51 +0800] "GET / HTTP/1.0" 200 24 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "192.168.0.1"官方文档:https://nginx.org/en/docs/stream/ngx_stream_core_module.html
通过fastcgi协议将指定的客户端请求转发至php-fpm处理
未完待续,敬请期待……