啟用網站壓縮deflate
提示:以Apache為例
1.查看并啟用mod_deflate ,壓縮傳輸
查看mod_deflate:提示未找到

打開網頁查看:也沒有壓縮回應頭

2.在httpd.conf組態檔末尾,添加deflate引數配置
vim /usr/local/httpd/conf/httpd.conf
定位104行并去除“#”號:LoadModule deflate_module modules/mod_deflate.so

添加:
<IfModule mod_deflate.c>
DeflateCompressionLevel 6
SetOutputFilter DEFLATE
#AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript application/x-httpd-php
#AddOutputFilterByType DEFLATE image/*
AddOutputFilterByType DEFLATE text/*
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
</IfModule>
3.保存退出,重啟服務
systemctl restart httpd
4.驗證:


啟用網站快取expires
1、 修改網站組態檔,啟動expires模塊
vim /usr/local/httpd/conf/httpd.conf
定位110行,并去除“#”號:LoadModule expires_module modules/mod_expires.so

2、 在組態檔末尾添加,expires配置引數
ExpiresActive On
ExpiresByType text/css "now plus 1 month"
ExpiresByType application/x-javascript "now plus 5 day"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/bmp "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType image/png "access plus 1 minute"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresDefault "now plus 0 minute"
</IfModule>
3.保存退出,重啟服務
systemctl restart httpd
4、 驗證:

禁止網站目錄遍歷
1、打開httpd.conf
vim /usr/local/httpd/conf/httpd.conf
定位:Options Indexes FollowSymLinks
去除Indexes

保存退出,重啟服務
apache隱藏版本號
1.主組態檔啟用httpd-default.conf
vim /usr/local/httpd/conf/httpd.conf
定位并去除“#”號:Include conf/extra/httpd-default.conf
保存退出
2.修改httpd-default.conf配置
vim /usr/local/httpd/conf/extra/httpd-default.conf
修改:
ServerTokens Prod
ServerSignature Off
保存退出
3.重啟httpd服務
systemctl restart httpd
4.驗證:
未做優化前:

優化后:

配置httpd日志輪替切割
1.禁止默認日志保存配置
vim /usr/local/httpd/conf/httpd.conf
定位并禁用:
#ErrorLog “logs/error_log”
#CustomLog “logs/access_log” common
添加的輪替策略:
ErrorLog “|/usr/local/httpd/bin/rotatelogs -l logs/error_%Y%m%d.log 86400”
CustomLog “|/usr/local/httpd/bin/rotatelogs -l logs/access_%Y%m%d.log 86400” combined
保存退出
2.重啟httpd服務
systemctl restart httpd
3.驗證:

配置防盜鏈
注意:可以使用另一方式實作防盜鏈 referer
1.組態檔啟用rewrite模塊
vim /usr/local/httpd/conf/httpd.conf
定位并去除“#”號:LoadModule rewrite_module modules/mod_rewrite.so
保存退出
2.打開虛擬主機組態檔
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
在內部添加:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://benet.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://benet.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.benet.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.benet.com$ [NC]
RewriteRule . *\.(gif|jpg|swf)$http://www.benet.com/about/nolink.png[R,NC,L]
如圖:

保存退出
3.重啟httpd服務
systemctl restart httpd
4.驗證:
可以另開一臺安裝Apache的虛擬機,盜用此網站圖片,將會不可實作盜用(略)
在php服務器上安裝xcache快取,提高網站訪問速度
使用ab命令進行壓力測驗(目的是和做了快取優化之后做對比)
yum -y install httpd-tools
使用ab壓力測驗命令進行160人并發訪問,發出10000個請求,(注意:不能在本地服務器測驗,另開一臺測驗)

1.上傳或下載xcache安裝包
上傳xcache安裝包到虛擬機
或
網上下載:wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
2.安裝xcache
tar xf xcache-3.2.0.tar.gz
cd xcache-3.2.0/
查看是否安裝autoconf

若未安裝,則執行命令安裝:
yum -y install autoconf
進入xcache解壓目錄:

/usr/local/php5.6/bin/phpize
./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config
&&make && make install
完成如下圖:

3.創建快取檔案并賦權
touch /tmp/xcache
chmod 777 /tmp/xcache
4.復制xcache網站目錄到php(192.168.1.105)和httpd(192.168.1.109)網站根目錄下
cp -r htdocs/ /var/www/benet/xcache
或
scp -rp /var/www/benet/xcache/ root@192.168.1.109:/var/www/benet/
5.修改php組態檔,添加xcache引數,重啟php-fpm
vim /etc/php.ini
添加到檔案末尾:
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so #把xcache.so前的路徑換成自己make install的最后一行路徑
添加到檔案末尾:
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = Off
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""
保存退出
重啟服務:systemctl restart php-fpm
6.訪問httpd的xcache虛擬目錄
驗證:

訪問網頁前,命中是63,再次重繪首頁(模擬訪問)變成225

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/296844.html
標籤:其他
上一篇:「萌新指南」SOA vs. 微服務:What’s the Difference?
下一篇:docker搭建lnmp環境
