Nginx用來做爲靜態資源web服務;CDN、瀏覽器緩存、跨域、防盜鏈等。
非服務器動態運行生成的文件。javascript
類型 | 種類 |
---|---|
瀏覽器端渲染 | HTML、CSS、JS |
圖片 | JPG、GIF、JPEG、PNG |
視頻 | FLV、MPEG |
文件 | TXT等 |
Syntax:sendfile on|off
Default:sendfile off
Context:http、server、location、if on location
Syntax:tcp_nopush on|off
Default:tcp_nopush off
Context:http、server、location
Syntax:tcp_nodelay on|off
Default:tcp_nodelay off
Context:http、server、location
Syntax:gzip on|off
Default:gzip off
Context:http、server、location、if on location
Syntax:gzip_comp_level
Default:gzip_comp_lvel 1;
Context:http、server、location
syntax:gzip_http_version 1.0|1.1
Default:gzip_http_version 1.1
Context:http、server、location
擴展Nginx壓縮模塊php
http_gzip_static_module-預讀gzip功能
http_gunzip_module-應用支持gunzip壓縮方式css
配置實例:html
server { ... sendfile on; location ~ .*\.(jpg|png|gif)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; } location ~ .*\.(txt|xml)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 1; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; } location ~ ^/download { gzip on; tcp_nopush on; gzip_static on; ... } }
優先級 | 機制 | 參數 |
---|---|---|
1 | 校驗是否過時 | Expires、Cache-Control(max-age) |
2 | 協議中的Etag頭信息校驗 | Etag |
3 | Last-Modified頭信息校驗 | Last-Modified |
配置實例:java
server { ... expires 24h; }
通俗地講,跨域訪問就是在訪問某一個網址(www.mantis.me) 的同時訪問另外一個網址(www.xxx.com) ,對於瀏覽器來講這是不容許的,由於容易出現CRSF攻擊,瀏覽器會阻止這種行爲。node
配置語法:web
Syntax:add_header name value [always]
Default:--
Context:http、server、location、if in location
瀏覽器會檢查Access-Control-Allow-Origin對應的值,提示錯誤:跨域
XMLHttpRequest cannot load http://www.mantis.me/. The 'Access-Control-Allow-Origin' header has a value 'http://www.xxx.com' that is not equal to the supplied origin. Origin 'http://www.mantis.me/' is therefore not allowed access.
配置實例:瀏覽器
server { ... location ~ .*\.(html|htm)$ { ... add_header Access-Control-Allow-Origin www.xxx.com; // 容許全部能夠設置爲*(不建議,容易被跨域攻擊) add_header Access-Control-Allow-Methods POST,GET,OPTIONS,PUT,DELETE; } }
防止資源被盜用,其餘網站盜用咱們網站的資源連接信息致使資源信息被盜用同時還有可能致使咱們服務器的壓力增大。緩存
Syntax:valid_referers none|blocked|server_names|string...;
Default:---
Context:server、location
配置實例:
server { location ~ .*\.(jpg|jpeg|png|gif)$ { ... valid_referers blocked www.mantis.me ~/baidu\./;// 這裏只容許refer頭爲www.mantis.me的地址和baidu搜索過來的,能夠便於seo優化 if ($invalid_referer) { return 403; } } }