<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>船长日志 &#187; 汉字编码</title>
	<atom:link href="http://www.cslog.cn/tag/%e6%b1%89%e5%ad%97%e7%bc%96%e7%a0%81/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cslog.cn</link>
	<description>最让我激动的是不知道下一个星球上能发现什么...</description>
	<lastBuildDate>Wed, 30 Jul 2025 16:06:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>mysql不能储存生僻汉字？试试utf8mb4字符集</title>
		<link>http://www.cslog.cn/Content/mysql-utf8mb4/</link>
		<comments>http://www.cslog.cn/Content/mysql-utf8mb4/#comments</comments>
		<pubDate>Fri, 07 Jun 2013 15:13:29 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[信息处理]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[汉字]]></category>
		<category><![CDATA[汉字编码]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=1165</guid>
		<description><![CDATA[今天发现存进mysql的数据部分丢失了。发现丢失的都是一些生僻的汉字。我这个my &#8230; <a href="http://www.cslog.cn/Content/mysql-utf8mb4/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>今天发现存进mysql的数据部分丢失了。发现丢失的都是一些生僻的汉字。我这个mysql使用的是utf8字符集，之前一直以为不会发生这种问题，今天才看到了。而且这个问题<a href="http://bbs.zdic.net/thread-115854-1-1.html">很早就存在了</a>。这是<a href="http://bugs.mysql.com/bug.php?id=14052">mysql的一个bug</a>，它的UTF-8字符集只能存储3字节字符，而有部分生僻的汉字是4个字节的，存入时就会出错：</p>
<blockquote><p>ActiveRecord::StatementInvalid: Mysql::Error: Incorrect string value: &#8216;\xF0\x90\x8D\x83\xF0\x90&#8230;&#8217; for column &#8216;content&#8217; at row 1</p></blockquote>
<p>上面的官方bug页面说要到mysql6.0才会解决。其实这个问题在mysql 5.5就解决了。5.5或以上的mysql都可以通过使用utf-16或utf8mb4等字符集解决这个问题。</p>
<p>不过使用ruby on rails的话可能<a href="http://donpark.org/blog/2013/02/16/rails-3-2-12-not-ready-for-mysql-5-5-utf8mb4">需要一些额外的设置</a>。
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/mysql-utf8mb4/">http://www.cslog.cn/Content/mysql-utf8mb4/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/mysql-utf8mb4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>再谈Nginx Rewrite， 中文URL和其它</title>
		<link>http://www.cslog.cn/Content/nginx-rewrite-chinese/</link>
		<comments>http://www.cslog.cn/Content/nginx-rewrite-chinese/#comments</comments>
		<pubDate>Fri, 29 Jun 2012 15:36:17 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[信息处理]]></category>
		<category><![CDATA[站长文档]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[nginx中文url重写]]></category>
		<category><![CDATA[nginx的utf8支持]]></category>
		<category><![CDATA[nginx重写]]></category>
		<category><![CDATA[pcre]]></category>
		<category><![CDATA[rewrite]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[url重写]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[中文url]]></category>
		<category><![CDATA[服务器]]></category>
		<category><![CDATA[汉字编码]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=1058</guid>
		<description><![CDATA[上次谈到过Nginx和中文URL的问题，这几天又加深了认识。 多分享几个关于Ng &#8230; <a href="http://www.cslog.cn/Content/nginx-rewrite-chinese/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>上次谈到过<a href="http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/">Nginx和中文URL的问题</a>，这几天又加深了认识。 多分享几个关于Nginx Rewrite的经验。</p>
<p>Nginx匹配指定中文URL的方法：<br />
<code><br />
rewrite "(*UTF8)^\x{66f4}\x{6539}$" /index.html last;<br />
</code><br />
这个会将 http://www.cslog.cn/%E6%9B%B4%E6%94%B9 重定向到http://www.cslog.cn/index.html。<br />
&#8216;/%E6%9B%B4%E6%94%B9&#8242;是中文URL ‘/更改’， \x{66f4}\x{6539}是这两个汉字的Unicode编码。</p>
<p>注意配合PCRE 7.9以上版本使用，详细看<a href="http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/">Nginx和中文URL的问题</a>。<br />
<span id="more-1058"></span><br />
Nginx匹配URL中中文参数的方法：<br />
如果要将 http://www.cslog.cn/index.php?title=%E6%9B%B4%E6%94%B9 重定向到http://www.cslog.cn, 则要使用<br />
<code><br />
location = /index\.php$ {<br />
if ($args_title =%E6%9B%B4%E6%94%B9 ) {<br />
rewrite ^ /;<br />
}<br />
}<br />
</code><br />
在这里， &#8216;?title=%E6%9B%B4%E6%94%B9&#8242; 是不能用上例那里的rewrite捕捉到的， 而要使用$arg_参数名这样的方式处理。 而且在这里， &#8216;%E6%9B%B4%E6%94%B9&#8242;是不用特别转换的，直接放上去就可以了。</p>
<p>还要注意到 location后面的 ~, *~, ^~, =或空是有不同的效果的。详细参看<a href="http://nginx.org/en/docs/http/ngx_http_core_module.html#location">nginx的官方手册</a>。</p>
<p>让Nginx在同时识别到两个参数的情况下重定向的方法<br />
假设要在例如 http://www.cslog.cn/index.php?title=%E6%9B%B4%E6%94%B9&amp;lang=Zh-tw 这样的URL，只有在lang=Zh-tw参数存在，并且title参数也存在（不管它的值）的情况下才重定向， 要求会苛刻些，因为nginx自身没有并列的逻辑处理能力，但可以使用如下的方法曲线解决：<br />
<code><br />
location = /index\.php {<br />
set $redirect "";</code></p>
<p><code># 检查是否存在"title"参数:<br />
if ( $arg_title ) {<br />
set $redirect "Y";<br />
}</p>
<p># 检查lang参数是否为 "Zh-tw":<br />
if ( $arg_lang =Zh-tw ) {<br />
set $redirect "${redirect}ES";<br />
}</p>
<p># 看看条件是否同时成立 "YES":<br />
if ( $redirect = YES ) {<br />
rewrite ^ / last;<br />
}</p>
<p></code></p>
<p><code> ....<br />
}<br />
</code>
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/nginx-rewrite-chinese/">http://www.cslog.cn/Content/nginx-rewrite-chinese/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/nginx-rewrite-chinese/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nginx、PCRE和中文URL（UTF8编码）rewrite路径重写匹配问题</title>
		<link>http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/</link>
		<comments>http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 15:08:45 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[信息处理]]></category>
		<category><![CDATA[站长文档]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[nginx中文url重写]]></category>
		<category><![CDATA[nginx的utf8支持]]></category>
		<category><![CDATA[pcre]]></category>
		<category><![CDATA[PCRE的UTF8编码支持]]></category>
		<category><![CDATA[rewrite]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[url重写]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[中文url]]></category>
		<category><![CDATA[服务器]]></category>
		<category><![CDATA[汉字编码]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=793</guid>
		<description><![CDATA[最近遇到了使用Nginx 重写中文UTF8编码路径的问题。 才发现默认情况下Ng &#8230; <a href="http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>最近遇到了使用<a href="http://www.cslog.cn/tag/nginx/">Nginx</a> 重写中文UTF8编码路径的问题。 才发现默认情况下Nginx的rewrite是不支持UTF8匹配的。 比如：</p>
<p><code>rewrite ^/(..)$ /2个字符文章.html break; #用2个点</code><br />
可以匹配到 /ab 或 /51, 但 /汉字 是匹配不到的。 我测试了一下， 要匹配两个字的 /汉字 路径， 得用</p>
<p><code>rewrite ^/(……)$ /2个汉字文章.html break; #要用六个点</code></p>
<p>如果要让 ^/(..)$ 匹配到2个汉字字符，得开启Nginx对UTF8字符的正则支持， 准确点说是开启Nginx使用的PCRE库的对UTF8字符的支持。因为Nginx的rewrite模块是调用PCRE来处理正则的。 如果PCRE没有问题，Nginx支持UTF8编码的重写样式是：</p>
<p><code>rewrite "(*UTF8)^/(..)$" /2个字符文章.html break;<br />
#注意(*UTF8)前缀和引号的加入。</code></p>
<p><span id="more-793"></span>因为PCRE只有7.9以上的版本才支持这个(*UTF8)开头，所以系统上的PCRE一定要使用7.9以上的版本。 另外，编译PCRE时一定要开启UTF8的支持。 即使用(&#8211;enable-utf8)参数。 检查系统的PCRE版本和是否支持UTF8编码可以使用<br />
<code>pcretest -C</code><br />
命令。执行后会显示如下的报告：</p>
<blockquote><p>PCRE version 8.13 2011-08-16<br />
Compiled with<br />
UTF-8 support<br />
Unicode properties support<br />
Newline sequence is LF<br />
\R matches all Unicode newlines<br />
Internal link size = 2<br />
POSIX malloc threshold = 10<br />
Default match limit = 10000000<br />
Default recursion depth limit = 10000000<br />
Match recursion uses stack</p></blockquote>
<p>显示“UTF-8 support”就是支持UTF8编码了。</p>
<p>如果Nginx调用的PCRE为7.9以下的版本，使用 rewrite &#8220;(*UTF8)^/(..)$&#8221; /2个字符文章.html break; 这种形式的重写，在执行nginx -t检查时会出现如下的错误提示：</p>
<blockquote><p>[emerg]: pcre_compile() failed: (*VERB) not recognized in &#8220;(*UTF8)^、……</p></blockquote>
<p>如果Nginx调用的是7.9以上版本的PCRE，但是PCRE安装时没打开UTF8的支持开关，则会出现如下的错误提示：</p>
<blockquote><p>nginx: [emerg] pcre_compile() failed: this version of PCRE is not compiled with PCRE_UTF8 support in &#8220;(*UTF8)^/&#8230;</p></blockquote>
<p>解决办法是安装最新版本的的PCRE，并打开UTF8支持，方法如下（以当前8.13版的PCRE为例）：<br />
<code>wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz<br />
tar zxvf pcre-8.13.tar.gz<br />
cd pcre-8.13<br />
./configure --enable-utf8 --enable-unicode-properties<br />
make<br />
make install</code></p>
<p>然后重新编译安装Nginx。 Nginx默认会打开rewrite模块，并会自动查找系统上已经安装了的PCRE。 如果Nginx查找不到已经安装在系统上的PCRE， 或者系统上有多个PCRE， nginx调用了不支持UTF8的、或低版本的PCRE时（我遇到了后一种情况，并花费了很长的时间解决这个问题，这也是我为什么写这篇总结文章在这里给大家分享的原因。。。），可以在编译安装Nginx时指定PCRE源文件。例如：</p>
<p><code>wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz<br />
tar zxvf pcre-8.13.tar.gz<br />
wget http://nginx.org/download/nginx-1.0.8.tar.gz<br />
tar zxvf nginx-1.0.8.tar.gz<br />
cd nginx-1.0.8<br />
./configure \<br />
--with-pcre=../pcre-8.13</code></p>
<p>注意，如果接着直接make &amp;&amp; make install的话， PCRE因为没有启用UTF8， nginx将不能支持UTF8重写， 所以在这一种，我动了一点小手脚：打开./objs/Makefile 文件，找到以下段落：</p>
<p><code> ../pcre-8.13/Makefile:	objs/Makefile<br />
cd ../pcre-8.13 \<br />
&amp;&amp; if [ -f Makefile ]; then $(MAKE) distclean; fi \<br />
&amp;&amp; CC="$(CC)" CFLAGS="-O2 -fomit-frame-pointer -pipe " \<br />
./configure --disable-shared </code><br />
在&#8211;disable-shared后加上 &#8211;enable-utf8和 &#8211;enable-unicode-properties参数， 即变成：<br />
<code> ../pcre-8.13/Makefile:	objs/Makefile<br />
cd ../pcre-8.13 \<br />
&amp;&amp; if [ -f Makefile ]; then $(MAKE) distclean; fi \<br />
&amp;&amp; CC="$(CC)" CFLAGS="-O2 -fomit-frame-pointer -pipe " \<br />
./configure --disable-shared --enable-utf8 --enable-unicode-properties</code><br />
然后再make, make install。 最后关闭nginx并重启nginx（这里不能用nginx -s reload)。这样nginx就支持中文UTF8编码的url重写了。
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/">http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/nginx-pcre-utf8-rewrite/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>中文文件名编码和html文件访问</title>
		<link>http://www.cslog.cn/Content/url-encode-and-file-name/</link>
		<comments>http://www.cslog.cn/Content/url-encode-and-file-name/#comments</comments>
		<pubDate>Sun, 19 Sep 2010 00:27:46 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[站长文档]]></category>
		<category><![CDATA[服务器]]></category>
		<category><![CDATA[汉字编码]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[之前用windows服务器， 上面的有中文名的html文件。 为了保证访问， 链 &#8230; <a href="http://www.cslog.cn/Content/url-encode-and-file-name/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>之前用windows服务器， 上面的有中文名的html文件。 为了保证访问， 链接到这些文件的URL做了中文编码处理。 那时使用的是ASP写的， 编码用的是GB2312. 最近想把这个网站搬到linux服务器上. 将文件上传到linux服务器后发现原来的链接访问不到中文html文件. 到后来才想到自己linux系统上默认使用的是utf-8编码文件系统. 后来将FTP软件设定为GB2312编码, 重新上传这些文件后才问题解决了.</p>
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/url-encode-and-file-name/">http://www.cslog.cn/Content/url-encode-and-file-name/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/url-encode-and-file-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
