nginx
一、配置官方yum源
[root@base2 ~]# systemctl stop firewalld
[root@base2 ~]# setenforce 0
[root@base2 ~]# cd /etc/yum.repos.d/
[root@base2 yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@base2 ~]# yum -y install nginx
[root@base2 ~]# nginx -V
二、nginx新版本的配置文件
全局配置文件
/etc/nginx/nginx.conf
虚拟主机配置文件
/etc/nginx/conf.d/*.conf
例子:
1.使用域名搭建一台虚拟主机
[root@base2 ~]# mkdir /www
复制网页代码到/www目录下
[root@base2 www]# ls
bikes.html css images js products.html single.html
contact.html download index.html login.html register.html 说明.htm
关于网页代码可以上中国站长站去下载。
2.进入到/etc/nginx/conf.d/目录下
[root@base2 www]# cd /etc/nginx/conf.d/
目录下有个default,conf文件,把它移出去备份。然后直接编辑新的配置文件;在server模块中添加。
[root@base2 conf.d]# vim www.conf
server{
listen 80;
server_name www.yundong.com;
location / {
root /www;
index index.html index.htm;
}
}
[root@base2 conf.d]# systemctl restart nginx
然后用客户端或者本机的浏览器进行访问验证。
三、nginx目录索引(autoindex自动索引模块)
nginx默认不启用目录索引,更不允许列出网站目录提供下载。
模块配置方法
Syntax: autoindex on | off; 索引功能的开或关
Default: autoindex off; 默认关闭
Context: http, server, location 场景:全局、某个虚拟主机、某个虚拟主机的目录
例如:在www网站下,创建download下载目录,索引显示;在server模块中添加。
[root@base2 conf.d]# mkdir /www/download
[root@base2 conf.d]# cd /www/download
[root@base2 download]# touch {1..9}
[root@base2 download]# ls
1 2 3 4 5 6 7 8 9
[root@base2 download]# echo "你" > 1
[root@base2 download]# echo "好" > 2
[root@base2 download]# echo "吗" > 3
[root@base2 download]# echo "我" > 4
[root@base2 download]# echo "依" > 5
[root@base2 download]# echo "然" > 6
[root@base2 download]# echo "爱" > 7
[root@base2 download]# echo "着" > 8
[root@base2 download]# echo "你" > 9
[root@base2 download]# cd /etc/nginx/conf.d
[root@base2 conf.d]# vim www.conf
server{
listen 80;
server_name www.yundong.com;
location / {
root /www;
index index.html index.htm;
}
location /download { #添加内容
root /www;
autoindex on; #启用索引显示
charset utf-8,gbk; #字符编码为中文
autoindex_exact_size on; #显示文件大小
autoindex_localtime on; #显示文件创建时间
}
}
[root@base2 conf.d]# systemctl restart nginx
然后访问192.168.229.134/download进行验证
四、nginx状态监控(status模块)
模块配置方法
Syntax: stub_status; 启用状态化追踪
Default: — 默认关闭
Context: server, location 场景:
例子:针对www网站,启用状态化追踪;在server模块中添加。
[root@base2 conf.d]# vim www.conf
server{
listen 80;
server_name www.yundong.com;
location / {
root /www;
index index.html index.htm;
}
location /download {
root /www;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size on;
autoindex_localtime on;
}
location /status { #新添加内容
stub_status; #启用状态化追踪
access_log off; #关闭status的日志记录
}
}
[root@base2 conf.d]# systemctl restart nginx
然后访问192.168.229.134/status显示如下内容
Active connections: 8 #当前活跃的连接数;
server accepts handled requests #accepts:当前的总tcp连接数;handled:成功的连接数;requests:总HTTP请求数。
8 8 28
Reading: 0 Writing: 1 Waiting: 7 #读、写、等待。
五、nginx基于IP的访问控制(access模块)
例子:仅允许内部网段或VPN访问status,在server模块中的status处添加;
[root@base2 conf.d]# vim www.conf
server{
listen 80;
server_name www.yundong.com;
location / {
root /www;
index index.html index.htm;
}
location /download {
root /www;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size on;
autoindex_localtime on;
}
location /status {
stub_status;
access_log off;
allow 192.168.1.0/24; #新添加内容,表示仅允许1.0网段访问;如果是单个IP,可以设置为192.168.229.128。
deny all; #拒绝其他所有
}
}
[root@base2 conf.d]# systemctl restart nginx
这样的话,只有被允许的才可以访问此网站。
六、nginx基于用户的访问控制(auth模块)
例子:设置访问/status,用户密码验证;在server模块中的status处设置;
首先需要安装工具
[root@base2 conf.d]# yum -y install httpd-tools
[root@base2 conf.d]# htpasswd -b -c /etc/nginx/.auth_conf admin 123456
Adding password for user admin
[root@base2 conf.d]# vim www.conf
将server模块中的status处修改为:
location /status {
stub_status;
access_log off;
auth_basic "input your passwd:"; 用户验证启用描述
auth_basic_user_file /etc/nginx/.auth_conf; 用户验证文件路径
}
[root@base2 conf.d]# systemctl restart nginx
然后访问192.168.229.134/status,会出现如下内容,输入账号密码后才可以进入
七、nginx的访问限制
limit_conn_module:连接频率限制;
例子:
首先在/etc/nginx/nginx.conf下的http模块添加如下内容
[root@base2 conf.d]# vim /etc/nginx/nginx.conf
在http模块下面添加下面的内容
http {
limit_conn_zone $binary_remote_addr zone=addr:10m; #创建zone区域名为addr,大小10m,保存客户端的二进制ip;
然后在/etc/nginx/conf.d/www.conf下的server模块里的添加如下内容;
[root@base2 conf.d]# vim /etc/nginx/conf.d/www.conf
server {
location /download/ {
limit_conn addr 1; #一个ip同一时间点只允许建立一个连接
}
}
[root@base2 conf.d]# systemctl restart nginx
limit_req_module:请求频率限制;
例子:
首先在/etc/nginx/nginx.conf下的http模块添加如下内容
[root@base2 conf.d]# vim /etc/nginx/nginx.conf
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次;
然后在/etc/nginx/conf.d/www.conf下的server模块里的添加如下内容;
[root@base2 conf.d]# vim /etc/nginx/conf.d/www.conf
server {
location /download {
limit_req zone=one burst=5; #调用请求速率区域,另外设置额外突发5次;
}
}
[root@base2 conf.d]# systemctl restart nginx
八、nginx日志格式
[root@base2 ~]# vim /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request"' #定义日志输出格式
main
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access_log main; #调用日志格式main
}
1.nginx日志格式的变量
$remote_addr #记录客户端的ip地址
$remote_user #记录客户端的用户名
$time_local #通用的时间格式
$time_iso8601 #iso8601时间格式
$request #请求的方法和请求的HTTP协议
$status #请求状态码
$body_bytes_sent #服务器回应的字节数,不包含头部大小
$bytes_sent #服务器回应的总字节数
$msec #日志写入时间,单位为秒,精度为毫秒
$http_referer #记录链接访问源地址
$http_user_agent #记录客户端浏览器信息
$http_x_forwarded_for #跨越代理服务器,记录客户机的原始ip
$request_length #请求包的长度(请求头+请求正文)
$request_time #请求花费的时间,单位为秒,精度为毫秒
2.nginx的location
语法规则:
location [=|~|~*|^~] /uri/ { … }
下列是各种字符匹配的规则
=:开头表示精确匹配;
^~:开头表示uri以某个常规字符串开头,理解为匹配url路径即可;
~:开头表示区分大小写的正则匹配;
~*:开头表示不区分大小写的正则匹配;
!~和!~*:分别为区分大小写不匹配及不区分大小写不匹配的正则;
/:通用匹配,任何请求都会匹配到。
例子:测试匹配符的优先级从高到低
[root@base2 ~]# cd /etc/nginx/conf.d
[root@base2 conf.d]# vim test.conf
server {
listen 80;
server_name test.benet.com;
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~ /";
}
location ~* / {
default_type text/html;
return 200 "location ~* /";
}
location / {
default_type text/html;
return 200 "location /";
}
}
[root@base2 conf.d]# systemctl restart nginx
客户端修改hosts文件,测试访问
[root@base2 conf.d]# vim /etc/hosts
真实企业场景配置:*
#通用匹配,任何请求都会匹配到。
location / {
}
#严格区分大小写,匹配.php结尾
location ~ \.php$ {
fastcgi_pass http://127.0.0.1:9000;
}
#严格区分大小写,匹配.jsp结尾
location ~ \.jsp$ {
proxy_pass http://127.0.0.1:8080;
}
#不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$ {
default_type text/html;
return 403 "启用访问控制";
}