目录
一、高级配置
1.1. 网页的状态页
1.2.Nginx 第三方模块
ehco 模块 打印
1.3.变量
1.3.1 内置变量
1.3.2自定义变量
1.4.Nginx压缩功能
1.5.https 功能
1.6.自定义图标
一、高级配置
1.1. 网页的状态页
基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module,否则配置完成之后监测会是提示语法错误注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态。
配置示例: location /nginx_status { #stub_status; auth_basic "auth login"; auth_basic_user_file /apps/nginx/conf/.htpasswd; allow 192.168.0.0/16; allow 127.0.0.1; deny all; }
实际上只需要添加
location location /nginx_status { stub_status; }
状态页用于输出nginx的基本状态信息: #输出信息示例: Active connections: 291 server accepts handled requests 16630948 16630948 31070465 上面三个数字分别对应accepts,handled,requests三个值 Reading: 6 Writing: 179 Waiting: 106
Active connections: #当前处于活动状态的客户端连接数,包括连接等待空闲连接数=reading+writing+waiting
accepts: #统计总值,Nginx自启动后已经接受的客户端请求的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求总数,通常等于accepts,除非有因worker_connections限制等被拒绝的连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数。
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于active – (reading+writing)
1.2.Nginx 第三方模块
ehco 模块 打印
安装git
yum安装
yum install git -y
编译安装
1. git clone https://github.com/openresty/echo-nginx-module.git
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 --add-module=/opt/echo-nginx-module-master
make && make install
安装好后
再试一次,没有报错,重启一下
1.3.变量
1.3.1 内置变量
$remote_addr; #存放了客户端的地址,注意是客户端的公网IP
$proxy_add_x_forwarded_for #此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没有X-Forwarded-For,就使用$remote_addrthe “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable. 客户机 代理1 代理2 nginx服务器 $proxy_add_x_forwarded_for: 在代理1 上存的是 客户机的ip $proxy_add_x_forwarded_for: 在代理2 上存的是 客户机的ip,代理1的ip 用逗号隔开 $proxy_add_x_forwarded_for: nginx 上存的是 客户机的ip,代理1的ip,代理2的ip
$args; #变量中存放了URL中的参数,例如:http://www.kgc.org/main/index.do?id=20190221&partner=search #返回结果为: id=20190221&partner=search 存放的就是这个
select * from table where id=20190221
$document_root; #保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html
$document_uri; #保存了当前请求中不包含参数的URI,注意是不包含请求的指令,比 如:http://www.kgc.org/main/index.do?id=20190221&partner=search会被定义为/main/index.do #返回结果为:/main/index.do
$host; #存放了请求的host名称
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; https:// www.baidu.com/main/index.do?id=20190221&partner=search #包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,例如:/main/index.do?id=20190221&partner=search
$scheme; #请求的协议,例如:http,https,ftp等
$server_protocol; #保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr; #保存了服务器的IP地址
$server_name; #请求的服务器的主机名
$server_port; 443 https #请求的服务器的端口号
$http_
$http_user_agent; #客户端浏览器的详细信息
$http_cookie; #客户端的cookie信息
$cookie_
$http_
#示例: echo $http_user_agent; echo $http_host;
$sent_http_
$arg_
location /main { index index.html; default_type text/html; echo "hello world,main-->"; echo $remote_addr; echo $args; echo $document_root; echo $document_uri; echo $host; echo $http_user_agent; echo $http_cookie; echo $request_filename; echo $scheme; echo $scheme://$host$document_uri?$args; }
实际操作
location /main { index index.html; default_type text/html; echo "hello world,main-->"; echo $remote_addr; echo $args; echo $arg_user echo $document_root; echo $document_uri; echo $host; echo $http_user_agent; echo $http_cookie; echo $request_filename; echo $scheme; echo $scheme://$host$document_uri?$args; }
加入此段后 去主机2上进行测试 curl http://www.pc.com/main curl 'http://www.pc.com/main?user=zhou&title=cto'
curl -b uid=100 'http://www.pc.com/main?user=zhou&title=cto' -b 加上cookie
1.3.2自定义变量
假如需要自定义变量名称和值,使用指令set $variable value;
语法格式:
Syntax: set $variable value; Default: — Context: server, location, if
范例:
set $name kgc; echo $name; set $my_port $server_port; echo $my_port; echo "$server_name:$server_port";
输出信息如下
[root@centos6 ~]#curl www.kgc.org/main kgc 80 www.kgc.org:80
实际例子: location /test { set $name kgc; echo $name; set $my_port $server_port; echo $my_port; }
1.4.Nginx压缩功能
支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module
#启用或禁用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;
#预压缩,先压缩好,不用临时压缩,消耗cpu gzip_static on | off;
太小的文件没必要压缩,压缩说不定变大了
实际操作
server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; gzip on; gzip_comp_level 9; gzip_min_length 1k; gzip_vary on;
开启预压缩
gzip 需要的访问的文件 server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; gzip on; gzip_comp_level 9; gzip_min_length 1k; gzip_vary on; gzip_static on; 访问 www.pc.com/文件名
1.5.https 功能
Web网站的登录页面都是使用https加密传输的,加密数据以保障数据的安全,HTTPS能够加密信息,以免敏感信息被第三方获取,所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议,HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据。
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]替代 listen 443 ssl;
ssl_certificate /path/to/file; #指向包含当前虚拟主机和CA的两个证书信息的文件,一般是crt文件
ssl_certificate_key /path/to/file; #当前虚拟主机使用的私钥文件,一般是key文件
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; #支持ssl协议版本,早期为ssl现在是TLS,默认为后三个
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
自签名证书
所有的证书需要放在一起不能移开
bash certificate.sh cat kgc.com.crt ca.crt > www.kgc.com.crt mv kgc.com.key www.kgc.com.key ll www.kgc.com*
server{ listen 80; listen 443 ssl; ssl_certificate /apps/nginx/conf.d/ssl/www.kgc.com.crt; ssl_certificate_key /apps/nginx/conf.d/ssl/www.kgc.com.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; server_name www.kgc.com; root /data/nginx/pc/; gzip on; gzip_comp_level 9; gzip_min_length 1k; gzip_static on; }
location / { root /apps/nginx/html; if ( $scheme = http) { rewrite ^/(.*)$ https://www.kgc.com/$1 redirect; } }
1.6.自定义图标
favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错。
#方法一:服务器不记录访问日志: location = /favicon.ico { log_not_found off; access_log off; } #方法二:将图标保存到指定目录访问: #location ~ ^/favicon\.ico$ { location = /favicon.ico { root /data/nginx/html/pc/images; expires 365d; #设置文件过期时间 }
【定制小图标】 wget www.baidu.com/favicon.ico 放到主目录就可以了
不生效可以重新打开浏览器
相关文章
发表评论