ubuntu22.04 nginx配置下载目录,亲测成功

安装nginx

ubuntu最简单,apt安装即可

apt install nginx 

配置文件

文件都在目录下

/etc/nginx/

添加内容

修改/ etc/nginx/sites-available/default , 注意这里不是nginx.conf,直接修改nginx.conf不奏效

        location /downloads {             # 域名后缀
                alias /work/downloads;    # 监听的下载目录
                autoindex on;			  #
                sendfile on;
                autoindex_exact_size on; # 显示确切大小,单位bytes
                autoindex_localtime on;  # 显示服务器时间
                charset utf-8,gbk;       # 避免中文乱码
        }

这一段插入Server配置中,修改完如下

root@node-1:~# cat /etc/nginx/sites-available/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;
        }
        location /downloads {
                alias /work/downloads;
                autoindex on;
                sendfile on;
                autoindex_exact_size on; # 显示确切大小,单位bytes
                autoindex_localtime on;  # 显示服务器时间
                charset utf-8,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;
        #}
}

重启服务

systemctl reload nginx

查看结果

在这里插入图片描述

### 安装 Nginx 的方法 #### 使用默认存储库安装 Nginx 对于希望快速部署 Nginx 而不涉及自定义配置或最新版本需求的情况,可以从 Ubuntu 默认仓库直接安装 Nginx。这通常是最简单的方式。 ```bash sudo apt update sudo apt install nginx ``` 上述命令会更新包列表并安装最新的官方稳定版 Nginx[^1]。 #### 下载源码编译安装特定版本的 Nginx 如果需要更具体的控制权或是想要获取尚未加入到官方仓库中的新特性,则可以选择从官方网站下载指定版本的 tarball 文件来手动编译安装: ```bash wget http://nginx.org/download/nginx-1.22.1.tar.gz tar -zxvf nginx-1.22.1.tar.gz cd nginx-1.22.1/ ./configure make && sudo make install ``` 这种方法允许用户根据自己的环境调整编译选项,但同时也增加了复杂度和潜在的风险[^2]。 #### 离线环境下编译安装 Nginx 针对那些无法连接互联网的工作站或者安全性较高的生产环境中,可以采用离线方式准备所有必要的依赖项以及 Nginx 源文件,在目标机器上完成整个构建过程。具体操作可参照相关项目的指导文档[^3]。 #### 验证安装成功与否 无论采取哪种方式进行安装,最后都应该验证 Web Server 是否正常工作。可以通过浏览器访问服务器 IP 地址查看是否有默认欢迎页面显示出来确认这一点[^5]。 另外,还可以通过以下命令管理 Nginx 服务的状态: 停止 Nginx 服务: ```bash sudo systemctl stop nginx ``` 重启 Nginx 服务: ```bash sudo systemctl restart nginx ``` 查询当前的服务状态: ```bash sudo systemctl status nginx ``` 这些指令有助于日常维护和服务监控[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值