Nginx的常用模块配置(一)
1、http_stub_status_module模块
作用:用来显示当前服务器的连接请求状态信息
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location /mystatus { //添加此location选项,定义状态信息页面;
stub_status; //添加状态信息关键字;
}
[[email protected] ~]# nginx -c /etc/nginx/nginx.conf -s reload //对服务做重载
[[email protected] ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",1756,6),("nginx",4238,6))
[[email protected] ~]#
Active connections: 2 //活动状态的连接数
server accepts handled requests //accept:已接收的客户端请求数量;
handled:已处理的客户端请求数量
requests:客户端发送的总请求数
2 2 1
Reading: 0 Writing: 1 Waiting: 1 // Reading:正在读取客户端请求报文首部的连接数;
Writing:正在向客户端发送响应报文的连接数;
Waiting:正在等待客户端发送请求报文的空闲连接数;
2、http_random_index_module模块
作用:客户端发起请求时,在页面目录中随机选择一个主页返回给用户
[[email protected] ~]# mkdir /opt/test/web -pv
mkdir: 已创建目录 "/opt/test"
mkdir: 已创建目录 "/opt/test/web"
[[email protected] ~]# echo "Test Page web1" > /opt/test/web/index1.html
[[email protected] ~]# echo "Test Page web2" > /opt/test/web/index2.html
[[email protected] ~]# echo "Test Page web3" > /opt/test/web/index3.html
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
location / {
root /opt/test/web; //随机主页的目录存放位置;
random_index on; //开启随机功能;
index index.html index.htm;
}
[[email protected] ~]# nginx -c /etc/nginx/nginx.conf -s reload //重载nginx服务;
[[email protected] ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",1756,6),("nginx",4281,6))
[[email protected] ~]#
3、http_sub_module模块
作用:实现对客户端请求资源内容的替换功能
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
location / {
root /opt/test/web;
sub_filter 'Page' 'Hello Jyy'; //把页面中的“Page”内容替换为“Hello Jyy”
sub_filter_once on; //申明为全文替换;
index index.html index.htm;
}
[[email protected] ~]# nginx -c /etc/nginx/nginx.conf -s reload
[[email protected] ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",1756,6),("nginx",4311,6))
[[email protected] ~]#
4、http_access_module模块
作用:实现基于客户端ip地址做访问控制的功能
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
location / {
root /opt/test/web;
deny 192.168.126.1;
allow all;
index index.html index.htm;
}
[[email protected] ~]# nginx -c /etc/nginx/nginx.conf -s reload
[[email protected] ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",1756,6),("nginx",4368,6))
[[email protected] ~]#
403状态码表示请求被服务器端拒绝;用其他主机访问测试
[[email protected] ~]# curl http://192.168.126.131/index1.html
Test Page web1
//请求成功
5、http_auth_basic_module模块
作用:客户端访问服务器指定资源时需要输入用户名和密码信息进行认证
[[email protected] ~]# htpasswd -c /etc/nginx/auth_user_conf jyy //创建一个存放用户名和密码的文件
New password:
Re-type new password:
Adding password for user jyy
[[email protected] ~]# cat /etc/nginx/auth_user_conf
jyy:BvGdPQb7iKsOk
[[email protected] ~]#
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
location / {
root /opt/test/web;
auth_basic "Auth access test! input your password"; //用户进行认证时候的自定义描述信息;
auth_basic_user_file /etc/nginx/auth_user_conf; //指明认证用户的配置文件位置;
}
[[email protected] ~]# nginx -c /etc/nginx/nginx.conf -s reload
[[email protected] ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",1756,6),("nginx",4419,6))
[[email protected] ~]#
6、http_gzip_module模块
作用:客户端请求的文件较大时,可以对资源压缩后返回给客户端,节省网络带宽
[[email protected] web]# pwd
/opt/test/web
[[email protected] web]# ll
总用量 92
-rw-r--r--. 1 root root 15 3月 25 17:22 index1.html
-rw-r--r--. 1 root root 15 3月 25 17:22 index2.html
-rw-r--r--. 1 root root 15 3月 25 17:22 index3.html
-rw-r--r--. 1 root root 79044 3月 25 18:04 miao.jpg //这是一个测试图片文件
[[email protected] web]#
首先我们不开启压缩功能。查看文件传输的大小(181byte)
下面修改nginx的配置文件开启gzip压缩功能
location / {
root /opt/test/web;
gzip on; //开启压缩功能;
gzip_http_version 1.1; //压缩的版本;
gzip_comp_level 2; //压缩的等级;
gzip_type text/plain application/javascript text/css application/xml text/javascripts application/x-httpd-php
image/jpg image/gif image/png; //支持压缩的资源类型;
}
[[email protected] ~]# nginx -c /etc/nginx/nginx.conf -s reload
[[email protected] ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",1756,6),("nginx",4419,6))
[[email protected] ~]#
再次查看请求的资源大小变化