Nginx隐藏版本号的方法:修改配置文件法
curl -I http://20.0.0.10
HTTP/1.1 200 OK
Server: nginx/1.12.2
vi /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
重启nginx服务
systemctl restart nginx
再次查看版本信息
curl -I http://20.0.0.10 HTTP/1.1 200 OK Server: nginx
关闭防火墙,
systemctl stop firewalld
setenforce 0
修改Nginx用户和组
编译安装时指定用户与组
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx; //第一行,修改
systemctl restart nginx //重启nginx服务
ps aux | grep nginx //查看进程信息
实现Nginx的日志切割
vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
expires 1d; //添加,设置缓存时间为1天
}
systemctl restart nginx
实现Nginx的日志切割
随着Nginx运行时间增加,日志也会增加。太大的日志文件对监控是一个大灾难。所以需要定期进行日志文件的切割
Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割(Kill -HUP cat /xxx/log/nginx.pid #平滑重启nginx,类似reload)
-QUIT :结束进程;-USR1:日志分割;-USR2:平滑升级
通过Linux的计划任务周期性地进行日志切割
修改配置文件
vi /usr/local/nginx/conf/nginx.conf
error_log logs/error.log info;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; //四条配置去除前面#号
date +%Y%m%d
20201117
date 2020年 11月 17日 星期二 16:03:48 CST
更改Nginx运行进程数
在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。
修改配置文件的worker_processes参数
一般设为CPU的个数或者核数
在高并发情况下可设置为CPU个数或者核数的2倍
增加进程数,可减少了系统的开销,提升了服务速度
使用ps aux查看运行进程数的变化情况
cat /proc/cpuinfo | grep -c "physical id" ##查看物理CPU的个数
4
vim /usr/local/nginx/conf/nginx.conf
worker_processes 4 //第2行 将工作进程数设为4
service nginx stop
service nginx start
配置Nginx实现网页压缩功能
vim /usr/local/nginx/conf/nginx.conf
gzip on; //开启gzip压缩功能
gzip_min_length 1k; //压缩阈值(超过1k的文件进行压缩)
gzip_buffers 4 16k; //buffer(缓冲)大小为4个16k缓冲区大小
gzip_http_version 1.1; //压缩版本
gzip_comp_level 6; //压缩比率,最小为1,处理速度快,传输速度慢;最大为9,处理速度慢,传输速度快
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE [1-6]\."; //配置禁用gzip条件,支持正则,表示ie6以下不启用
在/usr/local/nginx/html目录下插入jpg图片
重启服务 systemctl restart nginx
vim /usr/local/nginx/html/index.html ...
<html><body><h1>belle</h1><img src=a.jpg / ></body></html>
网页准备
Web源主机(www.tt.com)配置:
cd /usr/local/nginx/html
配置首页文件 vim index.html
<html>
<body>
<h1>Spring is here~</h1>
<img src="work.jpg"/> </body> </html>
添加IP和域名的映射关系 echo "172.16.100.100www.tt.com" >>
/etc/hosts echo "172.16.100.100www.yuji.com" >> /etc/hosts
盗链网站主机(www.yuji.com)配置:
切换到站点目录 cd /usr/local/nginx/html
#2、配置首页文件,图片盗用Web源主机中的图片资源
vim index.html
<html>
<body>
<h1>test</h1>
<img src="http://www.tt.com/work.jpg"/>
</body>
</html>
#3、添加IP和域名的映射关系 echo "172.16.100.100www.tt.com" >> /etc/hosts
echo "172.16.100.100www.yuji.com" >> /etc/hosts
转自:
作者:聂鲁达的邮差
链接:https://juejin.cn/post/7098689321380610062
Web源主机配置防盗链、
vim /usr/local/nginx/conf/nginx.conf
http {
......
server {
......
#不是由tt.com域名请求的图片资源,重写到盗链图片error.png
location ~* \.(jpg|gif|swf)$ {
root html;
expires 1d;
valid_referers none blocked *.tt.com tt.com;
if ( $invalid_referer ) { rewrite ^/ http://www.tt.com/error.png; }
}
......
}
}
systemctl restart nginx