Nginx 网页优化 安全优化 防盗链

Nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD

Nginx优化

隐藏版本号

在生产环境中,暴露nginx的版本号是非常不安全的,因为黑客可以利用nginx版本的漏洞对生产环境进行攻击。故隐藏nginx的版本号非常必要。

[root@5centos /]# nginx -v
nginx version: nginx/1.12.2

在这里插入图片描述

修改配置文件更改版本号

[root@5centos /]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;   ##几个模块里都可以加

在这里插入图片描述

[root@5centos /]# curl -I 20.0.0.5
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 10 Aug 2020 09:23:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 09:15:56 GMT
Connection: keep-alive
ETag: "5f31104c-264"
Accept-Ranges: bytes

编译时修改

[root@5centos bianyi]# ls
nginx-1.12.2  nginx-1.12.2.tar.gz
[root@5centos bianyi]# cd nginx-1.12.2/
[root@5centos nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@5centos nginx-1.12.2]# cd src/core/
[root@5centos nginx-1.12.2]# cd src/core/
[root@5centos core]# vim nginx.h
#define NGINX_VERSION      "100.200.300"   ##随便改,嘻嘻

在这里插入图片描述

[root@5centos /]# curl -I 20.0.0.5
HTTP/1.1 200 OK
Server: nginx/100.200.300
Date: Mon, 10 Aug 2020 09:34:21 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 09:31:30 GMT
Connection: keep-alive
ETag: "5f3113f2-264"
Accept-Ranges: bytes

设置网页缓存时间

当nginx将静态资源返回给客户端后,可设置缓存的时间,以方便在日后进行相同过内容的请求时直接返回,避免重复请求,加快了访问速度。

http段、server段、location段均可添加expires参数,设置缓存时间

[root@5centos /]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    expires     5m;    ##我设置了五分钟

[root@5centos /]# nginx -t   ##检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@5centos /]# systemctl restart nginx.service 

在这里插入图片描述

连接超时设置

连接超时设置,可以控制连接访问时间,避免同一个客户长时间占用连接,造成资源的浪费。
Keepalive_timeout
​ 设置连接保持时间,默认75秒,可在http段、server段、location段进行配置
Client_header_timeout
​ 指定等待客户端发送请求头的超时时间
Client_body_timeout
​ 设置请求体读超时时间

在HTTP添加超时参数

[root@5centos /]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    expires     5m;

    keepalive_timeout 60;
    client_header_timeout 20;
    client_body_timeout 15;
[root@5centos /]# systemctl restart nginx.service 

在这里插入图片描述

更改运行进程数

一般有几核,最大就分几,我这边用的虚拟机,生产环境中,大部分的处理器使用 英特尔 至强 CPU(INTEL XEON),配置很强很到位,我列出的算是低端的了
在这里插入图片描述在这里插入图片描述

[root@5centos /]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  4;
[root@5centos /]# systemctl restart nginx
[root@5centos /]# 
[root@5centos /]# ps aux |grep nginx
root      81129  0.0  0.0  20540   604 ?        Ss   19:00   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     81130  0.0  0.0  23068  1380 ?        S    19:00   0:00 nginx: worker process
nginx     81131  0.0  0.0  23068  1380 ?        S    19:00   0:00 nginx: worker process
nginx     81132  0.0  0.0  23068  1380 ?        S    19:00   0:00 nginx: worker process
nginx     81133  0.0  0.0  23068  1380 ?        S    19:00   0:00 nginx: worker process
root      81136  0.0  0.0 112724   988 pts/2    S+   19:00   0:00 grep --color=auto nginx

网页压缩

nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能
允许nginx服务器将输出内容在发送给客户端之前进行压缩,以节约网站带宽,提高用户的访问体验,默认已安装
gzip on|off
开启gzip压缩
可用位置:http,server,location
gzip_min_length 2k
大小超过2k则压缩,允许压缩的页面最小字节数
可用位置:http,server,location
gzip_buffers 4 16k
申请4个单位为16k的内存作为压缩结果流缓存,默认是申请与原始数据大小相同的内存空间来存储gzip压缩结果
可用位置:http,server,location
gzip_comp_level 2
指定gzip压缩比,1压缩比最小,处理最快;9压缩比最大,传输速度快,但处理速度最慢,使用默认即可
可用位置:http,server,location
gzip_types text/plain
压缩类型,对指定类型文件进行压缩
可用位置:http,server,location
gzip_vary on|off
让前端的缓存服务器缓存经过gzip压缩的页面
可用位置:http,server,location
gzip_proxied off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any
响应报文在何种条件下启用压缩功能
expired,no-cache, no-store,private:响应报文首部Cache-Control值任何一个,启用压缩功能
可用位置:http, server, location

