美文网首页
Nginx 配置一个文件下载功能(并修改页面样式)

Nginx 配置一个文件下载功能(并修改页面样式)

作者: 王某某的笔记 | 来源:发表于2024-09-06 00:03 被阅读0次

2024-9-6 修改,新增CSS样式

nginx 指定一个目录提供文件列表和下载功能

原始的

nginx 配置文件

server {
    listen 8443 ssl;
    server_name download.yousite.com;
    
    access_log /var/log/nginx/downloads_access.log;
    
    # 证书
    ssl_certificate /etc/letsencrypt/live/yousite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yousit.com/privkey.pem;
    
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # 根目录设置为 /www/mnt/downloads
    root /www/mnt/downloads;

    # 启用目录列表
    location / {
        autoindex on; # 启用目录列表
        autoindex_exact_size off; # 以更人性化的格式显示文件大小
        autoindex_localtime on; # 以服务器本地时间显示文件时间
        charset utf-8; # 设置字符集为 UTF-8
        try_files $uri $uri/ =404; # 确保请求的文件或目录存在,否则返回 404

        #阻止其他网站直接链接你的文件
        valid_referers none blocked yousite.com *.yousite.com;
        if ($invalid_referer) {
                return 403;
        }
    }

    # 禁止访问上级目录,防止目录遍历攻击
    location ~ /\.\./ {
        deny all;
    }

}

展示效果

原始效果.png

但是这个页面比较难看,特别是对于移动端浏览器很难用,文件都选不中,于是就想着修改一下

修改后的

不想使用其他的文件服务程序,就用 sub_filter 替换内容来实现引入一个CSS样式文件

修改后的nginx配置

server {
    listen 8443 ssl;
    server_name download.yousite.com;

    access_log /var/log/nginx/downloads_access.log;

    # 证书
    ssl_certificate /etc/letsencrypt/live/yousite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yousite.com/privkey.pem;

    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # 根目录设置为 /www/mnt/downloads
    root /www/mnt/downloads;

    # 启用目录列表
    location / {
        autoindex on; # 启用目录列表
        autoindex_exact_size off; # 以更人性化的格式显示文件大小
        autoindex_localtime on; # 以服务器本地时间显示文件时间
        charset utf-8; # 设置字符集为 UTF-8
        try_files $uri $uri/ =404; # 确保请求的文件或目录存在,否则返回 404

        #内容替换
        sub_filter '</head>' '<link rel="stylesheet" href="/css/download.css"></head>';
        sub_filter '<title>Index of' '<title>目录';
        sub_filter '<h1>Index of' '<h1>目录';
        sub_filter '</body>' '<p>王某某的公开下载服务</p></body>';
        sub_filter '<hr>' '';
        sub_filter_once off;

        #阻止其他网站直接链接你的文件
        valid_referers none blocked yousite.com *.yousite.com;
        if ($invalid_referer) {
                return 403;
        }
    }

    # 禁止访问上级目录,防止目录遍历攻击
    location ~ /\.\./ {
        deny all;
    }

    #配置自定义的CSS
    location /css/download.css {
        alias /www/mnt/service/download/download.css;
    }

    #favicon.ico
    location /favicon.ico {
        alias /www/mnt/service/download/favicon.ico;
    }

}

主要是使用 sub_filter 替换内容,从而实现引入一个CSS 文件

download.css文件内容

body {
    margin: 0;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

body pre {
    font-family: Microsoft YaHei;
    font-size: 1rem;
    width: 960px;
    text-align: right;
    padding-right: 20px;
    margin-top: 0px;
}

a {
    text-decoration: none;
    border: 1px solid #ccc;
    border-radius: 5px;
    padding: 5px 10px;

    display: flex;
    position: relative;
    top: 26px;
    left: 10px;
}

a:hover {
    border-color: #000;
}

body p {
    margin-top: auto;
    margin-bottom: 10px;
}

/* 针对移动设备的 */
@media (max-width: 980px) {
    body pre {
        font-size: 1.4rem;
    }

    a {
        top: 35px;
    }
}

将CSS文件保存到上面指定的位置,如:/www/mnt/service/download/download.css;

展示效果

修改后的效果.png
手机端浏览器
手机端浏览.png

相关文章

  • nginx(五)nginx cmd

    nginx cmd 1、启动nginx start nginx 2、修改配置文件并生效 测试nginx配置文件是否...

  • 基于Nginx和FFmpeg搭建流媒体服务器(Windows)

    搭建Nginx环境 1.下载并配置Nginx Nginx下载下载完成后,解压Nginx压缩包,并将Nginx的文件...

  • Nginx安装

    安装nginx需要的linux环境: 下载nginx 编译安装 使用nginx代理 ,修改配置文件 启动nginx...

  • 搭建CI使用Laravel ORM模块

    下载CI框架并解压到工作目录 配置nginx,并重启nginx -s reload 修改本地hosts配置 此时已...

  • nginx图片服务器

    以windows为例1下载nginx文件, 2修改nginx.conf配置文件 修改端口为8083注意以alias...

  • zabbix监控nginx

    一、打开nginx的监控页面 二、导入模板 三、修改宏 四、准备zabbix的配置文件,并测试脚本 五、在zabb...

  • 使用nginx作为websocket的proxy server

    以下内容均copy该文章,链接地址 一、下载 下载链接。下载后解压即可。 二、修改配置文件 nginx配置文件在 ...

  • 通过nginx发布IOS包

    需要安装nginx并配置支持https协议 编辑 .plist文件: 编辑 页面,跳转到 .plist文件

  • Nginx gzip压缩实现性能优化

    介绍 通过修改nginx配置文件nginx.conf开启gzip压缩功能,这样nginx服务器会根据配置的策略对发...

  • 二、指令

    nginx启动: nginx停止: 修改配置文件后检查配置文件 重启

网友评论

      本文标题:Nginx 配置一个文件下载功能(并修改页面样式)

      本文链接:https://www.haomeiwen.com/subject/isudljtx.html