在Ubuntu24.04上用nginx启用文件索引服务:autoindex on; 笔记241102

在Ubuntu24.04上用nginx启用文件索引服务:autoindex on;

sudo apt install nginx 后, 出现 /etc/nginx 目录

bash 复制代码
root@1235vw-ubt2441d:~# cd /etc/nginx
root@1235vw-ubt2441d:/etc/nginx# ls
conf.d          koi-utf     modules-available  proxy_params     sites-enabled  win-utf
fastcgi.conf    koi-win     modules-enabled    scgi_params      snippets
fastcgi_params  mime.types  nginx.conf         sites-available  uwsgi_params

从总配置文件 nginx.conf 可看到这样一行include /etc/nginx/modules-enabled/*.conf;在最上级根模块中

这样两行在http模块中

bash 复制代码
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

说明可以在 conf.d 文件夹中建立以.conf结尾的server块配置文件

或者在sites-enabled文件夹中建立任意名称的server块配置文件

conf.d 初始为空, sites-enabled下有个default默认配置文件, 和sites-available下的default一模一样, sha256sum相同

本次apt install nginx安装到的是nginx1.24的default的初始内容如下:

复制代码
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

default定义了端口为80的server块

并在80端口server块定义了 root /var/www/html; , 在location块中可不再指定root

可以在/var/www/html目录下新建文件夹 f 或其它名字, 成为 /var/www/html/f

然后在default配置文件中的server块中加入这么一段

复制代码
 		location /f {


                ###  开启目录索引 (主要)
                autoindex on;                                                                ###  开启目录索引 (主要)



                ### 不显示具体文件大小(可选), 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
                autoindex_exact_size off;                                            ### 不显示具体文件大小(可选), 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

                ### 使用本地时间显示文件时间戳(可选)默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
                autoindex_localtime on;                                             ### 使用本地时间显示文件时间戳(可选)默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间

                ### 启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了(可选)
                sendfile on;                                                                   ### 启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了(可选)
                
				### 设置字符编码,以支持中文文件名等(可选)
				charset utf8,gbk;                                                         ### 设置字符编码,以支持中文文件名等(可选)
        }

成为:

复制代码
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        
        location /f {


                ###  开启目录索引 (主要)
                autoindex on;                                                                ###  开启目录索引 (主要)



                ### 不显示具体文件大小(可选), 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
                autoindex_exact_size off;                                            ### 不显示具体文件大小(可选), 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

                ### 使用本地时间显示文件时间戳(可选)默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
                autoindex_localtime on;                                             ### 使用本地时间显示文件时间戳(可选)默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间

                ### 启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了(可选)
                sendfile on;                                                                   ### 启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了(可选)

				### 设置字符编码,以支持中文文件名等(可选)
				charset utf8,gbk;                                                         ### 设置字符编码,以支持中文文件名等(可选)


        }
        

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

然后重新加载nginx

bash 复制代码
sudo nginx -s reload

就能通过 "http://localhost/f" 访问到带索引功能的 /var/www/html/f

方法2, 在conf.d新建 x.conf 或在 sites-enabled 新建 x 配置文件

可以在 conf.d 文件夹中建立以.conf结尾的server块配置文件

或者在sites-enabled文件夹中建立任意名称的server块配置文件

例如:

复制代码
###  在/var/www/下建立一个名为fi的文件夹(/var/www/fi), http://hostname:6 能访问到这个文件夹
server {
        listen 6;

        location / {
                root /var/www/fi;
                autoindex on;  # 开启目录索引(主要)
                autoindex_exact_size off;  # 不显示具体文件大小(可选)
                autoindex_localtime on;  # 使用本地时间显示文件时间戳(可选)
                charset utf8,gbk;   # 设置字符编码,以支持中文文件名等(可选)

        }
}
### 在/var/www/fi下再建立一个名为fi的文件夹(/var/www/fi/fi)(两个fi), http://hostname:8/fi(一个fi) 能访问到这个文件夹
server {
        listen 8;

        location /fi {
                root /var/www/fi;


                ###  开启目录索引 (主要)
                autoindex on;                                                                ###  开启目录索引 (主要)



                ### 不显示具体文件大小(可选), 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
                autoindex_exact_size off;                                            ### 不显示具体文件大小(可选), 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

                ### 使用本地时间显示文件时间戳(可选)默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
                autoindex_localtime on;                                             ### 使用本地时间显示文件时间戳(可选)默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间

                ### 启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了(可选)
                sendfile on;                                                                   ### 启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了(可选)

				### 设置字符编码,以支持中文文件名等(可选)
				charset utf8,gbk;                                                         ### 设置字符编码,以支持中文文件名等(可选)

        }
}

###  在/var 下建立一个名为file的文件夹(/var/file), http://hostname:10/file 能访问到这个文件夹
server{
        listen 10;
        location /file {
                alias /var/file;
                autoindex on;
        }
}

###  在/var/file 下建立一个名为file的文件夹(/var/file/file), http://hostname:1119/file 能访问到这个文件夹   ###另外,本次端口9,11,111,110,119不能用,未深究,记录一下
server{
        listen 1119;
        location /file {
                root /var/file;
                autoindex on;
        }
}

###  创建文件夹/var/autoindex/file, http://hostname:12/file 能访问到这个文件夹
server{
        listen 12;
        location /file {
                root /var/autoindex;
                autoindex on;
        }
}

### root可以写在server层,也可以写在location层。 alias只能写在location层。  alias的文件位置是alias, root的文件位置是root+location 。   这个例子用http://localhost:133/file/  能访问到 /var/autoindex/file, 和上个例子一样
server{
        listen 133;
        root /var/autoindex;
        location /file {
                autoindex on;
        }
}

### root可以写在server层,也可以写在location层。 alias只能写在location层。  alias的文件位置是alias, root的文件位置是root+location 。   这个例子用http://localhost:14/file/   能访问到 /var/autoindex/a/b/c 文件夹
server{
        listen 14;
        location /file {
                alias /var/autoindex/a/b/c;
                autoindex on;
        }
}

AI回答1

通过Nginx启用文件索引服务:

  1. 安装Nginx

如果Nginx尚未安装,你可以使用以下命令通过APT进行安装:

bash 复制代码
sudo apt update
sudo apt install nginx
  1. 配置Nginx以启用文件索引

你需要编辑Nginx的配置文件来启用文件索引功能。通常,你可以在/etc/nginx/sites-available/目录下找到默认的配置文件(如default),或者你可以创建一个新的配置文件。

打开或创建配置文件并添加或修改location块以启用autoindex

bash 复制代码
sudo nano /etc/nginx/sites-available/default  # 或者你创建的新配置文件

在配置文件中,找到或添加你想要启用文件索引的location块,并添加autoindex指令:

nginx 复制代码
server {
    listen 80;
    server_name your_domain_or_IP;  # 替换为你的服务器域名或IP地址

    # ... 其他配置 ...

    location /files {  # 这是你想要启用文件索引的路径
        alias /var/www/files;  # 这是实际存放文件的目录路径
        autoindex on;  # 启用目录索引
        autoindex_exact_size off;  # 可选:不显示文件的确切大小
        autoindex_localtime on;  # 可选:使用本地时间显示文件时间戳
    }

    # ... 其他location块和配置 ...
}

注意:

  • alias指令用于指定实际存放文件的目录路径。确保该路径存在并且Nginx有权限读取其中的文件。
  • autoindex指令启用目录索引功能。
  • autoindex_exact_sizeautoindex_localtime是可选的,用于自定义目录索引的显示方式。
  1. 检查配置并重启Nginx

在保存配置文件后,使用nginx -t命令检查配置文件的语法是否正确:

bash 复制代码
sudo nginx -t

如果配置文件没有问题,使用以下命令重启Nginx服务以使配置生效:

bash 复制代码
sudo nginx -s reload

或者

bash 复制代码
sudo systemctl restart nginx

sendfile

nginx 中的 sendfile 指令是用于优化文件传输性能的。当 sendfile 指令被启用时,nginx 会利用操作系统提供的 sendfile() 系统调用来直接发送文件数据给客户端,而不是通过传统的 read()write() 方法。这种方式减少了数据在用户空间和内核空间之间的拷贝次数,从而提高了文件传输的效率。

使用方法

nginx 的配置文件中(通常是 nginx.conf),你可以通过以下方式启用 sendfile 指令:

nginx 复制代码
http {
    ...
    sendfile on;
    ...
}

或者,如果你想要在某个特定的 serverlocation 块中设置,也可以:

nginx 复制代码
server {
    ...
    location / {
        sendfile on;
        ...
    }
}

为什么要使用 sendfile

  1. 性能提升sendfile 通过减少数据拷贝次数,显著提高了文件传输的速度和效率。
  2. 资源利用 :由于减少了用户空间和内核空间之间的数据传输,sendfile 能够更有效地利用系统资源。
  3. 兼容性好sendfile 通常在大多数现代操作系统上都能很好地工作,包括 Linux、BSD 等。

注意事项

  • 配置检查 :确保在启用 sendfile 后,检查 nginx 的配置是否正确,并重启 nginx 以应用更改。
  • 操作系统支持 :虽然大多数现代操作系统都支持 sendfile,但最好确认你的系统是否提供了高效的 sendfile() 实现。
  • 其他优化sendfile 通常与其他优化选项一起使用,如 tcp_nopushtcp_nodelay,以达到最佳性能。

示例配置

以下是一个结合了 sendfile 和其他优化选项的 nginx 配置示例:

nginx 复制代码
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
}

在这个配置中,sendfile 被启用以优化文件传输,tcp_nopushtcp_nodelay 则是用来优化 TCP 连接的性能。这样的配置组合可以显著提升 nginx 的整体性能。

nginx配置文件的 rootalias

在Nginx配置文件中,rootalias是两个用于指定文件路径的重要指令,它们的主要区别在于Nginx如何解释location后面的URI,并如何将请求映射到服务器文件上。

root指令
  • 功能root指令用于指定web的家目录,也就是存放网站文件的目录。当Nginx接收到请求时,它会在这个指定的目录下查找请求的文件。
  • 用法 :在serverlocation块中使用root指令,后面跟上文件的绝对路径。例如:
nginx 复制代码
server {
    listen 80;
    server_name www.example.com;
    root /data/nginx/html;

    location / {
        # 这里不需要再指定root,因为已经在server块中指定了
    }

    location /images {
        # 如果需要在特定location中覆盖server块中的root设置
        # root /data/nginx/images;
    }
}
  • URI映射 :当使用root指令时,Nginx会将location块中指定的URI附加到root指定的根路径后面来查找文件。例如,如果root设置为/data/nginx/html,并且请求的URI是/index.html,那么Nginx会在/data/nginx/html/index.html查找文件。
alias指令
  • 功能alias指令用于定义路径别名,它会把访问的路径重新定义到其指定的路径,是文档映射的另一种机制。alias更多地用作路径的替换,而不是指定一个绝对的根目录。
  • 用法alias指令只能用于location上下文,后面跟上替换后的路径。例如:
nginx 复制代码
server {
    listen 80;
    server_name www.example.com;

    location /images/ {
        alias /data/nginx/static/images/;
    }
}
使用位置的区别

root\] 语法:root path 默认值:root html 配置段:http、server、location、if \[alias\] 语法:alias path 配置段:location * **URI映射** :当使用`alias`指令时,Nginx会将`location`块中指定的URI替换为`alias`指定的路径来查找文件。注意,这里的替换是完整的路径替换,而不是简单的附加。例如,如果请求的URI是`/images/logo.png`,并且`alias`设置为`/data/nginx/static/images/`,那么Nginx会在`/data/nginx/static/images/logo.png`查找文件。 在Nginx配置文件中,root和alias指令都用于设置资源文件的查找路径,但它们有一些区别: root指定了完整的路径,而alias指定了路径的一部分。 alias可以在location块中使用,而root不能。 root示例: ```nginx server { listen 80; server_name example.com; root /var/www/html; location / { index index.html index.htm; } } ``` 这里,Nginx会在/var/www/html目录下寻找资源,如果请求是http://example.com/image.png,则对应的文件路径为/var/www/html/image.png。 alias示例: ```nginx server { listen 80; server_name example.com; location /images/ { alias /var/www/images/; } } ``` 这里,alias指令将/images/请求映射到/var/www/images/目录。如果请求是http://example.com/images/cat.jpg,则对应的文件路径为/var/www/images/cat.jpg。 注意,当使用alias时,location后的URI与alias指定的路径之后的部分会拼接起来形成最终的文件路径。而root则不需要,因为root指定的是最顶层目录,后面的URI会直接拼接到root指定的路径后面。 ##### 注意事项 * 在使用`alias`时,要确保路径的正确性,并且路径末尾通常需要加上斜杠(/),以避免路径解析错误。 * `alias`和`root`在处理URI时的方式不同,因此在使用时要根据实际需求选择合适的指令。 * 谨慎使用`alias`与`root`,避免因为路径覆盖或错误配置导致的资源访问问题。 综上所述,`root`和`alias`都是Nginx配置文件中用于指定文件路径的重要指令,它们在不同的场景下有着各自的优势和适用情况。正确理解和使用这两个指令,对于优化Nginx的文件传输性能和资源访问管理具有重要意义。

相关推荐
深圳市九鼎创展科技4 小时前
MT8883 vs RK3588 开发板全面对比:选型与场景落地指南
大数据·linux·人工智能·嵌入式硬件·ubuntu
闫利朋4 小时前
Ubuntu22.04桌面版安装Cubic2024版本并定制ISO完整教程
ubuntu·自动化的安装系统
高木木的博客6 小时前
数字架构智能化测试平台(1)--总纲
人工智能·python·nginx·架构
徐子元竟然被占了!!7 小时前
Nginx
运维·nginx
烛衔溟7 小时前
TypeScript this 参数类型与全局 this
javascript·ubuntu·typescript
图图玩ai8 小时前
SSH 命令管理工具怎么选?从命令收藏到批量执行一次讲清
linux·nginx·docker·ai·程序员·ssh·可视化·gmssh·批量命令执行
CXH7289 小时前
nginx——https
运维·nginx·https
Lentou11 小时前
nginx反向代理
运维·nginx
遇见火星11 小时前
linux设置开启启动服务
linux·运维·服务器·nginx
今晚务必早点睡11 小时前
Ubuntu 部署 RuoYi-Vue-FastAPI 完整实战指南(含踩坑总结)
vue.js·ubuntu·fastapi