![我的 Nginx 网络服务器无法正确提供文件名中带有重音字符的内容](https://linux22.com/image/784056/%E6%88%91%E7%9A%84%20Nginx%20%E7%BD%91%E7%BB%9C%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%97%A0%E6%B3%95%E6%AD%A3%E7%A1%AE%E6%8F%90%E4%BE%9B%E6%96%87%E4%BB%B6%E5%90%8D%E4%B8%AD%E5%B8%A6%E6%9C%89%E9%87%8D%E9%9F%B3%E5%AD%97%E7%AC%A6%E7%9A%84%E5%86%85%E5%AE%B9.png)
我刚刚使用 Linux 机器和 NginX 创建了一个新的 Web 服务器。它似乎可以正确运行大多数内容。
我刚刚转换了一个以前在 Windows Server 机器上的网站,大部分内容和功能(HTML 和 JavaScript)都转换正常。但是,我发现 NginX 似乎对包含重音字符(例如 é、è、ô 等)的图像名称(也许其他文件也一样??)存在问题。
如果文件数量很少,我可以手动重命名这些文件,但文件数量有数百个(甚至数千个),手动重命名是行不通的。有人能提供一种轻松重命名带有这些重音字符的文件的方法吗?
谢谢..RDK
编辑:旧网站的原始转换版本中,许多页面使用“windows-1252”,少数页面使用“UTF-8”作为“字符集”。后者在显示页面内容中的特殊字符时会出现问题,通常显示为“黑色菱形?”符号。另一个似乎存在 JavaScript 问题,并且还存在一些显示问题。在网上搜索该问题后,我将所有“charset=”值更改为“iso-8859-1”,这是许多浏览器的默认设置,从而解决了所有这些问题。但现在我遇到了文件名中的特殊字符问题……
答案1
删除变音符号的一种方法是用Perl 的rename
:
如果你需要重命名变音符号相当于 ascii:
rename -u utf8 '
BEGIN{use Text::Undiacritic qw(undiacritic)}
s/.*/undiacritic($&)/e
' éééé.txt
rename(éééé.txt, eeee.txt)
另一种方法是使用排毒实用程序。可作为软件包随 Debian/Ubuntu 和其他发行版使用。
最后一种方法是使用这个脚本,基于convmv(1)从法国项目翻译成英文: 论坛主页:
它的目的是将错误的字符集更改为 utf8。(这不是我的脚本,但是Lapogne71) 可以成为问题解决者。
#!/bin/bash
VERSION="v0.04"
#---------------------------------------------------------------------------------------
# This script allows to loop the "convmv" utility that allows converting file names coded in
# something other than UTF-8 to UTF-8
# Restart the script with the ALLCODES argument if no result has been found
#---------------------------------------------------------------------------------------
# here are the colors of the text displayed in the shell
RED="\\033[1;31m"
NORMAL="\\033[0;39m"
BLUE="\\033[1;36m"
GREEN="\\033[1;32m"
echo
echo -e "$GREEN $0 $NORMAL $VERSION"
echo
echo "----------------------------------------------------------
This script allows to loop the 'convmv' utility that allows converting file names coded in
something other than UTF-8 to UTF-8. Restart the script with the ALLCODES argument if no result
has been found.
----------------------------------------------------------"
# The main loop launches convmv tests to "visually" detect the original encoding
# We only loop over the iso-8859* and cp* code families as they are the most likely ones (EBCDIC codes have also been removed from the list)
CODES_LIST="
iso-8859-1
iso-8859-2
iso-8859-3
iso-8859-4
iso-8859-5
iso-8859-6
iso-8859-7
iso-8859-8
iso-8859-9
iso-8859-10
iso-8859-11
iso-8859-13
iso-8859-14
iso-8859-15
iso-8859-16
cp437
cp737
cp775
cp850
cp852
cp855
cp856
cp857
cp860
cp861
cp862
cp863
cp864
cp865
cp866
cp869
cp874
cp932
cp936
cp949
cp950
cp1250
cp1251
cp1252
cp1253
cp1254
cp1255
cp1256
cp1257
cp1258
"
# We check if the convmv utility is installed
path=`which convmv 2> /dev/null`
if [ -z "$path" ]; then
echo -e "$RED ERROR: convmv is not installed, please install it by typing:"
echo
echo -e "$BLUE sudo apt-get install convmv "
echo
echo -e "$RED ==> program exit"
echo
echo -e "$NORMAL"
exit 1
fi
# To loop over all the codepages supported by convmv, the ALLCODES argument must be provided
if [ "$1" = "ALLCODES" ]; then
CODES_LIST=`convmv --list`
echo
echo -e "$RED Check which original encoding seems correct (press 'y' and validate if waiting for display)$NORMAL"
echo
fi
# Main loop of the program
for CODAGE in $CODES_LIST; do
echo -e "$BLUE--- Encoding hypothesis: $RED $CODAGE $BLUE---$NORMAL"
echo
# echo -e "$RED Press 'y' and validate if no list is displayed $NORMAL"
convmv -f $CODAGE -t utf-8 -r * 2>&1 | grep -v Perl | grep -v Starting | grep -v notest | grep -v Skipping > /tmp/affichage_convmv.txt
NOMBRE_FICHIERS=`cat /tmp/affichage_convmv.txt | wc -l`
if [ $NOMBRE_FICHIERS -eq 0 ]; then
echo
echo -e "$RED No filename to convert " $NORMAL
echo
echo -e "$BLUE Exiting program ... $NORMAL"
echo
rm /tmp/affichage_convmv.txt 2>/dev/null
exit 0
fi
# sed 's .. ' source.txt ==> this removes the first 2 characters from a string
echo -e $GREEN "Original filenames coded in $CODAGE: " $NORMAL
# ALTERNATIVE cat /tmp/affichage_convmv.txt | cut -f 2 -d '"' | sed 's .. '
cat /tmp/affichage_convmv.txt | cut -f 2 -d '"'
echo
echo -e $GREEN "Filenames converted to UTF-8: " $NORMAL
# ALTERNATIVE cat /tmp/affichage_convmv.txt | cut -f 4 -d '"' | sed 's .. '
cat /tmp/affichage_convmv.txt | cut -f 4 -d '"'
echo
echo -n -e $GREEN "Found encoding? $RED [N]$NORMAL""on /$RED o$NORMAL""ui /$RED q$NORMAL""uit: "
read confirm
echo
# request for file conversion using convmv
if [ "$confirm" = O ] || [ "$confirm" = o ];then
echo -e "$BLUE Convert filenames now from encoding $CODAGE? $NORMAL"
echo -e "$BLUE ==> convmv -f $CODAGE -t utf-8 * --notest $NORMAL"
echo -n -e $GREEN "Confirm conversion $RED [N]$NORMAL""on /$RED o$NORMAL""ui /$RED r$NORMAL""ecursive: "
read confirm
echo
case $confirm in
O|o) convmv -f $CODAGE -t utf-8 * --notest 2>/dev/null
echo
echo -e "$BLUE File name conversion done... $NORMAL" ;;
R|r) convmv -f $CODAGE -t utf-8 * -r --notest 2>/dev/null
echo
echo -e "$BLUE Recursive file name conversion done... $NORMAL" ;;
*) echo -e "$BLUE Exiting program... $NORMAL" ;;
esac
echo
rm /tmp/affichage_convmv.txt 2>/dev/null
exit 0
# request for program exit
elif [ "$confirm" = Q ] || [ "$confirm" = q ];then
echo -e "$BLUE Exiting program... $NORMAL"
echo
rm /tmp/affichage_convmv.txt 2>/dev/null
exit 0
fi
clear
done
rm /tmp/affichage_convmv.txt 2>/dev/null
答案2
您需要使用 Notepad++、Sublime、VSCode 或其他支持切换字符集的文本编辑器打开每个文件。将字符集切换为 UTF-8,然后保存文件。如果您直接在 Linux 中编辑文件,您可以考虑使用图标将每个文件转换为 UTF-8 编码。
然后,在将所有基于文本的文件转换为 UTF-8 字符集后,测试 nginx,字符应该会显示出来。如果没有,您也可以尝试(在nginx.conf
或任何.conf
包含服务器配置的文件中)添加此行:
charset UTF-8;
文件和网络服务器必须使用相同的字符集,因此将所有内容转换为 UTF-8 是避免将来出现这些问题的最简单方法。