开始安装
系统环境
- CentOS Linux release 7.9.2009 (Core)
- nginx官网地址
- nginx源码包下载地址
yum安装
# 配置nginx yum源
cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 刷新缓存
yum makecache
# 查看nginx 最新版本
yum list nginx --showduplicates | sort -r
* updates: mirrors.huaweicloud.com
nginx.x86_64 1:1.8.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.8.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.20.1-2.el7 epel
nginx.x86_64 1:1.20.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.20.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.18.0-2.el7.ngx nginx-stable
nginx.x86_64 1:1.18.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.16.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.16.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.14.2-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.14.1-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.14.0-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.12.2-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.12.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.12.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.3-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.2-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.0-1.el7.ngx nginx-stable
# 根据需求安装指定的版本
yum -y install nginx-1.20.1-2
启动服务
systemctl start nginx
systemctl enable nginx
systemctl status nginx
源码包安装
下载安装包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xf nginx-1.18.0.tar.gz
安装编译环境
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel wget
编译安装
# 添加用户和组
groupadd nginx
useradd -g nginx nginx
# 配置
./configure --prefix=/etc/nginx --user=www --group=www --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
# 编译安装
make -j48 && make install # 我的是48核心所以指定一下,加快编译速度
创建启动服务
# 创建启动服务
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
# 创建目录,否则服务起不来
mkdir -p /var/cache/nginx/client_temp
启动nginx服务
systemctl restart nginx
systemctl status nginx
nginx升级
注意:一定要备份nginx配置文件以及相应证书,建议直接把/etc/nginx 这个文件夹打包备份。
tar -zcf /etc/nginx.tar /etc/nginx
mv /usr/sbin/nginx /usr/sbin/nginx.bak
查看nginx版本等相关信息
# 主要是查看当前版本有那些编译参数,就是configure arguments: 这个行的信息
[root@ceshi ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --user=www --group=www --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
下载最新版nginx
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -xf nginx-1.20.1.tar.gz && cd nginx-1.20.1/
编译nginx
-
解压ningx下载的压缩包编译make,切记不要make install
-
make编译完后会在安装目录下生成一个objs目录且在该目录下有一个nginx执行文件。
make -j48
cd objs/
# 由于我们之前已经备份,所以直接覆盖
cp nginx /usr/sbin/nginx
# 覆盖过去以后检查有无问题
[root@ceshi objs]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 使用make upgrade替换老的nginx进程
[root@ceshi nginx-1.20.1]# make upgrade
/usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
kill -USR2 `cat /var/run/nginx.pid`
sleep 1
test -f /var/run/nginx.pid.oldbin
kill -QUIT `cat /var/run/nginx.pid.oldbin`
[root@ceshi nginx-1.20.1]#
执行nginx -V查看nginx最新的版本及编译的参数
# 版本正确升级没问题
[root@ceshi nginx-1.20.1]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --user=www --group=www --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@ceshi nginx-1.20.1]#
提示:在编译Nginx服务时,直接指定用户和组,这样无论配置文件中是否加参数,默认都是nginx用户。
查看查看启动停止是否正常
systemctl restart nginx
systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2021-07-01 15:35:56 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 12485 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 12490 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 12491 (nginx)
CGroup: /system.slice/nginx.service
├─12491 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─12492 nginx: worker process
Jul 01 15:35:56 ceshi systemd[1]: Starting nginx - high performance web server...
Jul 01 15:35:56 ceshi systemd[1]: Started nginx - high performance web server.
[root@ceshi nginx-1.20.1]#
[root@ceshi nginx-1.20.1]#
[root@ceshi nginx-1.20.1]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
到此升级完成
nginx配置优化
修改nginx配置文件
- server_tokens off;
修改源码,隐藏版本及服务名称
- 这里以nginx-1.18.0版本举例
# 下载nginx-1.18.0
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xf nginx-1.18.0.tar.gz
# 进入目录
cd /root/nginx-1.18.0/src/core
vim nginx.h # 修改这个文件
# 修改这两行,这是修改前的
#define NGINX_VERSION "1.18.0"
#define NGINX_VER "nginx/" NGINX_VERSION
# 这是修改后的
#define NGINX_VERSION " "
#define NGINX_VER " " NGINX_VERSION
# 修改ngx_http_header_filter_module.c 49行
vim src/http/ngx_http_header_filter_module.c
# 修改前
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
# 修改后
static u_char ngx_http_server_string[] = "Server: " CRLF;
# 修改 ngx_http_special_response.c 36行
vim src/http/ngx_http_special_response.c
# 修改前
35 static u_char ngx_http_error_tail[] =
36 "<hr><center>nginx</center>" CRLF
37 "</body>" CRLF
38 "</html>" CRLF
39 ;
# 修改后
35 static u_char ngx_http_error_tail[] =
36 "<hr><center> </center>" CRLF
37 "</body>" CRLF
38 "</html>" CRLF
39 ;
# 到此修改完成
- 这样在启动服务就不会看见服务名称和版本号
- 编译安装,按照我上面的方法,这里就不再赘述了。
修改Nginx服务的默认用户
# 按照我的方法安装的已经修改了,只需要修改配置文件即可。
[root@ceshi conf]# grep '#user' nginx.conf
#user nobody;
[root@ceshi conf]#
useradd nginx -s /sbin/nologin -M # 不需要有系统登录权限,应当禁止其登录能力
限制并发量
-
DDOS攻击者会发送大量的并发连接,占用服务器资源,连接数、带宽等。
-
添加limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
- #备注说明:
#limit_req_zone语法格式如下:
#limit_req_zone key zone=name:size rate=rate;
#上面案例中是将客户端IP信息存储名称为one的共享内存,内存空间为10M
#1M可以存储8千个IP信息,10M可以存储8万个主机连接的状态,容量可以根据需要任意调整
#每秒中仅接受1个请求,多余的放入漏斗
#漏斗超过5个则报错
限制非法请求
# 只允许GET和POST请求。(我是没这样配置过)
http{
server {
listen 80;
if ($request_method !~ ^(GET|POST)$ ) {
return 444;
}
}
}
Linux内核参数优化
echo '# nginx 优化
* soft nofile 100000
* hard nofile 100000 ' >> /etc/security/limits.conf
配置NGINX进程
- 以下参数调整的是Nginx服务的worker进程数,Nginx有Master进程和worker进程之分,Master为管理进程,真正处理请求的是worker进程。
worker_processes 4; # 指定了Nginx要开启的进程数,建议按照cpu核心数来指定
worker_cpu_affinity 0001 0010 0100 1000; # 将每个进程分配给指定的CPU
worker_connections 1024; # 用于定义Nginx每个进程的最大连接数,默认1024
sendfile on; # 开启文件的高效传输模式
tcp_nopush on; # 激活 TCP_CORK socket 选择
tcp_nodelay on; # 数据在传输的过程中不进缓存
keepalive_timeout 65; # 用于设置客户端连接保持会话的超时时间,超过这个时间服务器会关闭该连接
client_header_timeout 15; # 用于设置读取客户端请求头数据的超时时间,如果超时客户端还没有发送完整的 header 数据,服务器将返回 "Request time out (408)" 错误。
client_body_timeout 15; #用于设置读取客户端请求主体数据的超时时间,如果超时客户端还没有发送完整的主体数据,服务器将返回 "Request time out (408)" 错误。
send_timeout 25; # 用于指定响应客户端的超时时间,如果超过这个时间,客户端没有任何活动,Nginx 将会关闭连接。
client_max_body_size 100m; # 设置客户端最大的请求主体大小为 100 M
开启gzip压缩
## 注意压缩的对象必须大于 1KB,极小的文件压缩后可能反而变大。
http {
gzip on; # 开启压缩功能
gzip_min_length 1k; # 允许压缩的对象的最小字节
gzip_buffers 4 32k; # 压缩缓冲区大小,表示申请4个单位为32k的内存作为压缩结果的缓存
gzip_http_version 1.1; # 压缩版本,用于设置识别HTTP协议版本
gzip_comp_level 9; # 压缩级别,1级压缩比最小但处理速度最快,9级压缩比最高但处理速度最慢
gzip_types text/plain application/x-javascript text/css application/xml; # 允许压缩的媒体类型
gzip_vary on; # 该选项可以让前端的缓存服务器缓存经过gzip压缩的页面,例如用代理服务器缓存经过Nginx压缩的数据
}
配置 expires 缓存期限
server {
listen 80;
server_name www.abc.com abc.com;
root html/www;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ # 缓存的对象
{
expires 365d; # 缓存期限为1年
}
}