Nginx优化与防盗链(隐藏版本号、配置缓存时间、日志分割、修改进程数、配置连接超时、使用gzip压缩页面、防盗链设置,fpm优化)
文章目錄
- 隱藏Nginx版本號(hào)
- 網(wǎng)頁(yè)壓縮
- 網(wǎng)頁(yè)壓縮配置
- 網(wǎng)頁(yè)緩存時(shí)間
- 網(wǎng)頁(yè)緩存時(shí)間設(shè)置
- 更改Nginx運(yùn)行進(jìn)程數(shù)
- 連接超時(shí)
- nginx防盜鏈設(shè)置
- 盜鏈網(wǎng)站
- 配置httpd
- 日志分割
- fpm參數(shù)優(yōu)化
隱藏Nginx版本號(hào)
配置nginx服務(wù)
編譯環(huán)境
[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel [root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz#創(chuàng)建用戶nginx
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx [root@promote conf]# 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" instart)$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@promote conf]# chmod +x /etc/init.d/nginx [root@promote conf]# service nginx stop [root@promote conf]# service nginx start查看版本號(hào)
[root@localhost sbin]# curl -I http://192.168.136.88 HTTP/1.1 200 OK Server: nginx/1.12.2 版本號(hào) Date: Mon, 10 Aug 2020 17:06:32 GMT Content-Type: text/html Content-Length: 636 Last-Modified: Mon, 10 Aug 2020 16:05:29 GMT Connection: keep-alive ETag: "5f317049-27c" Accept-Ranges: bytes修改方法1
vim /usr/local/nginx/conf/nginx.conf http {include mime.types;default_type application/octet-stream;server_tokens off; //添加....}查看版本號(hào)
[root@localhost sbin]# curl -I http://192.168.136.88 HTTP/1.1 200 OK Server: nginx 版本號(hào) Date: Mon, 10 Aug 2020 17:06:32 GMT Content-Type: text/html Content-Length: 636 Last-Modified: Mon, 10 Aug 2020 16:05:29 GMT Connection: keep-alive ETag: "5f317049-27c" Accept-Ranges: bytes方法2
在編譯前修改 [root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel [root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz#創(chuàng)建用戶nginxv
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx開啟編譯
[root@promote nginx-1.12.2]# vim /opt/nginx-1.12.2/src/core/nginx.h#define nginx_version 1012002 #define NGINX_VERSION "2.2.2" //修改 #define NGINX_VER "nginx/" NGINX_VERSION 編譯安裝?``` [root@localhost opt]# cd 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 sbin]# cd /usr/local/nginx/sbin/ [root@localhost sbin]# nginx查看一下
[root@localhost sbin]# curl -I http://192.168.136.88 HTTP/1.1 200 OK Server: nginx/2.2.2 版本號(hào) Date: Mon, 10 Aug 2020 17:06:32 GMT Content-Type: text/html Content-Length: 636 Last-Modified: Mon, 10 Aug 2020 16:05:29 GMT Connection: keep-alive ETag: "5f317049-27c" Accept-Ranges: bytes網(wǎng)頁(yè)壓縮
編譯環(huán)境
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel [root@localhost ~]# useradd -M -s /sbin/nologin nginx編譯安裝
[root@promote ~]# cd /opt/[root@promote opt]#tar zxvf nginx-1.12.2.tar.gz[root@promote opt]# cd 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路徑優(yōu)化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin網(wǎng)頁(yè)壓縮配置
gzip on; #開啟gzip壓縮功能
gzip_ min_ length 1k; #壓縮閾值
gzip_ buffers 4 16k; #buffer大小為4個(gè)1 6k緩沖區(qū)大小
gzip_ http_ version 1.1; #壓縮版本
gzip_ comp_ level 6; #壓縮比率,最小為1,處理速度快,傳輸速度慢,9最大壓縮比,處理速度慢,傳輸適中選6
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以下不啟用gzip
gzip_ vary on; #選擇支持very header可以讓前端的緩存服務(wù)器緩存經(jīng)過(guò)gzip壓縮的頁(yè)面
添加圖片
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/ [root@promote html]# ll 總用量 44 -rw-r--r--. 1 root root 537 8月 10 18:35 50x.html -rw-r--r--. 1 root root 33086 8月 10 18:58 game.jpg //添加圖片 vim /usr/local/nginx/html/index.html <h1>Welcome to nginx!</h1> <img src="game.jpg"/> //添加圖片信息開啟服務(wù)
[root@localhost nginx-1.12.2]# 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@promote html]# iptables -F [root@promote html]# setenforce 0 [root@localhost nginx-1.12.2]# nginx軟件轉(zhuǎn)包看到圖片已經(jīng)壓縮成gzip格式
網(wǎng)頁(yè)緩存時(shí)間
編譯環(huán)境
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel編譯安裝
[root@promote ~]# cd /opt/[root@promote opt]#tar zxvf nginx-1.12.2.tar.gz[root@promote opt]# cd 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路徑優(yōu)化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin添加圖片
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/ [root@promote html]# ll 總用量 44 -rw-r--r--. 1 root root 537 8月 10 18:35 50x.html -rw-r--r--. 1 root root 33086 8月 10 18:58 game.jpg //添加圖片 vim /usr/local/nginx/html/index.html <h1>Welcome to nginx!</h1> <img src="game.jpg"/> //添加圖片信息網(wǎng)頁(yè)緩存時(shí)間設(shè)置
[root@promote html]# vim /usr/local/nginx/conf/nginx.conf location ~\.(gif|jpg|jepg|png|ico)$ {root html;expires 1d; } [root@promote html]# cd /usr/local/nginx/conf/ [root@promote conf]# nginx [root@promote html]# setenforce 0 [root@localhost nginx-1.12.2]# nginx用軟件查看一下是否修改
更改Nginx運(yùn)行進(jìn)程數(shù)
在高并發(fā)場(chǎng)景,需要啟動(dòng)更多的Nginx進(jìn)程以保證快速響應(yīng),以處理用戶的請(qǐng)求,避免造成阻塞
修改配置文件的worker_processes參數(shù)
一般設(shè)為CPU的個(gè)數(shù)或者核數(shù)
在高并發(fā)情況下可設(shè)置為CPU個(gè)數(shù)或者核數(shù)的2倍
增加進(jìn)程數(shù),可減少系統(tǒng)的開銷,提升了服務(wù)速度
使用ps aux查看運(yùn)行進(jìn)程數(shù)的變化情況
通常需要用到命令(cat /proc/meminfo)查看內(nèi)核數(shù)據(jù)結(jié)構(gòu)[root@promote conf]# cat /proc/cpuinfo | grep -c "physical" 查了開了幾個(gè)進(jìn)程 2 [root@promote sbin]# vim /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 4; 將默認(rèn)的1修改為4 [root@promote sbin]# ps aux | grep nginx root 18085 0.0 0.0 20544 612 ? Ss 20:04 0:00 nginx: master process nginx nginx 18086 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process nginx 18087 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process nginx 18088 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process nginx 18089 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process root 18121 0.0 0.0 112824 980 pts/1 R+ 20:07 0:00 grep --c連接超時(shí)
Nginx使用keepalive_ timeout來(lái)指定KeepAlive的超時(shí)時(shí)間(timeout) 。
指定每個(gè)TCP連接最多可以保持多長(zhǎng)時(shí)間。Nginx的默認(rèn)值是65秒,有些瀏覽器最多只保持60秒,
若將它設(shè)置為0,就禁止了keepalive連接。
nginx防盜鏈設(shè)置
編譯環(huán)境
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel [root@localhost ~]# useradd -M -s /sbin/nologin nginx編譯安裝
[root@promote ~]# cd /opt/[root@promote opt]#tar zxvf nginx-1.12.2.tar.gz[root@promote opt]# cd 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路徑優(yōu)化
[root@localhost nginx-1.12.2]# ln /usr/local/nginx/sbin/nginx /usr/local/bin/添加圖片
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/ [root@promote html]# ll 總用量 44 -rw-r--r--. 1 root root 537 8月 10 18:35 50x.html -rw-r--r--. 1 root root 33086 8月 10 18:58 game.jpg //添加圖片 vim /usr/local/nginx/html/index.html <h1>Welcome to nginx!</h1> <img src="game.jpg"/> //添加圖片信息配置dns
[root@localhost html]# vim /etc/named.conf options {listen-on port 53 { any; };listen-on-v6 port 53 { ::1; };directory "/var/named";dump-file "/var/named/data/cache_dump.db";statistics-file "/var/named/data/named_stats.txt";memstatistics-file "/var/named/data/named_mem_stats.txt";recursing-file "/var/named/data/named.recursing";secroots-file "/var/named/data/named.secroots";allow-query { any; };[root@localhost html]# vim /etc/named.rfc1912.zones zone "kgc.com" IN {type master;file "kgc.com.zone";allow-update { none; }; [root@localhost named]# cp -p named.loopback kgc.com.zone [root@localhost named]# vim kgc.com.zone www IN A 192.168.136.88開啟服務(wù)
[root@localhost nginx-1.12.2]# 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@promote html]# iptables -F [root@promote html]# setenforce 0 [root@localhost nginx-1.12.2]# nginx盜鏈網(wǎng)站
配置httpd
[root@localhost named]# vim /etc/httpd/conf/httpd.conf ServerName www.text.com:80 Listen 192.168.136.10:80 #Listen 80設(shè)置盜鏈網(wǎng)站
[root@localhost named]# vim /var/www/html/index.html <h1> this is dao tu web </h1> <img src="http://www.kgc.com/game.jpg" /> [root@localhost named]# systemctl restart httpd [root@localhost named]# systemctl stop firewalld [root@localhost named]# iptables -F [root@localhost named]# setenforce 0在源地址設(shè)置放盜鏈
vim /usr/local/nginx/conf/nginx.conflocation / {root html;index index.html index.htm;}location ~*\.(jpg|gif|swf)$ { ///添加valid_referers none blocked *.kgc.com kgc.com;if ( $invalid_referer ) {rewrite ^/ http://www.kgc.com/error.png;}}[root@promote conf]# cd /usr/local/nginx/html/ [root@promote html]# ll 總用量 80 -rw-r--r--. 1 root root 537 8月 13 09:18 50x.html -rw-r--r--. 1 root root 31566 8月 10 22:46 error.jpg [root@localhost sbin]# 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 sbin]# nginx日志分割
[root@localhost opt]# vim fenge.sh #!/bin/bash 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} #自動(dòng)創(chuàng)建日志 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開啟權(quán)限
[root@localhost opt]# chmod +x fenge.sh查看
[root@localhost nginx]# date -s 08/10/20 修改日期 2020年 08月 10日 星期一 00:00:00 CST [root@localhost nginx]# cd /var/log/nginx/ [root@localhost nginx]# ls test.com-access.log-20200810fpm參數(shù)優(yōu)化
vim php-fpm.conf pid=run/php-fpm.pid pm=dynamic pm.max_children=20 //static模式下空閑進(jìn)程數(shù)上限,大于下面的值 pm.start_servers=5 //動(dòng)態(tài)方式下默認(rèn)開啟的進(jìn)程數(shù),在最小和最大之間 pm.min_spare_servers=2 //動(dòng)態(tài)方式下最少的空閑進(jìn)程數(shù) pm.max_spare_servers=2 //動(dòng)態(tài)方式下最大的空閑進(jìn)程數(shù)總結(jié)
以上是生活随笔為你收集整理的Nginx优化与防盗链(隐藏版本号、配置缓存时间、日志分割、修改进程数、配置连接超时、使用gzip压缩页面、防盗链设置,fpm优化)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 内存大比拼:ROG Zephyrus S
- 下一篇: 内存频率揭秘:为何频率越高,电脑性能越强