nginx设置目录浏览及中文乱码问题解决方案

 

nginx设置目录浏览及中文乱码问题解决方案

 

 

在Nginx下默认是不允许列出整个目录的。如需此功能,
先打开nginx.conf文件,在location server 或 http段中加入

autoindex on;
另外两个参数最好也加上去:

autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
注意:改为on后,显示的文件时间为文件的服务器时间

 

nginx设置目录浏览及中文乱码问题解决方案

但是如果有中文目录的话会出现乱码问题,所以还需要在下面添加这一句:

charset utf-8,gbk;

 

另外两个参数最好也加上去:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
 改为on后,显示的文件时间为文件的服务器时间

 

 

完整的配置文件nginx.conf例子如下:

 

[root@localhost sbin]# cat ../conf/nginx.conf

user root;
worker_processes  2;

#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;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime off;
    charset utf-8,gbk;

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

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   /var/www;
            index  index.cgigo;
        }

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

}

 

 

 

 

 

 

 

### 解决 Nginx 访问目录中文文件名或路径显示为乱码的方法 #### 配置 Nginx 支持 UTF-8 编码 为了使 Nginx 正确处理并显示中文字符,需确保其配置文件中指定了正确的字符集。编辑 `nginx.conf` 文件,在 http 或 server 块内加入 charset 指令: ```nginx http { ... charset utf-8; } ``` 或者更具体地应用到某个虚拟主机配置里: ```nginx server { listen 80; server_name localhost; location / { root html; index index.html index.htm; charset utf-8; # 设置默认字符集为UTF-8[^1] } } ``` #### 调整 autoindex 模块行为 当启用了自动索引功能 (`autoindex on`) 后,如果遇到中文名称的文件夹或文件出现乱码情况,则可以尝试调整 `autoindex_format` 的值来改善这一状况。 ```nginx location /downloads/ { alias /var/www/downloads/; autoindex on; autoindex_exact_size off; autoindex_localtime on; charset utf-8,gbk; # 尝试多种编码方式以适应不同客户端需求[^2] # 使用 HTML 格式的自动生成目录列表,并指定编码 autoindex_format html; default_type text/html;charset=utf-8; } ``` 上述配置不仅设置了 `charset` 参数还通过 `default_type` 来进一步强化了返回给浏览器的内容类型及其编码属性。 #### 修改源代码中的硬编码部分(可选) 对于某些特定版本可能存在内部实现上的局限性,比如在 C++ 实现中关于字符串长度计算的部分可能会因为多字节字符而导致问题。虽然这不是常规解决方案的一部分,但在极端情况下可能需要考虑此选项。不过这通常涉及到编译定制版 Nginx 并不适合大多数应用场景[^4]。 #### 测试更改后的效果 完成以上修改之后保存配置文件并重启 Nginx 服务让改动生效。可以通过命令行工具如 curl 或者直接利用 Web 浏览器测试是否解决了中文乱码问题。 ```bash sudo systemctl restart nginx curl -I http://yourdomain.com/path/to/chinese-file.txt ``` 确认 HTTP 头部包含了 Content-Type 字段且其中含有 `charset=UTF-8` 即表示成功配置了正确编码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值