标签:get red hat 完成后 最大的 tar 错误页面 源码 ola 公司
一.配置Nginx隐藏版本号
[root@localhost ~]# curl -I 192.168.200.111
HTTP/1.1 200 OK
Server: nginx/1.16.1 //Nginx版本号
Date: Fri, 13 Sep 2019 02:20:55 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 612
Last-Modified: Fri, 13 Sep 2019 01:54:04 GMT
Connection: keep-alive
ETag: "5d7af6bc-264"
Accept-Ranges: bytes
隐藏方法
1.修改源码包(安装之前)
[root@localhost ~]# tar xf nginx-1.16.1.tar.gz -C /usr/src/
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# vim /usr/src/nginx-1.16.1/src/core/nginx.h
13 #define NGINX_VERSION "8.15.45"
14 #define NGINX_VER "chenyu/" NGINX_VERSION 这两个位置改成你想要的名字和版本号
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
[root@localhost ~]# cd /usr/src/nginx-1.16.1/
[root@localhost nginx-1.16.1]./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
[root@localhost nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.16.1]# nginx
[root@localhost nginx-1.16.1]# curl -I 192.168.200.111
HTTP/1.1 200 OK
Server: chenyu/8.15.45 //修改成功
Date: Fri, 13 Sep 2019 02:32:11 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 13 Sep 2019 02:30:34 GMT
Connection: keep-alive
ETag: "5d7aff4a-264"
Accept-Ranges: bytes
2.修改配置文件(安装完成后)
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在http{ }中添加 sever_tokens off;
[root@localhost ~]# 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 ~]# killall -HUP nginx
[root@localhost ~]# curl -I 192.168.200.111
HTTP/1.1 200 OK
Server: nginx //安装完成后的修改无法修改版本号
Date: Fri, 13 Sep 2019 02:35:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 13 Sep 2019 02:30:34 GMT
Connection: keep-alive
ETag: "5d7aff4a-264"
Accept-Ranges: bytes
二. 修改Nginx用户与组
1.编译安装时指定
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.16.1]./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
2.修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
2 user nginx nginx;
[root@localhost ~]# ps aux |grep nginx
root 4715 0.0 0.1 46100 1952 ? Ss 10:32 0:00 nginx: master process nginx
nginx 4803 0.0 0.2 48624 2340 ? S 10:35 0:00 nginx: worker process
root 4975 0.0 0.0 112724 996 pts/0 R+ 10:54 0:00 grep --color=auto nginx
三.配置Nginx网页缓存时间
当Nginx将网页数据返回给客户端后,可以设置缓存时间,以方便在日后进行相同内容的请求时直接返回
可修改配置文件,在http段,或server段,或者location段加入对特定内容的过期参数
以图片为例
[root@localhost html]# ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx.conf 主配置文件太长,所以我创建了条连接
[root@localhost html]# vim /etc/nginx.conf
location ~ \.(jpg|jpeg|gif)$ {
expires 1d;
}
[root@localhost html]# 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 html]# killall -HUP nginx
[root@localhost html]# vim /usr/local/nginx/html/index.html //在/body> 前加下面的句子引用图片
<img src="linux.jpg"/>
设置成功,缓存时间为1天
四. 实现Nginx的日志切割
[root@localhost ~]# vim fenge.sh
#!/bin/bash
data=$(date -d "-1 day" "+%Y%m%d") //前一天的时间
logs_path="/usr/local/nginx/logs" //日志存放位置
pid_path="/usr/local/nginx/logs/nginx.pid" //pid文件
[ -d $logs_path/bak ] || mkdir -p $logs_path/bak //判断是否存在备份目录
if [ -f $pid_path ];then //判断
mv $logs_path/access.log $logs_path/bak/access.log-$data //将日志文件打包放在bak中以前一天的时间为名
kill -USR1 $(cat $pid_path) //生成新的日志
find $logs_path -mtime +30 | xargs rm -f //删除30天前的命令
else
echo "Error,Nginx is not working!" | tee -a /var/log/messages //如果未运行或失败则输出并加入到系统日志中
fi
[root@localhost ~]# tail -f /usr/local/nginx/logs/access.log
192.168.200.111 - - [13/Sep/2019:10:32:11 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0"
192.168.200.111 - - [13/Sep/2019:10:35:53 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" //之前测试产生的日志
[root@localhost ~]# bash fenge.sh
[root@localhost ~]# cat /usr/local/nginx/logs/access.log //运行脚本后日志为空
[root@localhost ~]# cd /usr/local/nginx/logs/
[root@localhost logs]# ls
access.log bak error.log nginx.pid //生成了备份目录
[root@localhost logs]# cd bak
[root@localhost bak]# ls
access.log-20190912 //生成了备份文件
[root@localhost bak]# cat access.log-20190912
192.168.200.111 - - [13/Sep/2019:10:32:11 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0"
192.168.200.111 - - [13/Sep/2019:10:35:53 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0"//备份日志为之前产生的日志
[root@localhost bak]# cd
[root@localhost ~]# chmod +x fenge.sh 给脚本加执行权限放在每天0点执行
[root@localhost ~]# crontab -e
0 0 * * * /root/fenge.sh
五.配置Nginx实现链接超时
[root@localhost html]# vim /etc/nginx.conf
在server前添加
keepalive_timeout 65; //连接保持超时时间,根据网站情况设置,可在http段,server段或者location段设置
client_header_timeout 60; //请求头
client_body_timeout 60; //请求主体
[root@localhost html]# 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 html]# killall -HUP nginx
六.更改Nginx运行进程数
在高并发场景,需要启动更多的nginx进程以保证快速影响。
修改配置文件的worker_processes参数,一般设置CPU的个数或者核数的2倍
[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical"
2
[root@localhost ~]# vim /etc/nginx.conf
worker_processes 2;
默认Nginx的多进程可能更多的跑在一颗cpu上,可以分配不同的进程给不同的cpu处理。一台4核的cpu可以进行下面的配置,将进程进行分配
worker_cpu_affinity 0001 0010 0100 1000
七.配置Nginx实现网页压缩功能
Nginx的nex_http_gzip_module压缩模块提供了对文件内容压缩的功能,允许nginx服务器将输出内容发送给客户端之前进行压缩。
[root@localhost ~]# vim /etc/nginx.conf
gzip on; //开启gzip压缩输出
gzip_min_length 1k; //用于设置允许压缩的页面最小字节数
gzip_buffers 4 16k; //表示申请4个单位为16k的内存作为压缩结果流缓存
gzip_http_version 1.1; //设置识别http协议版本,默认是1.1
gzip_comp_level 2; //gzip压缩比,1-9等级
gzip_types text/plain text/javascript application/x-javascrip text/css text/xml application/xml application/xml+rss; //压缩类型,就是对哪类网页文档启用压缩功能
#gzip_vary on; //选项可以让前端的缓存服务器经过gzip压缩的页面
[root@localhost ~]# 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实现防盗链功能
111中引用图片,114中盗用图片链接
[root@localhost nginx]# vim html/index.html
..........
<img src="http://192.168.200.111/linux.jpg"/>
..........
114中的图片地址为111的图片地址,盗链成功
在111中设置防盗链
[root@localhost ~]# vim /etc/nginx.conf
location ~* \.(jpg|jpeg)$ {
valid_referers none blocked *.amber.com amber.com; //valid_referers 设置信任网站,一般为公司内部的ip。none浏览器中referer为空的情况,就直接在浏览器中访问图片。 block referer不为空的情况,但是值被代理或者防火墙删除了,这些值不以http://或https://开头
if ($invalid_referer) {
rewrite ^/ http://192.168.200.111/daolian.txt; //如果连接的来源不是*.amber.com amber.com的域,则强制跳转到http://192.168.200.111/daolian.txt
}
}
[root@localhost ~]# 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 ~]# killall -HUP nginx
图片加载错误,防盗链成功
九.对FPM模块进行参数优化
Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数跳转。
FPM:优化参数
pm //使用哪种方式启动fpm进程,可以说static和dynamic,前者将产生固定数量的fpm进程,后者将以动态的方式产生fpm进程
pm.max_children //static方式开启的fpm进程数
pm.start_servers //动态方式下初始的fpm进程数
pm.min_spare_servers //动态方式下最小的fpm空闲进程数
pm.max_spare_servers //动态方式下最大的fpm空闲进程数
注:以上调整要根据服务器的内存与服务器负载进行调整
示例:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
#vim/usr/local/php5/etc/php-fpm.conf
pm = dynamic
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
十.Nginx为目录添加访问控制
[root@localhost ~]# yum -y install httpd-tools //使用apache的htpasswd创建密码,安装http-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/user tom //使用htpasswd首次创建密码时,需要加-c,为了是创建储存用户和密码的文件夹。
New password:
Re-type new password:
Adding password for user tom
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /admin {
stub_status on;
access_log off; //前两行是ngxinx的管理模块
auth_basic "Nginx status"; //basic是一种基本认证方式,双引号的名字可以自定义
auth_basic_user_file /usr/local/nginx/user; //指定登录的用户和密码的保存位置
#allow 192.168.200.114; //允许114登录
#deny 192.168.200.0/24; //不允许200网段的登录
}
[root@localhost ~]# 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 ~]# killall -HUP nginx
登录模块管理界面需要登陆
登陆成功显示内容
十一.自定义错误页面
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
error_page 401 403 404 408 /40x.html; //当返回401 403 404 408错误时,去寻找40x.tml
location = /40x.html { //location匹配,当找40x.html时,去html里找
root html;
}
[root@localhost html]# cat 40x.html
<h1>这是我自定义的错误页面</h1> //创建40x.html 自定义错误页面
[root@localhost html]# 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 html]# killall -HUP nginx
测试时,在域名后随意输入内容,会返回错误页面,测试成功。
十二.自动索引
[root@localhost html]# mkdir download
[root@localhost html]# cd download/
[root@localhost download]# mkdir {3..7}.{1..9}
[root@localhost download]# ls
3.1 3.3 3.5 3.7 3.9 4.2 4.4 4.6 4.8 5.1 5.3 5.5 5.7 5.9 6.2 6.4 6.6 6.8 7.1 7.3 7.5 7.7 7.9
3.2 3.4 3.6 3.8 4.1 4.3 4.5 4.7 4.9 5.2 5.4 5.6 5.8 6.1 6.3 6.5 6.7 6.9 7.2 7.4 7.6 7.8
[root@localhost download]# cd 7.9
[root@localhost 7.9]# touch Centos7.9.26
[root@localhost 7.9]# ls
Centos7.9.26
[root@localhost 7.9]# vim /usr/local/nginx/conf/nginx.conf
location /download { //访问download开启下载
autoindex on;
}
[root@localhost 7.9]# 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 7.9]# killall -HUP nginx
点击可以下载
十三. 通过UA实现手机端和电脑端的分离(有些问题,先别看了)
location / {
root /usr/local/nginx/html;
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL\-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
{
root /usr/local/nginx/html/mobile;
}
index index.html index.htm;
}
十四.Nginx平滑升级版本
[root@localhost ~]# nginx -V
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost ~]# tar xf nginx-1.16.1.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.16.1/
[root@localhost nginx-1.16.1]# ./configure --prefix=usr/local/nginx --user=nginx --group=nginx && make //不要make install
[root@localhost nginx-1.16.1]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# mv nginx nginx.old
[root@localhost sbin]# cd /usr/src/nginx-1.16.1/
[root@localhost nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.16.1]# ls /usr/local/nginx/sbin/
nginx nginx.old
[root@localhost ~]# ps aux | grep nginx
root 8694 0.0 0.1 20552 608 ? Ss 10:05 0:00 nginx: master process /usr/local/nginx/sbin/nginx //老版本的进程
nginx 8695 0.0 0.2 23088 1380 ? S 10:05 0:00 nginx: worker process
root 11295 0.0 0.2 112724 996 pts/0 R+ 10:09 0:00 grep --color=auto nginx
[root@localhost ~]# kill -USR2 8694
[root@localhost ~]# ps aux | grep nginx
root 8694 0.0 0.1 20552 796 ? Ss 10:05 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8695 0.0 0.2 23088 1380 ? S 10:05 0:00 nginx: worker process
root 11305 0.0 0.3 20552 1600 ? S 10:09 0:00 nginx: master process /usr/local/nginx/sbin/nginx //出现两组nginx
nginx 11306 0.0 0.2 23088 1384 ? S 10:09 0:00 nginx: worker process
root 11308 0.0 0.2 112724 996 pts/0 R+ 10:09 0:00 grep --color=auto nginx
[root@localhost ~]# kill -WINCH 8694
[root@localhost ~]# ps aux | grep nginx
root 8694 0.0 0.1 20552 796 ? Ss 10:05 0:00 nginx: master process /usr/local/nginx/sbin/nginx/ //工作进程关闭
root 11305 0.0 0.3 20552 1600 ? S 10:09 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 11306 0.0 0.2 23088 1384 ? S 10:09 0:00 nginx: worker process
root 11328 0.0 0.2 112724 996 pts/0 R+ 10:10 0:00 grep --color=auto nginx
[root@localhost ~]# kill -QUIT 8694
[root@localhost ~]# ps aux | grep nginx
root 11305 0.0 0.3 20552 1600 ? S 10:09 0:00 nginx: master process /usr/local/nginx/sbin/nginx //只剩下新版本的进程
nginx 11306 0.0 0.2 23088 1384 ? S 10:09 0:00 nginx: worker process
root 11336 0.0 0.2 112724 996 pts/0 R+ 10:11 0:00 grep --color=auto nginx
[root@localhost ~]# nginx -v
nginx version: nginx/1.16.1 //升级完成
标签:get red hat 完成后 最大的 tar 错误页面 源码 ola 公司
原文地址:https://www.cnblogs.com/L1-5551/p/11624207.html