Nginx 默认的安装参数只能提供最基本的服务,需要调整相应参数,才能发挥出服务器的最大作用。
和apache一样,在生产环境中,需要隐藏 Nginx 的版本号,以避免泄漏 Nginx 的版本,使攻击者不能针对特定版本进行攻击。
之前对于手工编译安装nginx的步骤已经讲解过很多次了,所以这里就不过多讲解。
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# tar zvxf nginx-1.12.2.tar.gz
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig:- 99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0{start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add /etc/init.d/nginx
[root@localhost nginx-1.12.2]# service nginx start
在win10 虚拟机中验证服务
[root@localhost nginx-1.12.2]# curl -I http://14.0.0.27
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 10 Aug 2020 13:56:06 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 13:48:12 GMT
Connection: keep-alive
ETag: "5f31501c-264"
Accept-Ranges: bytes
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# curl -I http://14.0.0.27
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 10 Aug 2020 14:03:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 13:48:12 GMT
Connection: keep-alive
ETag: "5f31501c-264"
Accept-Ranges: bytes
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# tar zvxf nginx-1.12.2.tar.gz
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
[root@localhost nginx-1.12.2]# cd src/core/
[root@localhost core]# vim nginx.h
#define NGINX_VERSION "1.1.7"
[root@localhost core]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# curl -I http://14.0.0.14
HTTP/1.1 200 OK
Server: nginx/1.1.7
Date: Mon, 10 Aug 2020 14:30:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 14:29:15 GMT
Connection: keep-alive
ETag: "5f3159bb-264"
Accept-Ranges: bytes
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
重启服务
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# ps aux | grep nginx
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jpeg|png|ico)$ {
root html;
expires 1d;
}
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# vim index.html
<img src="why.png"/>
并将图片拷贝到当前目录
[root@localhost html]# ls
50x.html index.html why.png
重启服务
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
制作日志分割脚本
[root@localhost opt]# vim 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)
#删除30天前的日志
find $logs_path -mtime +30 | xargs rm -rf
[root@localhost opt]# ./fenge.sh
需要启动服务
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-200810
如果想要定期每天都进行日志分割,我们可以通过周期性任务计划crontab来定期执行。
在高并发环境中,需要启动更多的 Nginx 进程以保证快速响应,用以处理用户的请求, 避免造成阻塞。
使用 ps aux 命令查看 Nginx 运行进程的个数。从命令执行结果可以看出 master process 是 Nginx 的主进程,开启了 1 个;worker process 是子进程,子进程也是开启了1个,与配置文件中对应。
多的那一个进程是grep的进程
[root@localhost opt]# ps aux | grep nginx
root 21098 0.0 0.0 20620 1472 ? Ss 09:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21124 0.0 0.0 23124 1456 ? S 09:38 0:00 nginx: worker process
root 22163 0.0 0.0 112724 988 pts/0 S+ 09:51 0:00 grep --color=auto nginx
[root@localhost opt]# cat /proc/cpuinfo | grep -c "physical"
8 ##查看到最大支持的核心数是8
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 8;
进程数就变成了8
[root@localhost opt]# ps aux | grep nginx
root 22242 0.0 0.0 20544 672 ? Ss 09:52 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 22243 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22244 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22245 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22246 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22247 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22248 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22249 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
nginx 22250 0.0 0.0 23072 1388 ? S 09:52 0:00 nginx: worker process
root 22252 0.0 0.0 112724 984 pts/0 S+ 09:52 0:00 grep --color=auto nginx
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
要写在http的大括号中
keepalive_timeout 65;
client_header_timeout 60;
client_body_timeout 60;
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
gzip on; ##开启gzip压缩功能
gzip_min_length 1k; ##大于1K开始压缩
gzip_buffers 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]\."; ##微软IE浏览器1-6版本禁用
gzip_vary on; ##支持vary header,让前端的缓存服务器经过gzip压缩的页面
[root@localhost opt]# service nginx stop
[root@localhost opt]# service nginx start
我们用14.0.0.14这一台虚拟机安装httpd网站作为盗链网站。
[root@localhost opt]# yum install httpd -y
[root@localhost opt]# vim /etc/httpd/conf/httpd.conf
[root@localhost opt]# vim /var/www/html/index.html
<h1>this is a text web</h1>
<img src="http://www.abc.com/why.png"/>
14.0.0.27官网
[root@localhost opt]# yum install -y bind
[root@localhost opt]# vim /etc/named.conf
[root@localhost opt]# vim /etc/named.rfc1912.zones
[root@localhost opt]# cp -p /var/named/named.localhost /var/named/abc.com.zone
[root@localhost opt]# vim /var/named/abc.com.zone
[root@localhost opt]# systemctl start named
在win10虚拟机中访问,将win10中的DNS设为14.0.0.27
14.0.0.27中nginx配置文件
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(gif|jepg|png)$ {
valid_referers none blocked *.abc.com abc.com;
if ($invalid_referer) {
rewrite ^/ http://www.abc.com/error.jpg;
}
}
[root@localhost opt]# cd /usr/local/nginx/html/
将防止盗链的图片加进去
[root@localhost html]# ls
50x.html error.jpg index.html why.png
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
在php配置文件php-fpm.conf中
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20 ##static模式下空闲进程数上限,大于下面的值
pm.start_server=5 ##动态方式下默认开启的进程数,在最小和最大之间
pm.min_spare_servers=2 ##动态方式下最少空闲进程数
pm.max_spare_servers=8 ##动态方式下最大空闲进程数