NGINX,FastCGI,UTF-8编码:输出iso-8859-1而不是utf8

时间:2011-09-02 14:48:50

标签: utf-8 character-encoding nginx cgi fastcgi

我希望你能给我一个关于出了什么问题的想法。

Szenario: 我在fastcgi模式下使用脚本运行gitweb(CGI):

#!/bin/sh
export FCGI_SOCKET_PATH=127.0.0.1:7001
su git -c "/var/www/vh_[vhost]/htdocs/gitweb.cgi --fastcgi &"

然后我使用nginx来提供该内容:

...
fastcgi_pass 127.0.0.1:7001; 
...

一切都按预期工作,但问题出在这里:

$ wget "http://git.[host].de/?p=[repo].git;a=summary" -O /tmp/test.txt && file --mime-encoding /tmp/test.txt
> /tmp/test.txt: iso-8859-1



$ su git -c "./gitweb.cgi \"?p=[repo].git;a=summary\" > ./test" && file --mime-encoding ./test 
> ./test: utf-8

这显然意味着fast-cgi输出为utf8,而nginx提供的内容为iso-8859-1。

FireBugs响应标题:

Server  nginx
Date    Fri, 02 Sep 2011 14:14:08 GMT
Content-Type    application/xhtml+xml; charset=utf-8
Transfer-Encoding   chunked
Connection  close

看起来使用套接字传输会导致编码问题 我已经测试了很多,但无法弄清楚如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

虽然你没有使用PHP,但是我发现了我的问题的修复程序,但是将那些作为ISO-8859-1暴露的部分包裹起来:utf8_encode()http://php.net/manual/en/function.utf8-encode.php

如果您的CGI处于PERL状态,那么http://perldoc.perl.org/utf8.html可能会解决您的问题。它解决了我的......Z rich

另一种选择可能是将以下内容添加到nginx.conf中的http { }语句中:

 charset utf-8;

-sd

答案 1 :(得分:1)

我可以使用fcgiwrap

使其有效

我虽然有些环境变量在两种方法之间有所不同,所以我将以下代码添加到gitweb.cgi dispatch()子:

        open my $tmplogfile, ">", "/tmp/gitweb-env.txt";
        foreach my $varkey (sort keys %ENV) {
                print $tmplogfile "$varkey = $ENV{$varkey}\n";
        }
        close $tmplogfile;

但环境是一样的。

fcgiwrap可能会做点什么,我还没找到。

以下是我使用的命令以及我在fcgi套接字上使用tcpdump找到的差异:

# gitweb spawned by fcgiwrap outputs utf-8
/usr/bin/spawn-fcgi -d /usr/share/gitweb -a 127.0.0.1 -p 3000 -u www-data -g gitolite -P /run/gitweb/gitweb.cgi.pid -- /usr/sbin/fcgiwrap

# Require the following nginx gitweb_fastcgi_params
# fastcgi_param   QUERY_STRING            $query_string;
# fastcgi_param   REQUEST_METHOD          $request_method;
# fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
# fastcgi_param   DOCUMENT_ROOT           $document_root;

# With the following nginx configuration
# upstream gitweb {
#     server 127.0.0.1:3000;
# }
# 
# server {
#         listen   80;
# 
#         server_name git.example.net;
#
#         root /usr/share/gitweb;
#
#         access_log /var/log/nginx/gitweb-access.log;
#         error_log /var/log/nginx/gitweb-errors.log;
# 
#         location / {
#                 alias /usr/share/gitweb/gitweb.cgi;
#                 include gitweb_fastcgi_params;
#                 fastcgi_pass gitweb;
#         }
# 
#         location /static {
#                 alias /usr/share/gitweb/static;
#                 expires 31d;
#         }
# }

# STDOUT captured on lo
# Begin of the FCGI answer
#    00000000  01 06 00 01 1f f8 00 00  53 74 61 74 75 73 3a 20 ........ Status: 
#    00000010  32 30 30 20 4f 4b 0d 0a  43 6f 6e 74 65 6e 74 2d 200 OK.. Content-
#    00000020  54 79 70 65 3a 20 61 70  70 6c 69 63 61 74 69 6f Type: ap plicatio
#    00000030  6e 2f 78 68 74 6d 6c 2b  78 6d 6c 3b 20 63 68 61 n/xhtml+ xml; cha
#    00000040  72 73 65 74 3d 75 74 66  2d 38 0d 0a 0d 0a 3c 3f rset=utf -8....<?
#    00000050  78 6d 6c 20 76 65 72 73  69 6f 6e 3d 22 31 2e 30 xml vers ion="1.0
#    [...]
#
# "Guido Günther" as UTF-8
#    00000FA0  6c 65 3d 22 53 65 61 72  63 68 20 66 6f 72 20 63 le="Sear ch for c
#    00000FB0  6f 6d 6d 69 74 73 20 61  75 74 68 6f 72 65 64 20 ommits a uthored 
#    00000FC0  62 79 20 47 75 69 64 6f  20 47 c3 bc 6e 74 68 65 by Guido  G..nthe
#    00000FD0  72 22 20 63 6c 61 73 73  3d 22 6c 69 73 74 22 20 r" class ="list" 

之前,gitweb --fastcgi直接由spawn-fcgi生成:

# gitweb spawned by spawn-fcgi outputs iso-8859-1
/usr/bin/spawn-fcgi -d /usr/share/gitweb -a 127.0.0.1 -p 3000 -u www-data -g gitolite -P /run/gitweb/gitweb.cgi.pid -- /usr/share/gitweb/gitweb.cgi --fastcgi

# STDOUT captured on lo
# Begin of the FCGI answer with "00 46 02" in place of "1f f8 00" for utf-8 output
#    00000000  01 06 00 01 00 46 02 00  53 74 61 74 75 73 3a 20 .....F.. Status: 
#    00000010  32 30 30 20 4f 4b 0d 0a  43 6f 6e 74 65 6e 74 2d 200 OK.. Content-
#    00000020  54 79 70 65 3a 20 61 70  70 6c 69 63 61 74 69 6f Type: ap plicatio
#    00000030  6e 2f 78 68 74 6d 6c 2b  78 6d 6c 3b 20 63 68 61 n/xhtml+ xml; cha
#    00000040  72 73 65 74 3d 75 74 66  2d 38 0d 0a 0d 0a 00 00 rset=utf -8......
#    00000050  01 06 00 01 02 88 00 00  3c 3f 78 6d 6c 20 76 65 ........ <?xml ve
#    00000060  72 73 69 6f 6e 3d 22 31  2e 30 22 20 65 6e 63 6f rsion="1 .0" enco
#    00000070  64 69 6e 67 3d 22 75 74  66 2d 38 22 3f 3e 0a 3c ding="ut f-8"?>.<
#    [...]
#
# "Guido Günther" as ISO-8859-1
#    00001128  74 6c 65 3d 22 53 65 61  72 63 68 20 66 6f 72 20 tle="Sea rch for 
#    00001138  63 6f 6d 6d 69 74 73 20  61 75 74 68 6f 72 65 64 commits  authored
#    00001148  20 62 79 20 47 75 69 64  6f 20 47 fc 6e 74 68 65  by Guid o G.nthe