Nginx优化

一、实验环境

二、实验步骤

1.配置IP地址、关闭防火墙和防御子系统

2.安装nginx

yum -y install nginx

3.查看nginx的版本

nginx -V(显示版本详细信息)

nginx -v(显示版本号)

4.导入网页

mkdir /www

cd /www

将抓取的网页压缩后拖入

unzip www.baidu.com.zip

rm -rf www.baidu.com.zip

cd www.baidu.com/

mv * ../

cd ..

ls

5.修改配置文件

vim /etc/nginx/conf.d/www.conf

添加:

server {

        listen       80;

        server_name  www.yun71.com;

        location / {

        root   /www;  

        index  index.html index.htm;

        }      

}

6.添加hosts文件进行解析

echo "192.168.8.5 www.yun71.com" >> /etc/hosts

7.启动nginx

systemctl start nginx

验证:

firefox www.yun71.com

8.nginx目录索引

创建download下载目录

mkdir /www/download

cd /www/download

mkdir yun{71..73}

修改配置文件

vim /etc/nginx/conf.d/www.conf

添加:

location /download {

        root   /www;

        autoindex on;

        charset utf-8,gbk;

        autoindex_exact_size on;

        autoindex_localtime on;

}

重载nginx

systemctl reload nginx

测试:

firefox www.yun71.com/download

9.nginx状态监控

修改配置文件

vim /etc/nginx/conf.d/www.conf

添加:

location /status {

        stub_status;

        access_log off;

}

重载nginx

systemctl reload nginx

测试:

firefox www.yun71.com/status

使用ab压测工具测试

yum -y install httpd-tools

ab -c 1000 -n 10000 http://www.yun71.com/

10.nginx基于IP的访问控制

修改配置文件

vim /etc/nginx/conf.d/www.conf

添加:

        allow 192.168.8.0/24;

        deny all;

重载nginx

systemctl reload nginx

11.nginx基于用户的访问控制

yum -y install httpd-tools

修改配置文件

vim /etc/nginx/conf.d/www.conf

添加:

        auth_basic "input your passwd:";

        auth_basic_user_file /etc/nginx/.auth_conf;

重载nginx

systemctl reload nginx

测试:

firefox www.yun71.com/status

输入用户名和密码登录

12.nginx的访问限制

修改配置文件

vim /etc/nginx/nginx.conf

添加:

limit_conn_zone $binary_remote_addr zone=addr:10m;

vim /etc/nginx/conf.d/www.conf

添加:

        limit_conn addr 1;

重载nginx

systemctl reload nginx

13.nginx日志格式

修改配置文件

vim /etc/nginx/nginx.conf

添加:

$request_length

重载nginx

systemctl reload nginx

测试:

访问网页后查看日志内容

cat tail /var/log/nginx/access.log

14.nginx的location

语法详解

语法规则: location [=|~|~*|^~] /uri/ { … }

下列以优先级从高到低排序

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。

~ 开头表示区分大小写的正则匹配                   

~* 开头表示不区分大小写的正则匹配            

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则

/ 通用匹配,任何请求都会匹配到。

例子:测试匹配符的优先级

cd /etc/nginx/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 ~* /";

        }

}

保存退出

客户端修改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 "启用访问控制";

}   

(注意:以上主要用于网页的动、静分离)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值