+++++++++++++++++
優化參數:
+++++++++++++++++
vim /usr/local/nginx/conf/nginx.conf 文件下:
worker_processes 1;
worker_rlimit_nofile 100000;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on; (提升速類)
access_log off;
error_log error.log crit;
keepalive_timeout 10; (如果客戶打開該網頁,長時間沒請求,佔着不用。服務端可以設置多長時間,斷掉該客戶端連接)
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
include mime.types;
default_type text/html;
charset UTF-8;
gzip on; (壓縮頁面中 大於1000字節 壓縮格式類型)([root@proxe conf]# vim /usr/local/nginx/conf/mime.types, application/msword doc;)
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss
text/javascript;
client_header_buffer_size 1k;(當頭部信息比較大,報414錯時 加上這條和下面這條 )
large_client_header_buffers 4 4k;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
worker_processes 1; (cpu核心數量一致)
linux最大打開文件數量1024
worker_connections 65556;
ulimit -a (系統默認值)
...
open files 1024
++++++++++++
優化案例:
+++++++++++++
———————————————————————————————————————————
做併發連接數
1.[root@proxe conf]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10000;
}
[root@proxe conf]# nginx -s reload
2.vim /etc/security/limits.conf (最下面有模板)
* soft nofile 100000
* hard nofile 100000
3.
ulimit -a
ulimit -Hn 100000
ulimit -Sn 100000
ulimit -a
[root@proxe conf]# ab -c 5000 -n 5000 http://192.168.4.5/ (OK)
壓力測試:ab (yum中下一個httpd-tools)
[root@proxe conf]# ab -c 50 -n 5000 http://192.168.4.5/
——————————————————————————————————
安全設置 (屏蔽nginx版本號)
[root@proxe conf]# curl -I 192.168.4.5
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.8.0
Date: Thu, 16 Feb 2017 13:36:21 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: http://www.b.com/b.html
[root@proxe conf]# vim /usr/local/nginx/conf/nginx.conf
http {
server_tokens off; (加這個屏蔽版本號)
include mime.types;
default_type application/octet-stream;
[root@proxe conf]# nginx -s reload
[root@proxe conf]# curl -I 192.168.4.5
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Thu, 16 Feb 2017 13:36:42 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://www.b.com/b.html
———————————————————————————————————————————————————————————
解決客戶機訪問頭部信息過長的問題。
當訪問時輸入的地址頭部信息過長時報414錯誤時,查看buffer,默認時爲1,改成下面這兩行就行。若是發現本來buffers就設置爲4 4k時,
不用再改了,可能是別人惡意***,最大 不要改到4 8k)
client_header_buffer_size 1k;(當頭部信息比較大,報414錯時 加上這條和下面這條 )
large_client_header_buffers 4 4k;
————————————————————————————————————————————————————————————
在客戶機上做緩存
在服務器中設置格式爲pdf|jpg|mp3|png的文件,使打開該格式頁面文件的客戶機瀏覽器中緩存30天(一般只做靜態緩存)
[root@proxe ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.(pdf|jpg|mp3|png) ${
expires 30d;
}
[root@proxe ~]# cp knowledge\ point\(2.1\).pdf /usr/local/nginx/html/a.pdf
[root@proxe ~]# nginx -s reload
[root@host ~]# firefox http://192.168.4.5/a.pdf
在打開的瀏覽器(firefox)地址欄中:輸入 about:cache 可以看到剛打開的頁面 找到它可以看到從哪天保存到哪天。(做實驗時先把瀏覽器緩存清空)
——————————————————————————————————
如何解決客戶端訪問頭部信息過長的問題?
當客戶端訪問該網站時,輸入的頭部信息地址過大,出現的頁面報414錯時,把默認的buffer_size 1k改成4 4k.
(如果出現該錯時,先不急着改,先看是否寫了large_client_header_buffers 4 4k;要是寫了就不用改,完全夠用,預防別人是惡意***)
[root@proxe ~]# vim /usr/local/nginx/conf/nginx.conf
client_header_buffer_size 1k;(當頭部信息比較大,報414錯時 加上這條和下面這條 )
large_client_header_buffers 4 4k;
——————————————————————————————————
防止盜鏈
Referer:告訴服務器,從哪裏來
訪問新浪:可以直接訪問新浪,也可從百度中鏈接過去,但referer不一樣。
------>sina
baidu ----->sina
referer:www.baidu.com
referer:www.sina.com
www.youku.com www.letv.com
www.bird.org :所有資源(做鏈接),搜索功能 目的是擴大自己網站的影響和點擊,讓別人知道自己域名。
防止盜鏈
vaild_referers (有效的 允許鏈接)
if 拒絕的
實驗操作:
location ~*\. (pdf|jpg|mp3|png|flv) ${
vaild_referers none blocked www.tarena.com;
if($invalid_referer){
rewrite ^/ http:www.a.com/a.html
}
}
————————————————————————————————————————————————————————