<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 基本安全優化 ## 隱藏版本信息 軟件的漏洞都和版本相關,要隱藏或消除web服務對訪問用戶顯示的各種敏感信息。 ![](https://img.kancloud.cn/c7/f6/c7f63e5c1f22afb5d605dd4a9b4bf25e_337x131.png) 修改 nginx.conf: ~~~text http { …… server_tokens off; # 關閉 Nginx 版本號 …… ~~~ # 常用配置優化 ## worker_processes 工作進程的數量, 一般設置成 CPU 核的數量。同時配合 `worker_cpu_affinity` (用于worker process與指定cpu核綁定)。 例: ~~~shell # 分別給每個worker進程綁定一個CPU. worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; # 將CPU0/CPU2綁定給第一個worker進程,將CPU1/CPU3綁定給第二個worker進程。 worker_processes 2; worker_cpu_affinity 0101 1010; ~~~ > 一般設置為 `auto` (自動檢測) ## worker_connections 單個進程允許的客戶端最大連接數。 ~~~text events { …… worker_connections 1024; …… } ~~~ ## 配置nginx worker進程最大打開文件數 `worker_rlimit_nofile` 改變nginx 工作進程能打開的最大文件數。受系統文件的最大打開數限制。 ~~~shell # 查看系統文件的最大打開數 [root@admin nginx]# cat /proc/sys/fs/file-max 1299568 # 文件系統最大可打開文件數 [root@admin nginx]# ulimit -n 1024 ~~~ 配置: ~~~ …… events { …… worker_rlimit_nofile 2048; ~~~ ## 開啟高效文件傳輸模式 設置參數`sendfile on` 用于開啟文件的高效傳輸模式。同時將`tcp_nopush`和`tcp_nodelay`兩個指令設置為on,可防止網絡及磁盤i/o阻塞,提升nginx工作效率。 ~~~text http { …… sendfile on; tcp_nopush on; tcp_nodelay on; ~~~ ## 配置 Gzip 壓縮 nginx gzip壓縮模塊提供了壓縮文件內容的功能,用戶請求的內容在發送給客戶端之前,nginx服務器會根據一些具體的策略實施壓縮,以節省網站出口帶寬,同時加快了數據傳輸效率,提升了用戶的訪問體驗。 > https://nginx.org/en/docs/http/ngx_http_gzip_module.html ### 壓縮的對象 * 純文本內容壓縮比很高,因此,純文本的內容最好要壓縮 * 被壓縮的純文本文件必須要大于1KB,由于壓縮算法的特殊原因,極小的文件壓縮反而變大 * 圖片、視頻(流媒體)等文件盡量不要壓縮,因為這些文件大多都是經過壓縮的,如果再壓縮很可能不會減小或減小很少,或者有可能增大,而在壓縮時還會消耗大量的CPU、內存資源 ### 配置參數 ~~~text http { …… ## # Gzip Settings ## gzip on; gzip_vary on; # vary header支持 # gzip_proxied any; gzip_comp_level 6; # 壓縮比率 gzip_buffers 16 8k; # 壓縮緩存區大小 gzip_http_version 1.1; # 壓縮版本 gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 指定壓縮的類型 ~~~ ## nginx expires功能 為用戶訪問網站的內容設定一個過期時間,當用戶第一次訪問到這些內容時,會把這些內容存儲在用戶瀏覽器本地,這樣用戶第二次及之后繼續訪問該網站,瀏覽器就會檢查已經緩存在用戶瀏覽器本地的內容,就不會去瀏覽器下載了,直到緩存的內容過期或者被清除為止。 ### 作用和優點 * 可以降低網站的帶寬,節約成本 * 加快用戶訪問網站的速度,提升了用戶訪問體驗 * 服務器訪問量降低,服務器壓力就減輕了,服務器的成本也會降低 ### 配置詳解 ~~~text location ~ ^/(images|javascript|js|css|flash|media|static)/ { expires 360d; } ~~~ ### 缺點 當網站被緩存的頁面或數據更新了,此時用戶端看到的可能還是舊的已經緩存的內容。 * 對于經常需要的變動圖片等文件,可以縮短對象緩存時間 * 當網站改版或更新內容時,可以在服務器將緩存的對象改名 ## nginx日志的優化 * 編寫腳本實現日志輪詢 以通過利用腳本開發、nginx的信號控制功能或reload重新加載,來實現日志的自動切割、輪詢 ~~~shell mv www_access.log www_access_$(date +F -d -1day).log nginx -s reload ~~~ * 不記錄不需要的日志 對于負載均衡器健康檢查節點或某些特定的文件(圖片、JS、CSS)的日志,一般不需要記錄下來 * 訪問日志的權限設置 假如日志目錄為/app/logs,則授權方法為: ~~~shell chown -R root.root /app/logs chmod -R 600 /app/logs ~~~ 不需要在日志目錄上給nginx用戶讀寫或者讀寫許可 ## Nginx站點目錄及文件URL訪問控制 ### 根據擴展名限制程序和文件訪問 web2.0時代,絕大多數網站都是以用戶為中心的,這些產品有一些共同點,就是不允許用戶發布內容到服務器,還允許用戶發圖片甚至附件上傳到服務器上,給用戶開啟了上傳的功能。帶來了很大的安全隱患。 下面將利用nginx配置禁止訪問上傳資源目錄下的 `php`,`shell`,`perl`,`python`程序文件,這樣就算是用戶上傳了木馬文件也沒辦法執行 ~~~text 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服務配置的前面 ### 禁止訪問指定目錄下的所有文件和目錄 配置禁止訪問指定的單個或多個目錄 ~~~text location ~ ^/(static)/ { deny all; } location ~ ^/static { deny all; } ~~~ 禁止訪問目錄并且返回代碼404 ~~~text server { listen 80; server_name xxxxx; root /var/www/xxxxx; index index.html index.htm; access_log /var/logs/www_access.log commonlog; location /admin/ { return 404; } location /templates/ { return 403; } } ~~~ ### 限制網站來源的IP訪問 使用[`ngx_http_access_module`](https://nginx.org/en/docs/http/ngx_http_access_module.html)限制網站來源IP訪問。 `allow`:允許,`deny`:拒絕 ~~~text location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; } ~~~ ### 配置nginx禁止非法域名解析訪問網站 防止用戶IP訪問網站(惡意域名解析,相當于直接使用IP訪問網站) ~~~text server { listen 80 default_server; server_name _; return 501; } ~~~ ## nginx圖片防盜鏈解決方案 * 根據HTTP referer實現防盜鏈 * 根據cookie防盜鏈 利用Nginx [`ngx_http_referer_module`](https://nginx.org/en/docs/http/ngx_http_referer_module.html)模塊,用于阻止對“Referer”標頭字段中具有無效值的請求的訪問。 ~~~text location ~* ^.+\.(jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked *.xxx.com; if ($invalid_referer) { return 404; } } ~~~ `valid_referers [none|blocked|server_names|string ...] ` 默認值:none none:請求標頭中缺少“Referer”字段; blocked:“Referer”字段存在于請求標頭中,但其值已被防火墻或代理服務器刪除;此類值是不以“ http://”或“ https://”開頭的字符串; server_names:“Referer”請求標頭字段包含服務器名稱之一; # 使用CDN做網站內容加速 * 本地Cache加速 提高了企業站點(尤其含有大量圖片和靜態頁面站點)的訪問速度,并大大提高以上性質站點的穩定性 * 鏡像服務 消除了不同運營商之間互聯的瓶頸造成的影響,實現了跨運營商的網絡加速,保證不同網絡中的用戶都能得到良好的訪問質量 * 遠程加速 遠程訪問用戶根據DNS負載均衡技術智能自動選擇Cache服務器,選擇最快的Cache服務器,加快遠程訪問的速度 * 帶寬優化 自動生成服務器的遠程Mirror(鏡像)cache服務器,遠程用戶訪問時從cache服務器上讀取數據,減少遠程訪問的帶寬、分擔網絡流量、減輕原站點WEB服務器負載等功能。 * 集群抗攻擊 廣泛分布的CDN節點加上節點之間的智能冗余機制,可以有效地預防黑客入侵以及降低各種D.D.o.S攻擊對網站的影響,同時保證較好的服務質量 。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看