NGINX读取缓存的html文件在网页上显示特殊字符

时间:2013-12-11 07:23:00

标签: apache caching nginx httpserver

我正在使用带有APACHE 2.2的NGINX 1.4.4。 NGINX创建缓存文件,但是当请求shame页面时,NGINX会读取缓存文件并显示。问题是缓存的文件内容在网页上显示为编码字符。以下是我的nginx.conf配置。

#user       nobody;
worker_processes  5;  ## Default: 1
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
    worker_connections  1024;
}


http {
    include     mime.types;    
    include     fastcgi.conf;
    include     koi-utf;
    #include    koi-win;
    index   cache.html cache.htm cache.php;


    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;

    server_names_hash_bucket_size 128;

    #keepalive_timeout  0;
    #keepalive_timeout  65;

    gzip  on;

    server {
        listen       localhost:88;
        server_name  localhost; 
        access_log  logs/host.access.log  main;

        charset utf-8;
        #charset windows-1251;
        source_charset koi8-r;

        # serve static files
        location ~ ^/(images|javascript|js|css|flash|media|static)/  {
            root    D:/docroot/ngnix/static/;
            expires 30d;
        }

        location / {

            include     proxy.conf;
            root   D:/docroot/ngnix/html/;
            #proxy_pass http://192.168.2.201:80;

            # if the request uri was a directory, store the index page name
            #if ($request_uri ~ /$) {
            #     set $store_extra ${request_uri};
            #}

            # set the location the proxy will store the data to. Add the index page
            # name if the uri was a directory (nginx can't normally store these)
            proxy_store D:/docroot/ngnix/html/${request_uri}cache.html;

            # go through the proxy if there is no cache
            if (!-f D:/docroot/ngnix/html/${request_uri}cache.html) {
                proxy_pass http://192.168.2.201:80;
            }

            # workaround. headers module doesn't take into account proxy response
            # headers. It overwrites the proxy Cache-Control header, causing
            # private/no-cache/no-store to be wiped, so only set if not using proxy
            if (-f D:/docroot/ngnix/html/${request_uri}cache.html) {
                expires 0;
            }

        }

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

以下是koi-utf文件

# This map is not a full koi8-r <> utf8 map: it does not contain
# box-drawing and some other characters.  Besides this map contains
# several koi8-u and Byelorussian letters which are not in koi8-r.
# If you need a full and standard map, use contrib/unicode2nginx/koi-utf
# map instead.

charset_map  koi8-r  utf-8 {

    80  E282AC ; # euro

    95  E280A2 ; # bullet

    9A  C2A0 ;   # &nbsp;

    9E  C2B7 ;   # &middot;

    A3  D191 ;   # small yo
    A4  D194 ;   # small Ukrainian ye

    A6  D196 ;   # small Ukrainian i
    A7  D197 ;   # small Ukrainian yi

    AD  D291 ;   # small Ukrainian soft g
    AE  D19E ;   # small Byelorussian short u

    B0  C2B0 ;   # &deg;

    B3  D081 ;   # capital YO
    B4  D084 ;   # capital Ukrainian YE

    B6  D086 ;   # capital Ukrainian I
    B7  D087 ;   # capital Ukrainian YI

    B9  E28496 ; # numero sign

    BD  D290 ;   # capital Ukrainian soft G
    BE  D18E ;   # capital Byelorussian short U

    BF  C2A9 ;   # (C)

    C0  D18E ;   # small yu
    C1  D0B0 ;   # small a
    C2  D0B1 ;   # small b
    C3  D186 ;   # small ts
    C4  D0B4 ;   # small d
    C5  D0B5 ;   # small ye
    C6  D184 ;   # small f
    C7  D0B3 ;   # small g
    C8  D185 ;   # small kh
    C9  D0B8 ;   # small i
    CA  D0B9 ;   # small j
    CB  D0BA ;   # small k
    CC  D0BB ;   # small l
    CD  D0BC ;   # small m
    CE  D0BD ;   # small n
    CF  D0BE ;   # small o

    D0  D0BF ;   # small p
    D1  D18F ;   # small ya
    D2  D180 ;   # small r
    D3  D181 ;   # small s
    D4  D182 ;   # small t
    D5  D183 ;   # small u
    D6  D0B6 ;   # small zh
    D7  D0B2 ;   # small v
    D8  D18C ;   # small soft sign
    D9  D18B ;   # small y
    DA  D0B7 ;   # small z
    DB  D188 ;   # small sh
    DC  D18D ;   # small e
    DD  D189 ;   # small shch
    DE  D187 ;   # small ch
    DF  D18A ;   # small hard sign

    E0  D0AE ;   # capital YU
    E1  D090 ;   # capital A
    E2  D091 ;   # capital B
    E3  D0A6 ;   # capital TS
    E4  D094 ;   # capital D
    E5  D095 ;   # capital YE
    E6  D0A4 ;   # capital F
    E7  D093 ;   # capital G
    E8  D0A5 ;   # capital KH
    E9  D098 ;   # capital I
    EA  D099 ;   # capital J
    EB  D09A ;   # capital K
    EC  D09B ;   # capital L
    ED  D09C ;   # capital M
    EE  D09D ;   # capital N
    EF  D09E ;   # capital O

    F0  D09F ;   # capital P
    F1  D0AF ;   # capital YA
    F2  D0A0 ;   # capital R
    F3  D0A1 ;   # capital S
    F4  D0A2 ;   # capital T
    F5  D0A3 ;   # capital U
    F6  D096 ;   # capital ZH
    F7  D092 ;   # capital V
    F8  D0AC ;   # capital soft sign
    F9  D0AB ;   # capital Y
    FA  D097 ;   # capital Z
    FB  D0A8 ;   # capital SH
    FC  D0AD ;   # capital E
    FD  D0A9 ;   # capital SHCH
    FE  D0A7 ;   # capital CH
    FF  D0AA ;   # capital hard sign
}

第一个请求它正常工作,因为响应来自apache web服务器。当我重新请求时,它会读取如下所示的缓存页面。

?М}koI?Юg?Ъ?MВ6??HVЯ!J?(C?ЕІzmYcищsh4?dU?,?XEWEЁщfvьҐЩ|юbЖuЁ?8ЮX`В0?S?_"л
/ы?|TeЯ!?2уФt[щ&????????Y??кЭЧшЩДАЁ?ЁЪrrH?Qо%'/<9: ыB?ТUЕ?TzxЖ?ЭРЯыс'д,Д,?^Х
D?ОQЇT:<н?l7?ЗВJ?АpXV?~п)?=/ҐA\&???H+YЄ#;?ВЁL&Ё??{Ьм
??ъўЦҐ"щ€?ыeюW #9VиИяyр?Е{EгРЁ$`n#vЩ ??$/KJ{?х?F.??EыiXґP?lr?f?ын?W?т,?
F}жхFЛMTБ©;?МЩКi?Г??|??°1Yуx?ИU,?Ьй<дWnPукв? g· ?WU0^Тй ?mЙYё?Ъі?Z(ЁІІкяМас
зоґюИG?! <гК?Ї?у?ж?NЮхиЁЩ!?|?eжзоBe$~?y<Ъ+·?Н:Т2Tв8?4dА"BГQ?l# /$HnS
s]ч?uцьЮйзь?'Л сьmф?€AОBуj?X??Зўш?AхFю??Ь3 R|ЖЭ вЇ(??оІ0+РAШBЭ?-6Ю?sК??w$
?Є jЯ?Bpэкd?KДDп1P_wKБсw?·М?гgg'?ц_?8З??=ь?x|X8xv|ЖЭы?,а?цЦЁFV?убЖXD Йt?Ґ8Г?
,t?L?ТЕFЖt фGYцЯЧсцFЖоыhХv??E?R Н!?88хN?fрX©ЕG:нёЦ??©э ?·=yРЛ? Д.У:Ю
??сJрXы№?@шпО5ь2?ИHМT??Оw\єhю·O?К?Ё *іC?·сО?ЪZhnГ?m?ІЫC 8%s!?бt&ґОUХ4
М? й?ф?ЁG?-иw?ЇNНsҐЖ]'йГ>кґ?!???A +?U9P??_??ж?к?Nт?є?w?ZNf?€ЬзЫF?m Z?
Фю?v?ю6юi??]?& uъІ?M?Q?*9ЖоIы0к$Р =В?э!МюО??к?Йnп Н€е(TФ7?пFё?х?Л5?)?E
°pп ёFЙ??Aрeі"?+€DЩ6?^яЦЗг?лИ?б{?0ї?#л??+[@аQ?Q> ?д€ю?Xo6ю?Ф}7hтpў€
ыУ?)5ґ?є?фDзъ/d|h?І?щI?ю??'У?яr80OzG?;д5Х?>pЛ?'T)ХС
Є.Їц?л??}?$ц_2??CзбнёдO%0)???М[ч?Dщ#ЪT•K \/MPOтмq,kEю?ЧQb^ ?х1
жи}?є°ю? ?е?ЛFІ?Ґ?ё? КД?НлDuсH??©???N?2dх!ИяхB??"?#2є~?H1?З,GмMЛ*Уd
З;bBлщ=%5у?эiN??ЩX????]??с}?Tз? Kc Y?4bН?LUl??[ ·?6?Fс?+ПFC???@О=??G€<ж?К©AУb
`}?Ыy єOСjЭ[?OМQ^?і|??ёV&Ґ>мГН`?`Фж?]Ъ?ya ??ДґЙЯqn.$нмВГФбБэ\і87W\?3И?O?У?дY
qжg}?Б???8gр7M?X цИ^?E?sКU ? 2Р;W6VW?-?ZҐйИи"=]B??сHбZЭ:/ОЦ 
.КкQ?ъгєTтФСдLэl?4?{?y%????{|????6???КЩБ?Е6Y#?ў],???М=вїІзXd

导致此问题的原因是什么?如果配置错误,请纠正我。

1 个答案:

答案 0 :(得分:0)

修改了nginx.conf并且运行正常。

#user       nobody;
worker_processes  5;  ## Default: 1
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
    worker_connections  1024;
}


http {
    include     mime.types;    
    include     fastcgi.conf;
    #include     koi-utf;
    #include    koi-win;
    index   cache.html cache.htm cache.php;


    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;

    server_names_hash_bucket_size 128;

    #keepalive_timeout  0;
    #keepalive_timeout  65;

    gzip  on;

    ######## additional Gun-zip configurations start #########
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    #gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;

    # make sure gzip does not lose large gzipped js or css files
    # see http://blog.leetsoft.com/2007/07/25/nginx-gzip-ssl.html
    gzip_buffers 16 8k;

    # Disable gzip for certain browsers.
    gzip_disable “MSIE [1-6].(?!.*SV1)”;
    ######## additional Gun-zip configurations end #########

    server {
        listen       88;
        server_name  localhost; 
        access_log  logs/host.access.log  main;

        access_log  logs/host.access.log  main;

        # proxy module defaults
        proxy_store_access   user:rw  group:rw  all:r;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  Host       $host;

        #charset utf-8;
        #charset windows-1251;
        #source_charset koi8-r;

        # serve static files
        #location ~ ^/(images|javascript|js|css|flash|media|static)/  {
        #   root    D:/docroot/nginx/static/;
        #   expires 30d;
        #}

        # Main location
        location / {

            include     proxy.conf;
            root D:/docroot/ngnix/;
            index index.html index.htm;

            set $prefix  prefix_;

            # Use this if you don't cache a context (dont_cache_path) , send all requests through the proxy
            #location ~ ^/dont_cache_path {
            #   proxy_pass http://localhost:8080;
            #}

            # if the request uri was a directory, store the index page name
            if ($request_uri ~ /$) {
                set $store_extra index;
            }

            # set the location the proxy will store the data to. Add the index page
            # name if the uri was a directory (nginx can't normally store these)
            proxy_store D:/docroot/ngnix/${request_uri}${store_extra}.html;

            # go through the proxy if there is no cache
            if (!-f D:/docroot/ngnix/${request_uri}${store_extra}.html) {
                proxy_pass http://192.168.2.201:80;
            }

            # workaround. headers module doesn't take into account proxy response
            # headers. It overwrites the proxy Cache-Control header, causing
            # private/no-cache/no-store to be wiped, so only set if not using proxy
            #if (-f  D:/docroot/ngnix/${request_uri}${store_extra}.html) {
            #   expires 0;
            #}

            # handle static files directly. Set their expiry time to max, so they'll
            # always use the browser cache after first request
            location ~* (css|js|png|jpe?g|gif|ico|swf)$ {
                root D:/docroot/;
                expires max;
            }
        }

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

    }

}