Ngnix配置為靜態(tài)資源服務(wù)

完整配置

在默認(rèn)配置的基礎(chǔ)上稍加修改如下


#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;
}

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壓縮功能設(shè)置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /static {
            root   C:/Webplat/Runtime;
        }

        #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
        #
        #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;
    #    }
    #}

}

配置說明

上述配置,只在默認(rèn)配置上修改了2處

gzip

開啟gzip壓縮輸出,減少網(wǎng)絡(luò)傳輸

http {
    ......
    # gzip壓縮功能設(shè)置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;
    ......

靜態(tài)資源

http {
    ......
    server {
        ......
        location /static {
            root   C:/Webplat/Runtime;
        }
        ......

通過以上配置,便可以通過 http://localhost/static 訪問到靜態(tài)資源,例如http://localhost/static/img/avatar.png

其它可配置項(xiàng)

除以上配置外,可視服務(wù)器硬件和網(wǎng)絡(luò)環(huán)境實(shí)際情況,按需調(diào)整其它一些配置。

工作進(jìn)程數(shù)
這個數(shù)值簡單一點(diǎn)可以設(shè)置為cpu的核數(shù),開啟了ssl和gzip更應(yīng)該設(shè)置成與邏輯CPU數(shù)量一樣甚至為2倍,可以減少I/O操作。

worker_processes  1;

端口

http {
    ......
    server {
        listen       80;
        ......

參考文獻(xiàn)

nginx配置入門
nginx服務(wù)器安裝及配置文件詳解

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,569評論 1 41
  • nginx在工作中已經(jīng)有好幾個環(huán)境在使用了,每次都是重新去網(wǎng)上扒博客,各種編譯配置,今天自己也整理一份安裝文檔和n...
    AndyChin閱讀 2,326評論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • 上一篇《WEB請求處理一:瀏覽器請求發(fā)起處理》,我們講述了瀏覽器端請求發(fā)起過程,通過DNS域名解析服務(wù)器IP,并建...
    七寸知架構(gòu)閱讀 81,212評論 21 356
  • 我站在夜的山頂,看萬家燈火。少年的我,渴念名望。我刻意苦讀,臥冰踏雪。 我有無盡的精力,在我的少年。總喜歡找點(diǎn)刺激...
    銘玥詠全閱讀 205評論 0 0