目录
如何隐藏版本信息
修改配置文件
重新编译安装
修改用户与组
在编译安装时设置
修改配置文件
缓存时间
日志切割
编写脚本
连接超时
更改请求进程数
网页压缩
防盗链
fpm参数优化
如何隐藏版本信息
如何查看版本信息
[root@localhost ~]#curl 192.168.159.101 [root@localhost ~]#curl -I 192.168.159.101
修改配置文件
[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
#加入 server_tokens off;
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
........
}
[root@localhost ~]#curl -I 192.168.159.101
重新编译安装
[root@localhost nginx-1.12.0]#vim /opt/nginx-1.12.0/src/core/nginx.h
#define nginx_version 1012000
#define NGINX_VERSION "666" #改为666
#define NGINX_VER "IIS/" NGINX_VERSION #改为IIS
[root@localhost nginx-1.12.0]#vim /opt/nginx-1.12.0/src/core/nginx.h #define nginx_version 1012000 #define NGINX_VERSION "666" #改为666 #define NGINX_VER "IIS/" NGINX_VERSION #改为IIS
重新编译安装
[root@localhost nginx-1.12.0]#cd /opt/nginx-1.12.0/ [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module [root@localhost nginx-1.12.0]#make && make install
改配置文件
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
.........
http {
include mime.types;
default_type application/octet-stream;
server_tokens on; #打开版本号
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# curl -I http://192.168.159.102
修改用户与组
在编译安装时设置
[root@localhost opt]# cd nginx-1.15.9/
[root@localhost nginx-1.15.9]#
./configure
--prefix=/usr/local/nginx
--user=nginx #设置用户
--group=nginx #设置组
--with-http_stub_status_module
修改配置文件
修改Nginx配置文件的Nginx指定用户与组
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
[root@localhost opt]# cd nginx-1.15.9/ [root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx #设置用户 --group=nginx #设置组 --with-http_stub_status_module
修改配置文件
修改Nginx配置文件的Nginx指定用户与组
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
重启 Nginx 查看进程运行情况,主进程由 root 帐户创建,子进程则由 Nginx 创建。 [root@localhost nginx-1.12.0]#useradd -s /sbin/nologin junnan #创建用户junnan 并禁止登录shell [root@localhost nginx-1.12.0]# id junnan uid=1002(junnan) gid=1002(junnan) 组=1002(junnan) [root@localhost nginx-1.12.0]#systemctl restart nginx.service
缓存时间
当nginx 将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度一般针对静态网页设置,对动态网页不设置缓存时间。
location ~ .(gif|jpg|jepg|png|bmp|ico)$ {
root html;
expires 1d; #缓存时间1天
}
其中的 Cahce-Control:max-age=86400 表示缓存时间是 86400 秒,也就是缓存一天的 时间,一天之内浏览器访问这个页面,都是用缓存中的数据,而不需要向 Nginx 服务器重 新发出请求,减少了服务器的使用带宽。
日志切割
为了方便掌握 Nginx 的运行 状态,需要时刻关注 Nginx 日志文件。太大的日志文件对监控是一个大灾难,不便于分析 排查,需要定期的进行日志文件的切割。
编写脚本
[root@www ~]# vim /opt/fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$(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}/test.com-access.log-$d#移动并重命名日志文件
kill -HUP $(cat $pid_path) #重建日志文件
find $logs_path -mtime +30 | xargs rm -rf #删除30天前的日志文件
首先先定义一个前一天的时间戳的变量d,再指定两个变量,一个是pid文件位置,一个是保存分割之后的日志文件位置,然后判断我们定义的/var/log/nginx是否存在,如果不存在创建,根据时间戳,在每天1点把前一天的日志给移动到我们新创建的$logs_path下,并且以前一天的时间戳命名,然后再去发送一个信号让我们的nginx重载,也就是生成一个新的日志文件,以此再去进行揭露当天的日志并且到第二天凌晨一点重新做这个事,最后设置30天清一次。
连接超时
-
HTTP服务有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自同一客户端的其他请求,服务端会利用这个未被关闭的连接,而不需要再次建立一个连接
-
KeepAlive在一段时间内保持打开状态,它们会在这段时间内占用资源,占用过多就会影响服务器的性能
-
在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连 接超时参数,实现控制连接访问时间。可以修改配置文件 nginx.conf,设置 keepalive_timeout 超时时间。
[root@localhost opt]# cd /usr/local/nginx/conf/
[root@localhost conf]# vi nginx.conf
#keepalive_timeout 0;
keepalive_timeout 65;
client_header_timeout 80; #等待客户端发送请求头的超时时间,超时会发送408错误
client_body_timeout 80; #等待客户端发送请求体的超时时间
[root@localhost conf]# 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
更改请求进程数
-
HTTP服务有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自同一客户端的其他请求,服务端会利用这个未被关闭的连接,而不需要再次建立一个连接
-
KeepAlive在一段时间内保持打开状态,它们会在这段时间内占用资源,占用过多就会影响服务器的性能
-
在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连 接超时参数,实现控制连接访问时间。可以修改配置文件 nginx.conf,设置 keepalive_timeout 超时时间。
[root@localhost opt]# cd /usr/local/nginx/conf/ [root@localhost conf]# vi nginx.conf #keepalive_timeout 0; keepalive_timeout 65; client_header_timeout 80; #等待客户端发送请求头的超时时间,超时会发送408错误 client_body_timeout 80; #等待客户端发送请求体的超时时间 [root@localhost conf]# 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
更改请求进程数
在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
#查看cpu核数 [root@localhost conf]# lscpu |grep -i "cpu(s)" CPU(s): 2 On-line CPU(s) list: 0,1 [root@localhost conf]# lscpu |grep -i "cpu(s)"|head -1 CPU(s): 2 [root@localhost conf]# cat /proc/cpuinfo |grep -c "physical id" 2 [root@localhost conf]# cat /proc/cpuinfo|grep "processor"|wc -l 2
修改 Nginx 的配置文件的 worker_processes 参数,一般设为 CPU 的个数或者核数, 在高并发的情况下可设置为 CPU 个数或者核数的 2 倍,可以查看 CPU 的核数以确定参数。 [root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 4; worker_cpu_affinity 01 10 0100 1000; #设置每个进程由不同cpu处理,进程数配为4时,0001 0010 0100 1000 [root@localhost conf]# 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@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_vary on;
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/png;
gzip on:开启 gzip 压缩输出;
gzip_min_length 1k:用于设置允许压缩的页面最小字节数;
gzip_buffers 4 16k:表示申请 4 个单位为 16k 的内存作为压缩结果流缓存,默认值
是申请与原始数据大小相同的内存空间来存储 gzip 压缩结果;
Zip_http_version 1.0:用于设置识别 http 协议版本,默认是 1.1,目前大部分浏览
器已经支持 gzip 解压,但处理最慢,也比较消耗服务器 CPU 资源;
Gzip_comp_level 2:用来指定 gzip 压缩比,1 压缩比最小,处理速度最快;9 压缩
比最大,传输速度快,但处理速度最慢,使用默认即可;
Gzip_vary on:选项可以让前端的缓存服务器缓存经过 gzip 压缩的页面
Gzip_types text/plain:压缩类型,是对哪些网页文档启用压缩功能;
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html gougou.jpg index.html qiche.jpg
[root@localhost html]# vim index.html
......
Welcome to nginx!
#网页中插入图片
[root@localhost html]# systemctl restart nginx
防盗链
在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济 损失,也避免了不必要的带宽浪费。Nginx 的防盗链功能也非常强大,在默认情况下,只需 要进行很简单的配置,即可实现防盗链处理。
配置
vim /usr/local/nginx/conf/nginx.conf
http {
......
server {
......
location ~* .(jip|gif|swf)$ {
valid_referers *.kgc.com kgc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;
#return 403;
}
}
......
}
}
~* .(jpglgif|swf)$ :这段正则表达式表示匹配不区分大小写,以.jpg或.gif或.swf结尾的文件:
valid_ referers :设置信任的网站,可以正常使用图片:
后面的网址或者域名: referer 中包含相关字符串的网址:
if语句:如果链接的来源域名不在valid_ referers所列出的列表中,$invalid_ referer为1, 则执行后面的操作,即进行重写或返回403页面
网页准备: Web源主机(192.168.241.3)配置 cd /usr/local/nginx/html 将kb.jpg、error.png文件传到/usr/local/nginx/html目录下 vim index.html ......