[root@5centos /]# vim /usr/local/nginx/conf/nginx.conf
    gzip  on;
    gzip_min_length     1k;
    gzip_buffers        4 16k;
    gzip_http_version   1.1;
    gzip_comp_level     6;    gzip_types  text/plain/ application/x-javascript text/css image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE[1-6]\.";   ##不对微软IE1-6浏览器版本进行压缩,因为IE1-6不支持
    gzip_vary           on;
    
[root@5centos html]# vim index.html 
<h1>This is Ora's blog.</h1>
<img src="bizhi.jpg" />
[root@5centos /]# systemctl restart nginx.service

在这里插入图片描述
在这里插入图片描述

日志切割

nginx在处理高并发的时候,产生的日志量是非常大的,故nginx日志将需要管理员用科学的方法进行切割,以方便管理。
nginx本身不具备日志分割处理的功能,但可以通过nginx信号控制功能的脚本实现日志自动分割。

脚本主要思路

​ 设置时间变量
​ 设置保存日志路径
​ 将目前的日志文件进行重命名
​ 删除时间过长的日志文件
​ 设置cron任务,定期执行脚本,自动分割日志

制作脚本,验证日志

[root@5centos /]# mkdir jiaoben
[root@5centos /]# cd jiaoben/
[root@5centos jiaoben]# vim rizhifenge.sh
#!/bin/bash
date=$(date -d "-1 day" "+%Y%m%d")
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ]|| mkdir -p $logs_path
mv /usr/local/nginx/logs/access.log ${logs_path}/access.log-$date
kill -HUP $(cat $pid_path)
find $logs_path -mtime +30| xargs rm -rf

[root@5centos nginx]# crontab -e
0 0 * * * /jiaoben/rizhifenge.sh

oot@5centos jiaoben]# cd /
[root@5centos /]# systemctl restart nginx   ##去刷新几次网页
[root@5centos /]# cd /var/lo
local/ lock/  log/   
[root@5centos /]# cd /var/log/nginx/
[root@5centos nginx]# ls
access.log-20200809

防盗链设置

装个 DNS 服务
本机域名 www.ora.com
访问如下所示
在这里插入图片描述

盗图网站

<!DOCTYPE html>
<html>
        <head>
                <meta charset="utf-8">
                <title>这里是盗图站!!!</title>
        </head>
        <body>
                <h1>这里是盗图站!盗图!我们是专业的!</h1>
                <img src="http://www.ora.com/bizhi.jpg" />
        </body>
</html>

在这里插入图片描述

防盗链
注:切记,替换的图片地址要使用没有防盗链的网站图片,否则由于替换的图片其实也处于防盗链情况下,会造成仍旧无法显示设置的图片,我懒得开别的机器…也不想再做个网页了,就指向了那个盗图网站的图……嘻嘻。

[root@5centos html]# ls
bizhi.jpg  FDL.png  index.html
[root@5centos html]# vim /usr/local/nginx/conf/nginx.conf
       ##手敲
       server {
        listen       80;
        server_name  www.ora.com;
        root   /var/www/html;
        index  index.html index.htm;
        下面的注释掉……
       # location / {
        #    root   /var/www/html/;
         #   index  index.html index.htm;
        #}
        注:切记,替换的图片地址要使用没有防盗链的网站图片,否则由于替换的图片其实也处于防盗链情况下,会造成仍旧无法显示设置的图片,我懒得开别的机器…也不想再做个网页了,就指向了那个盗图网站的图……嘻嘻。
        location ~* \.(png|gif|jpg)$ {
                valid_referers none ora *.ora.com ora.com;
                if ($invalid_referer){
                rewrite ^/ http://20.0.0.7/FDL.png;
    }}
    }}


指向 IP 主页文件
[root@7CentOS html]# ls
FDL.png  index.html

不以域名访问会出现 “盗个锤锤”
在这里插入图片描述

在这里插入图片描述
以域名访问时,正常显示

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值