Nginx 配置詳解 緩存和壓縮功能

解析nginx.conf文件

 -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
cat /etc/nginx/nginx.conf

[root@cloud-master conf]# cat nginx.conf

#user  nobody;    
worker_processes  1;                 # 啓動進程數,設置工作進程數量

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;         #  單個進程併發量 (總併發 = worker_processes * worker_connections )
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #  虛擬機配置
    server {
        listen       80;                # 默認端口號
        server_name  localhost;         # 域名解析

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                    #  配置默認訪問頁
            root   html;                # 網站根目錄
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {           #  配置默認錯誤頁
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        # 如果apache 和 nginx 的網站目錄是一個, apache 需要使用分佈式配置文件,開啓此項
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

增加 server 信息(複製一段server)

    server {
       listen       80;
       server_name cloud-master.com;                     # 域名 cloud-master.com
       root /usr/local/nginx-1.14.0/html/cloud-master;   # 提取網站根目錄爲該server全局變量
       location / {
           index index.html index.htm;
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
            root   html;
       }
       location ~ \.(jpeg|jpg|png)$ {                    # 緩存圖片功能
           expires 1d; 
       }


    }

在 /usr/local/nginx-1.14.0/html/ 路徑下創建 cloud-master

[root@cloud-master cloud-master]# pwd
/usr/local/nginx-1.14.0/html/cloud-master
[root@cloud-master cloud-master]# ls
Alita.jpg  index.html
[root@cloud-master cloud-master]# cat index.html
<h1> hello nginx name : cloud master </h1>

Nginx 提供了語法檢查功能,習慣養好,先檢查再重載

# nginx -t
# nginx -s reload

[root@cloud-master cloud-master]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@cloud-master cloud-master]# nginx -s reload
[root@cloud-master cloud-master]#

配置本機host文件C:\Windows\System32\drivers\etc\hosts

增加一行 

192.168.74.9 cloud-master.com (ip 爲虛擬機 ip, cloud-master.com  爲conf中寫入的domain)

打開瀏覽器訪問 cloud-master.com

Nginx 緩存功能

Nginx 通過配置,可以告知瀏覽器,返回數據的有效時間,瀏覽器根據返回時間,確認是否到服務器請求,如果沒有超過有效期,就使用瀏覽器緩存,緩存功能是爲了用戶更快獲取和使用數據,減少服務器請求,降低帶寬壓力

nginx.conf 中加入 server 的描述

1d 表示緩存一天

location ~ \.(jpeg|jpg|png)$ {
           expires 1d;
       }

在/usr/local/nginx-1.14.0/html/cloud-master 下加入圖片 Alita.jpg

[root@cloud-master cloud-master]# pwd
/usr/local/nginx-1.14.0/html/cloud-master
[root@cloud-master cloud-master]# ls
Alita.jpg  index.html

通過瀏覽器訪問cloud-master.com/Alita.jpg 可以得到數據

檢查語法,重載nginx

[root@cloud-master cloud-master]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@cloud-master cloud-master]# nginx -s reload
[root@cloud-master cloud-master]#

 再次訪問瀏覽器 cloud-master.com/Alita.jpg

F12 查看 請求返回header

304 表示 從緩存獲取數據

200 表示 從服務器獲取數據

 

記得打開瀏覽器緩存功能

gzip 功能 

壓縮資源,通過網絡發送的大小就更加節省帶寬,啓用壓縮機制,爲了能更快的訪問資源

web服務器進行壓縮,瀏覽器需要進行解壓縮操作

gzip  on;                    # 配置 gzip 可用
gzip_http_version 1.0;       # http 協議版本 1.0
gzip_disable 'MSIE [1-6].';  # 如果是 microsoft ie 1-6 版本就關閉壓縮功能
gzip_types image/jpeg        # 需要壓縮的文件格式

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章