目錄
一.更改進程數
- 安裝Nginx服務
systemctl start vsftpd //開啓ftp服務
#使用wget工具從官網上下載nginx壓縮包
wget http://nginx.org/download/nginx-1.17.6.tar.gz
#解壓
tar zxvf nginx-1.12.2.tar.gz -C /opt
#安裝環境包
yum install gcc gcc-c++ pcre pcre-devel zlib-devel make -y
#創建用戶,不建立宿主文件,且不能再shell上登錄
useradd -M -s /sbin/nologin nginx
#配置,安裝且編譯
cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
#nigix運行的用戶和組都爲nginx
#啓用http_stub_status_module模塊以支持狀態統計,便於查看服務器的連接信息
make && make install
#爲主程序nginx創建軟鏈接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
#優化服務控制,service工具
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: -99 20
# description: Nginx Service Control Script
#主程序
PROG="/usr/local/nginx/sbin/nginx"
#PID號
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
#添加執行權限
chmod +x /etc/init.d/nginx
#添加爲系統服務
chkconfig --add /etc/init.d/nginx
#開啓服務#關閉防火牆
service nginx start
systemctl stop firewalld
setenforce 0
#驗證服務是否開啓
netstat -natp | grep 80
- 驗證Nginx服務
- 在高併發的壞境中,需要啓動更多的Nginx進程以保證快速響應,用以處理用戶的請求,避免造成阻塞,使用ps aux查看Nginx運行的進程個數
[root@localhost nginx-1.12.2]# ps aux | grep nginx
root 7765 0.0 0.0 20544 604 ? Ss 09:01 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 7769 0.0 0.1 23072 1640 ? S 09:01 0:00 nginx: worker process
root 42127 0.0 0.0 112728 972 pts/1 R+ 09:12 0:00 grep --color=auto nginx
[root@localhost nginx-1.12.2]#
注:其中master process是Nginx的主進程,worker process是Nginx的子進程
- 修改Nginx的配置文件中的worker_process參數,一般設置爲CPU的個數或者核數,在高併發的情況下可以設置爲Cpu的個數或者核數的2倍,可以先查看CPU的核數確定參數
[root@localhost nginx-1.12.2]# cat /proc/cpuinfo | grep -c "physical"
2
[root@localhost nginx-1.12.2]#
-
在nginx的主配置文件,設置參數爲2,與cpu的核數相同,運行進程數設置多一些,響應客戶端訪問請求時,Nginx就不會臨時啓動新的進程提供服務,減少了系統的開銷,提升了服務速度
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
...
worker_processes 2;
...
#重啓服務
[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 42392 0.0 0.0 20544 604 ? Ss 09:33 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 42393 0.0 0.1 23072 1392 ? S 09:33 0:00 nginx: worker process
nginx 42394 0.0 0.1 23072 1392 ? S 09:33 0:00 nginx: worker process
root 42406 0.0 0.0 112728 972 pts/1 R+ 09:34 0:00 grep --color=auto nginx
[root@localhost nginx-1.12.2]#
注:開啓了一個主進程和2兩個子進程,由此可見參數起到作用,當有多核cpu時,設置參數 worker_cpu_affiniity
二.網頁壓縮
- Nginx的ngx_http_gizp_module壓縮模塊提供了對文件內容壓縮的功能,允許Nginx服務器將輸出內容發送到客戶端之前進行壓縮,以節省網站的帶寬。
- 默認情況下,Nginx安裝的該模塊,只需要在配文件中加入相應的壓縮功能
#開啓壓縮功能
gzip on;
#表示申請4個單位爲16KB的內存作爲壓縮結果流緩存,默認值時申請與原始數據大小相同的內存空間來存儲gzip壓縮結果
gzip_buffers 4 64k;
#用於設置識別http協議版本,默認時1.1,目前大部分瀏覽器以及支持解壓,但是處理很慢,消耗資源多
gzip_http_version 1.1;
用來指定壓縮比,1壓縮最小,處理速度塊,9壓縮最多,處理速度慢
gzip_comp_level 2;
#用於設置允許壓縮的頁面最小字節數
gzip_min_length 1k;
#該選項可以讓前端的緩存服務器緩存經過gzip壓縮的頁面
gzip_vary on;
#壓縮類型,對那些網頁文檔啓動壓縮功能
gzip_types test/plain text/javascript application/x-javascript text/css text/html text/xml application/xml;
- 網頁壓縮操作
#在nginx的主頁面中加入圖片
[root@localhost html]# ls
1.jpg 50x.html index.html
[root@localhost html]# vim index.html
...
<img height=200px src="1.jpg"/>
...
[root@localhost html]#
- 設置壓縮參數
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
gzip on;
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 1k;
gzip_vary on;
gzip_types test/plain text/javascript application/x-javascript text/css text/xml image/jpg image/jpeg image/png image/gif application/xml application/xml+rss;
...
[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start
[root@localhost ~]#
- 測試壓縮模塊
三.防盜鏈
- 在企業網站服務中一般要配置防盜鏈功能,以避免網站被非法盜用,造成經濟損失,也避免不必要的帶寬浪費
- Nginx的防盜鏈功能非常強大,在默認情況之下,只需要進行簡單的配置,即可實現防盜鏈功能
- 防盜鏈具體操作
- 準備兩臺主機模擬盜鏈與防盜鏈
類別 | IP地址 | 域名 |
源主機 | 192.168.43.227 | www.kgc.com |
盜鏈主機 | 192.168.43.221 | www.yun.com |
- 設置源主機
[root@localhost html]# vim /etc/hosts
...
192.168.43.221 www.yun.com
192.168.43.227 www.kgc.com
...
[root@localhost named]# cd /usr/local/nginx/html
[root@localhost html]# ls
1.jpg 50x.html index.html
[root@localhost html]# vim index.html
...
<img height=200px src="1.jpg"/>
...
- 查看源主機的頁面
- 設置盜鏈主機進行盜鏈
[root@localhost named]# vim /etc/hosts
...
192.168.43.227 www.kgc.com
192.168.43.221 www.yun.com
...
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# vim index.html
...
<img height=300px src="http://www.kgc.com/1.jpg"/>
...
- 驗證盜鏈結果
- 在源主機上設置防盜鏈模塊
vim /usr/local/nginx/conf/nginx.conf
...
location ~* \.(jpg|gif|swf)$ {
valid_referers none blocked www.kgc.com; //滿足此條規則,不跳轉
if ($invalid_referer) { //如果爲真
rewrite ^/ http://www.kgc.com/error.png; //跳轉錯誤頁面
}
}
...
service nginx restart
//valid_referers,設置信任網站
- 驗證防盜鏈
四.FPM參數優化
- 在LNMP架構中php-fpm模塊處理動態請求,爲了提高PHP的處理速度,可以對FPM模塊進行參數的調整
- FPM進程有兩種啓動方式,由pm參數指定,分別是static和dynamic
- static將產生固定數據的fpm進程,可以使用pm.max_children指定的啓動的進程數量
- dynamic將以動態的方式產生fpm進程,這種方式產生的參數則要根據服務器的內存與服務器負載進行調整
- dynamic方式下的參數
選項 | 描述 |
pm.max_children | 指定啓動的進程的最大的數量 |
pm.start.servcie | 動態方式下初始的ftpm的進程數量 |
pm_mim_space_servers | 動態方式下最小的fpm空閒進程數 |
pm_max_space_servers | 動態方式下最大的fpm空閒進程數 |