nginx將發送給客戶端的響應報文先壓縮,這樣就能夠節約網絡帶寬和更快的傳輸至客戶端。在客戶端瀏覽器進行解壓。javascript
打開nginx配置文件 nginx.conf
裏面添加或者用include引用php
# 打開壓縮模塊 gzip on; # 最小多少就不壓縮了 gzip_min_length 1k; # buffer緩衝 gzip_buffers 4 16k; # 使用http那個版本。有1.0和1.1,主流是1.1 gzip_http_version 1.1; # gzip壓縮率。有1到9,數字越大壓縮比例越大,同時耗費性能越大 gzip_comp_level 2; # 進行壓縮的文件類型 gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png; # no Vary gzip_vary off; # IE1到6不支持壓縮,這裏就把它踢出去了 gzip_disable "MSIE [1-6]\.";
保存從新加載配置文件 nginx -s reload
css
最後用 curl
命令測試一下
curl -I -H "Accept-Encoding: gzip, deflate" "網站域名/各類文件類型"
html
不一樣於動態壓縮,靜態壓縮是對請求資源優先查找以.gz結尾的文件,有就直接發送.gz壓縮文件,沒有再走正常請求流程。全部靜態壓縮只針對靜態資源並同目錄下有相應以.gz的壓縮包java
在nginx配置文件 server
標籤里加入下面內容(這裏爲了小範圍測試就不寫在http標籤了)node
# 匹配後綴是png格式的圖片 location ~ .*\.png$ { # 打開靜態壓縮,須要打開gzip模塊 gzip_static on; # nginx做爲反向代理時候header頭包含什麼信息啓動壓縮 gzip_proxied expired no-cache no-store private auth; }
保存從新加載配置文件 nginx -s reload
並把png格式的圖片壓縮一下
tar -zcvf test.png.gz test.png
最後用 curl -I -H "Accept-Encoding: gzip, deflate" 網站域名加圖片url
測試一下python
Content-Length
和壓縮文件大小比對一下linux
利用客戶瀏覽器來緩存網站上不常常變動的圖片,是給服務器減壓的一個很方便且實用的作法。好比,網站的logo等,只要不是常常變動的,大均可以讓其存在客戶端,提升訪問速度,減少服務器的壓力nginx
打開nginx配置文件在 location
標籤中添加expiresweb
expires 40s; //表示把數據緩存40秒
expires 20m; //表示把數據緩存20分
expires 10h;//表示把數據緩存10小時
expires 3d;//表示把數據緩存3天
# 匹配後綴是jpg格式的圖片 location ~ .*\.jpg$ { # 被緩存文件的路徑 root /soft/code/; # 緩存三天 expires 3d; }
保存從新加載配置文件 nginx -s reload
打開網站圖片測試一下
nginx的鏈接處理機制在於不一樣的操做系統會採用不一樣的I/O模型,Linux下,nginx使用epoll的I/O多路複用模型,在freebsd使用kqueue的IO多路複用模型,在solaris使用/dev/pool方式的IO多路複用模型,在windows使用的icop等等。
打開nginx配置文件 nginx.conf
裏面添加
events { use epoll; # 限制每一個進程處理多少個鏈接請求 worker_connections 1024; }
nginx每一個版本都用漏洞,即便最新版也是暫時沒有漏洞,不可能實時更新到最新版。爲了防被被人針對版本漏洞攻擊web服務器,有必要隱藏nginx版本號
先用 curl -I 網站域名
檢測一下 Server
裏面的
打開nginx配置文件 nginx.conf
在 http
標籤添加 server_tokens off;
就能夠了
http { # 隱藏nginx版本號 server_tokens off;
再用 curl -I 網站域名
檢測一下 Server
裏面的
若是是用源碼包,編譯安裝且未安裝的,能夠直接在源碼包上修改版本號
下載且解壓完源碼包(這裏就介紹怎麼下載和解壓了)
vim nginx-1.14.2/src/core/nginx.h +13
vim nginx-1.14.2/src/http/ngx_http_header_filter_module.c +49
vim nginx-1.14.2/src/http/ngx_http_special_response.c +36
接下來就是編譯安裝nginx,再用 curl -I 網站域名
檢測一下
盜鏈指的是在本身的界面展現不在本身服務器上的內容,經過技術手段得到他人服務器的資源地址,繞過別人資源展現頁面,在本身頁面向用戶提供此內容,從而減輕本身服務器的負擔,由於真實的空間和流量來自別人服務器
防盜鏈 valid_referers
參數
none : 容許沒有http_refer的請求訪問資源;
blocked : 容許不是http://開頭的,不帶協議的請求訪問資源;
119.28.190.215 : 只容許指定ip來的請求訪問資源;
在nginx配置文件 server
標籤里加入下面內容
# 匹配後綴是jpg格式的圖片(有同樣的匹配科技直接在裏面寫) location ~ .*\.jpg$ { valid_referers none blocked www.songguoyou.com; if ($invalid_referer) { return 403; } }
保存從新加載配置文件 nginx -s reload
用 curl -e "域名網站"
測試一下
# 用本身網站域名訪問 [root@nginx ~]# curl -I -e "http://www.songguoyou.com" http://www.songguoyou.com/wp-content/test/test.jpg HTTP/1.1 200 OK Server: nginx Date: Wed, 27 Feb 2019 07:17:10 GMT Content-Type: image/jpeg Content-Length: 19452 Last-Modified: Sun, 10 Jun 2018 12:16:57 GMT Connection: keep-alive ETag: "5b1d16b9-4bfc" Accept-Ranges: bytes # 用別人網站域名訪問 [root@nginx ~]# curl -I -e "http://www.baidu.com" http://www.songuoyou.com/wp-content/test/test.jpg HTTP/1.1 403 Forbidden Server: nginx Date: Wed, 27 Feb 2019 07:17:14 GMT Content-Type: text/html; charset=utf-8 Content-Length: 146 Connection: keep-alive
在實際工做中,對於負載均衡器健康檢查節點或某些特定的文件(圖片、JS、CSS)的日誌,通常不須要記錄下來,由於在統計PV時是按照頁面計算的,並且日誌寫入的太頻繁會消耗磁盤i/o,下降服務的性能
在nginx配置文件 server
標籤里加入下面內容
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ { access_log off; }
網站都是以用戶爲中心的,這些產品有一些共同點,就是不容許用戶發佈內容到服務器,還容許用戶發圖片甚至附件上傳到服務器上,給用戶開啓了上傳的功能。帶來了很大的安全隱患。
在nginx配置文件 server
標籤中禁止訪問上傳資源目錄下的PHP,SHELL,PERL,PYTHON程序文件,這樣就算是用戶上傳了木馬文件也沒辦法執行
location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)${ deny all; } location ~ ^/static/.*\.(php|php5|.sh|.pl|.py)${ deny all; } location ~* ^/data/(attachment|avatar)/.*\.(php|php5)${ deny all; }
對於上述目錄的限制必須寫在nginx處理PHP服務配置的前面
配置禁止訪問指定的單個或多個目錄
location ~ ^/(static)/ { deny all; } location ~ ^/static { deny all; }
禁止訪問目錄而且返回代碼404
location /static/ { return 404; }
打開nginx配置文件 nginx.conf
裏面添加或者用include引用
fastcgi_connect_timeout 240; # Nginx和FastCGI鏈接超時時間 fastcgi_send_timeout 240; # Nginx容許FastCGI返回數據超時時間 fastcgi_read_timeout 240; # Nginx從FastCGI讀取響應信息超時時間 fastcgi_buffer_size 64k; # 讀取fastCGI應答第一部分須要多大緩衝區 fastcgi_buffers 4 64k; # 設定用來讀取從FastCGI端收到響應信息的緩衝區大小和數量 fastcgi_busy_buffers_size 128k; # 用於設置系統很忙時可使用的proxy_buffers大小 fastcgi_temp_file_write_size 128k; # FastCGI臨時文件的大小 # fastcti_temp_path /data/ngx_fcgi_tmp; # 從其餘服務器傳送臨時文件存放路徑 fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; # 緩存目錄(手動建立)
調高linux內核打開文件數量,可使用這些命令(必須是root賬號)
echo `ulimit -HSn 65535` >> /etc/profile echo `ulimit -HSn 65535` >> /etc/rc.local source /etc/profile
若是ulimit -n
數量依舊很少的話, 能夠在 /etc/security/limits.conf 文件最後加上
* soft nofile 51200 * hard nofile 51200
在 www
池裏的優化
user = nginx # 進程的發起用戶(和web容器同一個用戶) group = nginx #進程的發起用戶組(和web容器同一個用戶組) # listen = 127.0.0.1:9000 # 監聽ip端口(我這裏用的socket方式訪問,因此註釋了) listen = /dev/shm/php-fastcgi.sock listen.owner = nginx listen.group = nginx listen.mode = 0666 # unix socket設置選項,若是使用tcp方式訪問,上面四行註釋便可 listen.allowed_clients = 127.0.0.1 pm = dynamic; # 表示使用(dynamic|static)兩種進程數量管理方式 pm.max_children = 300 # 靜態下開啓的php-fpm進程數量(pm=static生效) pm.start_servers = 20 # 動態下的起始php-fpm進程數量(pm=dynamic生效) pm.min_spare_servers = 5 # 動態下的最小php-fpm進程數量(pm=dynamic生效) pm.max_spare_servers = 35 # 動態下的最大php-fpm進程數量(pm=dynamic生效) pm.max_requests = 10240 # 表示使用最大請求數 request_terminate_timeout = 30 # 最大執行時間, 在php.ini中也能夠進行配置(max_execution_time) request_slowlog_timeout = 2 # 開啓慢日誌 slowlog = log/$pool.log.slow # 慢日誌路徑 rlimit_files = 1024 # 增長php-fpm打開文件描述符的限制
nginx和php鏈接配置
location ~ \.php$ { # fastcgi_pass 127.0.0.1:9000; # 監聽ip端口(我這裏用的socket方式訪問,因此註釋了) fastcgi_pass unix:/dev/shm/php-fastcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /soft/code/wordpress$fastcgi_script_name; include fastcgi_params;
safe_mode = On # 啓用安全模式 safe_mode_exec_dir = Off # 關閉用戶組安全(啓動安全模式自動關閉,但最好設爲關閉) disable_functions = passthru,exec,system,popen,chroot,escapeshellcmd,escapeshellarg # 禁用危險函數 expose_php = Off # # 隱藏php版本號 register_globals = Off # 關閉註冊全局變量 magic_quotes_pgc = On # 轉換SQL命令,防止SQL注入 display_errors = Off # 關閉錯誤信息提示,防止信息泄露 log_errors = On # 打開錯誤日誌 max_execution_time = 30 # 最大執行時間, 在php-fpm中也能夠進行配置(request_terminate_timeout) memory_limit = 128M # 腳本使用的最大內存 max_input_time = 60 # 每一個腳本等待輸入數據最長時間(秒),-1表示不限制 upload_max_filesize = 2M # 上傳文件的最大許可大小 allow_url_fopen = Off # 禁止打開遠程地址 cgi.fix_pathinfo=0 # 防止Nginx文件類型錯誤解析漏洞 session.save_handler = files # 會話默認爲文件("files") # 自定義的處理器(如基於數據庫的處理器),可用"user" # 設爲"memcache"則可使用memcache做爲會話處理器(須要指定"--enable-memcache-session"編譯選項) session.save_path = "/tmp" # 會話保存路徑 # 配置memcache # session.save_handler = memcache # session.save_path = "tcp://127.0.0.1:11211"
sendfile系統調用在兩個文件描述符之間直接傳遞數據(徹底在內核中操做),從而避免了數據在內核緩衝區和用戶緩衝區之間的拷貝,操做效率很高,被稱之爲零拷貝。
http { # 零拷貝 sendfile on; # 啓用TCP_CORK,建議打開,須要打開sendfile。默認關閉 tcp_nopush on; # 禁用Nagle算法,建議打開,須要打開keepalived。默認關閉 tcp_nodelay on; }
HTTP容易傳輸數據被中間人盜用, 信息泄露露和數據內容劫持, 篡改等,因此使用HTTPS
配置蘋果要求的證書
1.服務器器全部鏈接使用TLS1.2以上版本(openssl 1.0.2)
2.HTTPS證書必須使用SHA256以上哈希算法簽名
3.HTTPS證書必須使用RSA 2048位或ECC256位以上公鑰算法
4.使用前向加密技術
是把進程綁定到相應的CPU內核上,減小進程之間不斷頻繁遷移,也減小CPU性能損耗
能夠用 lscpu|grep "CPU(s)"
命令查看物理狀態,多少核心
在 nginx.conf
的最外面添加下面兩行
worker_processes auto; worker_cpu_affinity auto;
保存從新加載配置文件後,用 ps -eo pid,args,psr|grep [n]ginx
查看 nginx worker
綁定狀況
最後來個 Nginx
通用優化配置文件
user nginx; # CPU親和 worker_processes auto; worker_cpu_affinity auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; # 限制每一個進程處理多少個鏈接請求 worker_connections 1024; } http { # 隱藏nginx版本號 server_tokens off; include /etc/nginx/mime.types; default_type application/octet-stream; # 統一使用utf-8字符集 charset utf-8; 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 /var/log/nginx/access.log main; # 零拷貝 sendfile on; # 啓用TCP_CORK,建議打開,須要打開sendfile。默認關閉 tcp_nopush on; # 禁用Nagle算法,建議打開,須要打開keepalived。默認關閉 tcp_nodelay on; keepalive_timeout 65; # 打開壓縮模塊 gzip on; # 最小多少就不壓縮了 gzip_min_length 1k; # buffer緩衝 gzip_buffers 4 16k; # 使用http那個版本。有1.0和1.1,主流是1.1 gzip_http_version 1.1; # gzip壓縮率。有1到9,數字越大壓縮比例越大,同時耗費性能越大 gzip_comp_level 2; # 進行壓縮的文件類型 gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png; # no Vary gzip_vary off; # IE1到6不支持壓縮,這裏就把它踢出去了 gzip_disable "MSIE [1-6]\."; fastcgi_connect_timeout 240; # Nginx和FastCGI鏈接超時時間 fastcgi_send_timeout 240; # Nginx容許FastCGI返回數據超時時間 fastcgi_read_timeout 240; # Nginx從FastCGI讀取響應信息超時時間 fastcgi_buffer_size 64k; # 讀取fastCGI應答第一部分須要多大緩衝區 fastcgi_buffers 4 64k; # 設定用來讀取從FastCGI端收到響應信息的緩衝區大小和數量 fastcgi_busy_buffers_size 128k; # 用於設置系統很忙時可使用的proxy_buffers大小 fastcgi_temp_file_write_size 128k; # FastCGI臨時文件的大小 # fastcti_temp_path /data/ngx_fcgi_tmp; # 從其餘服務器傳送臨時文件存放路徑 fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; # 緩存目錄(手動建立) include /etc/nginx/conf.d/*.conf; }