Auther:Rich七哥
Nginx优化一、Nginx隐藏版本号;二、网页缓存、连接超时、网页压缩传输;配置连接超时:3.网页压缩传输:三、访问控制、定义错误页面、自动索引、目录别名;四、日志分割;五、虚拟主机;六、平滑升级;七、加载第三方模块;
案例环境:
| 系统类型 | IP地址 | 主机名 | 所需软件 |
|---|---|---|---|
| Centos 6.5 | 192.168.1.78 | harry78 | nginx-1.6.2.tar.gz |
一、Nginx隐藏版本号;
方式一:修改配置文件
Ø 安装Nginx
[root@www ~]# yum -y install pcre-devel zlib-devel popt-devel openssl-devel openssl[root@www ~]# useradd -M -s /sbin/nologin nginx[root@www ~]# tar zxvf nginx-1.6.2.tar.gz -C /usr/src/[root@www ~]# cd /usr/src/nginx-1.6.2/[root@www nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install[root@www nginx-1.6.2]# cd[root@www ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/[root@www ~]# nginx [root@www ~]# netstat -utlpn |grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5618/nginx
Ø 配置版本号隐藏;
[root@www ~]# curl -I http://harry78 ##选项为-iHTTP/1.1 200 OKServer: nginx/1.6.2Date: Wed, 11 Jul 2018 16:43:05 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Wed, 11 Jul 2018 16:40:55 GMTConnection: keep-aliveETag: "5b463317-264"Accept-Ranges: bytes [root@www ~]# vi /usr/local/nginx/conf/nginx.conf ##在http{}内添加即可 20 server_tokens off;:wq[root@www ~]# nginx -t ##检查nginx配置文件语法nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@www ~]# killall -9 nginx [root@www ~]# nginx [root@www ~]# curl -I http://harry78HTTP/1.1 200 OKServer: nginx ##版本已经隐藏Date: Fri, 08 Dec 2017 22:56:00 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Fri, 08 Dec 2017 22:47:50 GMTConnection: keep-aliveETag: "5a2b1696-264"Accept-Ranges: bytes
方式二:修改源码包;
[root@localhost ~]# yum -y install pcre-devel zlib-devel popt-devel openssl-*[root@localhost ~]# useradd -M -s /sbin/nologin nginx[root@localhost ~]# tar zxf nginx-1.6.2.tar.gz -C /usr/src/[root@localhost ~]# cd /usr/src/nginx-1.6.2/[root@localhost nginx-1.6.2]# vim src/core/nginx.h ##修改源代码实现隐藏版本 13 #define NGINX_VERSION "6.6.6" 14 #define NGINX_VER "harry.cn/" NGINX_VERSION 15 16 #define NGINX_VAR "harry.cn":wq[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install[root@localhost nginx-1.6.2]# cd[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/[root@localhost ~]# nginx [root@localhost ~]# curl -I http://harry78HTTP/1.1 200 OKServer: harry.cn/6.6.6Date: Fri, 08 Dec 2017 23:06:20 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Fri, 08 Dec 2017 23:05:45 GMTConnection: keep-aliveETag: "5a2b1ac9-264"Accept-Ranges: bytes
二、网页缓存、连接超时、网页压缩传输;
1.**网页缓存:**
作用:页面缓存一般针对静态网页进行设置,对动态网页不用设置缓存时间。方便客户端
在日后进行相同内容的请求时直接返回,以避免重复请求,加快了访问速度。
配置nginx缓存:
[root@www ~]# cat <<END >/usr/local/nginx/html/index.html<html><head><title>harry78</title></head><body>harry78</body></html>END
[root@www ~]# ls /usr/local/nginx/html/index.html linux.jpg[root@www ~]# vi /usr/local/nginx/conf/nginx.conf 55 location ~ .(gif|jpg|jpeg|png|bmp|ico)$ { 56 expires 1d; 57 } 58 location ~ .*.(js|css)$ { 59 expires 1h; 60 }:wq
[root@www ~]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@www ~]# killall -9 nginx
配置连接超时:
作用:在企业网站中,为了避免同一个客户长时间占用连接,造成服务器资源浪费,可以设置相应的连接超时参数,实现控制连接访问时间。
配置项:
| keepalived_timeout | 设置连接保持超时时间,一般可只设置该参数,默认为 65 秒,可根据网站的情况设置,或者关闭,可在 http 段、 server 段、或者 location 段设置。 |
|---|---|
| client_header_timeout | 指定等待客户端发送请求头的超时时间。 |
| client_body_timeout | 设置请求体读取超时时间。 |
| 注意: 若出现超时,会返回 408 报错 |
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf 32 keepalive_timeout 65; 33 client_header_timeout 60; 34 client_body_timeout 60;:wq[root@www ~]# killall -9 nginx[root@www ~]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@www ~]# nginx
3.网页压缩传输:
作用:将服务端传输的网页文件压缩传输,使其更加快速、减少带宽的占用;
配置:
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf 37 gzip on; ##开启 gzip 压缩输出 38 gzip_min_length 1k; ##用于设置允许压缩的页面最小字节数 39 gzip_buffers 4 16k; ##表示申请4 个单位为 16k 的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来储存 gzip 压缩结果 40 gzip_http_version 1.1; # #设置识别 http 协议版本,默认是 1.1 41 gzip_comp_level 2; ##gzip 压缩比, 1-9 等级 42 gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss; ##压缩类型,是就对哪些网页文档启用压缩功能[root@www ~]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@www ~]# killall nginx[root@www ~]# nginx
三、访问控制、定义错误页面、自动索引、目录别名;
1.**访问控制:**
作用:限制访问网站资源;
配置项:
| auth_basic "Nginx Status"; | 认证提示文字 |
|---|---|
| auth_basic_user_file /usr/local/nginx/conf/user.conf; | 认证用户文件,可以使用apache提供的htpasswd命令来生成文件 |
| allow 192.168.100.1; | 允许客户端ip地址 |
| deny 192.168.100.0/24; | 拒绝的网段 |
配置:
[root@www ~]# yum -y install httpd-tools[root@www ~]# htpasswd -c /usr/local/nginx/conf/user.conf zs[root@www ~]# cat /usr/local/nginx/conf/user.conf zs:VJVdQdVHEIvZo[root@www ~]# vi /usr/local/nginx/conf/nginx.conf 70 location /status { 71 stub_status on; 72 access_log off; 73 auth_basic "Nginx Status"; 74 auth_basic_user_file /usr/local/nginx/conf/user.conf; 75 allow 192.168.100.1; 76 deny 192.168.100.0/24; 77 }[root@ www ~]# killall -9 nginx[root@ www ~]# nginx
客户端访问验证:
2.**定义错误页面:**
作用:根据客户端的访问网站的返回状态码,为其指定到特定的错误页面;
配置:
[root@ www ~]# vi /usr/local/nginx/conf/nginx.conf
78 error_page 403 404 /404.html;
79 location = /404.html {
80 root html;
81 }
[root@ www ~]# echo "deny" >>/usr/local/nginx/html/404.html
[root@ www ~]# killall -9 nginx
[root@www ~]# nginx
客户端访问验证:
3.**自动索引:**
作用:将网站转化为类似ftp的站点,作为共享文件的工具;
配置:
[root@www ~]# mkdir -p /usr/local/nginx/html/download/haha/[root@www ~]# touch /usr/local/nginx/html/download/haha/{1..10}.txt[root@www ~]# vi /usr/local/nginx/conf/nginx.conf 82 location /download { 83 autoindex on; 84 }[root@www ~]# killall -9 nginx[root@www ~]# nginx
客户端访问测试:
4.**目录别名:**
作用:将域名后缀的路径设置一个别名,通过多种方式访问;
配置:
[root@www ~]# vi /usr/local/nginx/conf/nginx.conf 85 location /dw { 86 alias /usr/local/nginx/html/haha/; 87 }[root@www ~]# mkdir /usr/local/nginx/html/haha[root@www ~]# echo "haha" >/usr/local/nginx/html/haha/index.html[root@www ~]# killall -9 nginx[root@www ~]# nginx
客户端访问测试:
四、日志分割;
方式:脚本方式
技术要点:
a.剪切日志后,使用kill -USR1发送信号重新生成日志文件,同时还不影响网站请求处理进程。
b.错误时通过echo和tee -a命令将错误显示的同时写入到日志文件/var/log/messages。
[root@www ~]# vi /root/cut_nginx_log.sh#!/bin/bash# by harry78 cut_nginx_log.shdatetime=$(date -d "-1 day" "+%Y%m%d")log_path="/usr/local/nginx/logs"pid_path="/usr/local/nginx/logs/nginx.pid"mkdir -p $log_path/backupif [ -f $pid_path ]thenmv log_path/access.log log_path/backup/access.log-$datetimekill -USR1 (cat pid_path)##USR1通常被用来告知应用程序重载配置文件;find $log_path/backup -mtime +30 | xargs rm -felseecho "Error,Nginx is not working!" >> /var/log/messagesfi:wq[root@www ~]# chmod +x /root/cut_nginx_log.sh[root@www ~]# echo "0 0 * * * /root/cut_nginx_log.sh" >>/var/spool/cron/root[root@www ~]# crontab -l0 0 * * * /root/cut_nginx_log.sh[root@www ~]# sh -x /root/cut_nginx_log.sh[root@www ~]# ls /usr/local/nginx/logs/access.log backup error.log nginx.pid[root@www ~]# ls /usr/local/nginx/logs/backup/access.log-20171208
五、虚拟主机;
作用:在同一台服务器上部署多个网站,减免资源的占用;
实现方式:
1.不同IP,不同域名,相同端口;
2.相同IP,相同域名,不同端口;
3.相同IP,相同端口,不同域名;
案例环境:
| 系统类型 | IP地址 | 主机名 | 所需软件 |
|---|---|---|---|
| Centos 6.5 | 192.168.100.151 | harry666 | nginx-1.6.2.tar.gz |
方式一:不同IP,不同域名,相同端口;
[root@harry ~]# ip a |grep 192.168.100 inet 192.168.100.151/24 brd 192.168.100.255 scope global eth0 inet 192.168.100.200/24 brd 192.168.100.255 scope global secondary eth0:0[root@linharry# vi /usr/local/nginx/conf/nginx.confworker_processes 1; events { use epoll; worker_connections 4096; }http { include mime.types; default_type application/octet-stream; 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; sendfile on; keepalive_timeout 65; server { listen 192.168.100.151:80; server_name harry78; charset utf-8; access_log logs/harry.access.log main; location / { root /var/www/harry/; index index.html index.php; } } server { listen 192.168.100.200:80; server_name www.harry.cn; charset utf-8; access_log logs/harry.access.log main; location / { root /var/www/harry/; index index.html index.php; } } }[root@linuxrharrykdir -p /var/www/harry[root@linuxren harryr -p /var/www/harry[root@linuxren ~]#harryarry78" >/var/www/harry/index.html[root@linuxren ~]# echarryharry.cn" >/var/www/harry/index.html[root@linuxren ~]# killaharryinx[root@linuxren ~]# nginxharry
客户端访问测试:
方式二:相同**IP,不同域名,相同端口;**
[root@harry ~]# vi /usr/local/nginx/conf/nginx.confworker_processes 1; events { use epoll; worker_connections 4096; }http { include mime.types; default_type application/octet-stream; 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; sendfile on; keepalive_timeout 65; server { listen 192.168.100.151:80; server_name harry78; charset utf-8; access_log logs/harry.access.log main; location / { root /var/www/harry/; index index.html index.php; } } server { listen 192.168.100.151:80; server_name www.harry.cn; charset utf-8; access_log logs/harry.access.log main; location / { root /var/www/harry/; index index.html index.php; } } }[root@linharry# killall -9 nginx[root@linuxrharryginx
客户端访问测试:
方式三:相同IP,不同端口,相同域名;
[root@harry ~]# vi /usr/local/nginx/conf/nginx.confworker_processes 1; events { use epoll; worker_connections 4096; }http { include mime.types; default_type application/octet-stream; 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; sendfile on; keepalive_timeout 65; server { listen 192.168.100.151:80; server_name harry78; charset utf-8; access_log logs/harry.access.log main; location / { root /var/www/harry/; index index.html index.php; } } server { listen 192.168.100.151:8080; server_name harry78; charset utf-8; access_log logs/harry.access.log main; location / { root /var/www/harry/; index index.html index.php; } } }[root@linharry# killall -9 nginx[root@linuxrharryginx
客户端访问测试:
六、平滑升级;
原理:
1.Nginx 的主进程( master process)启动后完成配置加载和端口绑定等动作, fork 出指定数量的工作进程( worker process),这些子进程会持有监听端口的文件描述符( fd),并通过在该描述符上添加监听事件来接受连接( accept);
2.Nginx 主进程在启动完成后会进入等待状态,负责响应各类系统消息,如 SIGCHLD、 SIGHUP、SIGUSR2 等;
3.主进程支持的信号:
TERM, INT: 立刻退出; QUIT: 等待工作进程结束后再退出
KILL: 强制终止进程; HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
USR1: 重新打开日志文件; USR2: 启动新的主进程,实现热升级
WINCH: 逐步关闭工作进程及工作进程支持的信号;
过程:
1.查看旧版 nginx 的编译参数;
2.编译新版本 Nginx 源码包,安装路径需与旧版一致,注意:不要执行 make install;
3.备份二进制文件,用新版本的替换;
4.确保配置文件无报错;
5.发送 USR2 信号:向主进程( master)发送 USR2 信号, Nginx 会启动一个新版本的 master 进程和对应工作进程,和旧版一起处理请求;
6.发送 WINCH 信号:向旧的 Nginx 主进程( master)发送 WINCH 信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版 Nginx 处理;
7.发送 QUIT 信号:升级完毕,可向旧的 Nginx 主进程( master)发送( QUIT、 TERM、或者 KILL)信号,使旧的主进程退出;
8.验证 nginx 版本号,并访问测试;
配置:
Ø 准备软件包并查看旧版安装选项;[root@harry ~]# ls nginx-1.*nginx-1.12.0.tar.gz nginx-1.6.2.tar.gz[root@harry ~]# nginx -Vnginx version: nginx/1.6.2built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) TLS SNI support enabledconfigure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre Ø 安装新版本Nginx;[root@harry ~]# tar zxvf nginx-1.12.0.tar.gz -C /usr/src/[root@harry ~]# cd /usr/src/nginx-1.12.0/[root@harry nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make ##不能加make install,如若添加,则覆盖了[root@harry nginx-1.12.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old[root@harry nginx-1.12.0]# cp objs/nginx /usr/local/nginx/sbin/[root@harry nginx-1.12.0]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@harry nginx-1.12.0]# cd Ø 使用信号实现热升级;[root@harry ~]# ps aux |grep nginx |grep -v greproot 6059(老版本主进程) 0.0 0.2 45000 1032 ? Ss 13:03 0:00 nginx: master process nginxnginx 6060 0.0 0.3 45432 1624 ? S 13:03 0:00 nginx: worker process[root@harry ~]# kill -USR2 6059 ##发送 USR2 信号:向主进程( master)发送 USR2 信号, Nginx 会启动一个新版本的 master 进程和对应工作进程,和旧版一起处理请求。[root@harry ~]# kill -WINCH $(cat /usr/local/nginx/logs/nginx.pid) ##关闭老版本的worker进程[root@harry ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) ##关闭老版本的master进程[root@harry ~]# /usr/local/nginx/sbin/nginx ##重新加载新版本的命令[root@harry ~]# ps aux |grep nginx |grep -v greproot 3864 0.0 0.2 45192 1188 ? Ss 03:24 0:00 nginx: master process /usr/local/nginx/sbin/nginxnginx 3865 0.0 0.6 46904 3052 ? S 03:24 0:00 nginx: worker process[root@harry ~]# nginx -Vnginx version: nginx/1.12.0built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre
七、加载第三方模块;
第三方模块下载地址:**https://www.nginx.com/resources/wiki/modules/**
模块:**echo-nginx-module-0.60.tar.gz**
[root@harry ~]# lsecho-nginx-module-0.60.tar.gz nginx-1.6.2.tar.gz[root@harry ~]# tar zxvf echo-nginx-module-0.60.tar.gz -C /usr/src/[root@harry ~]# tar zxvf nginx-1.6.2.tar.gz -C /usr/src/[root@harry ~]# cd /usr/src/nginx-1.6.2/ [root@harry nginx-1.6.2# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre --add-module=/usr/src/echo-nginx-module-0.60/ &&make &&make install[root@harry nginx-1.6.2# cd[root@harry ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/[root@harry ~] # vi /usr/local/nginx/conf/nginx.confworker_processes 1; events { use epoll; worker_connections 4096; }http { include mime.types; default_type application/octet-stream; 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; sendfile on; keepalive_timeout 65; server { listen 192.168.1.78:80; server_name harry78; charset utf-8; access_log logs/harry.access.log main; location / { echo "nginx"; } }}[root@harry ~]# killall -9 nginx[root@harry ~]# nginx[root@harry ~]# curl 192.168.1.78nginx[root@harry ~]# curl -I 192.168.1.78HTTP/1.1 200 OKServer: nginx/1.6.2Date: Fri, 13 Jul 2018 18:06:42 GMTContent-Type: application/octet-streamConnection: keep-alive