Featured image of post Nginx图床部署

Nginx图床部署

nginx 图床部署

下面将以【example.com】域名为例

图床根目录地址:https://blog.example.com:666/
图床访问图片地址:https://blog.example.com:666/img/tmdb-01.jpg

必须匹配到图片文件才可以
访问根目录,显示nginx欢迎页面

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
mkdir -pm 755 /etc/nginx/conf.d /mnt/file/blog && touch /etc/nginx/conf.d/blog.conf && cat <<'EOF' | sed '1!{/^[[:space:]]*#/d;/^[[:space:]]*$/d}' > /etc/nginx/conf.d/blog.conf
## nginx 图床服务
server {
    # 监听666端口,并启用SSL
    listen 666 ssl;
    # 替换为你的域名
    server_name blog.example.com.org;

    # 指定SSL证书文件和私钥文件的路径
    ssl_certificate /etc/nginx/keyfile/cert.pem;  
    ssl_certificate_key /etc/nginx/keyfile/key.pem;  

    # 设置支持的SSL协议版本
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # 启用服务器端加密套件优先
    ssl_prefer_server_ciphers on;
    # 设置加密套件,优先使用高强度加密算法,并排除匿名加密套件和MD5散列算法
    ssl_ciphers HIGH:!aNULL:!MD5;

    # 配置静态文件的访问规则
    location ~ .*\.(gif|jpg|lrc|txt|doc|pdf|rar|gz|zip|jpeg|png|mp4|mkv|html|yaml)$ {  
        root /mnt/file/blog; # 文件存放的位置
        # 防止浏览器预览打开,而是下载文件
        if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|lrc|docx|exe|xlsx|ppt|pptx|yaml)$){
           add_header Content-Disposition attachment;
       }
        autoindex on; # 开启目录浏览功能
        sendfile on; # 开启高效文件传输模式
        autoindex_localtime on; # 显示的文件时间为文件的服务器时间
        autoindex_format html;  # 输入格式,可选项为html、xml、json、jsonp
        autoindex_exact_size off; # 显示精确字节大小还是显示友好可读的大小
        charset utf-8,gbk; # 避免中文乱码
        expires 24h;  # 设置文件过期时间为24小时
        proxy_store on;  
        proxy_store_access user:rw group:rw all:rw;  
        proxy_redirect off;  
        proxy_set_header X-Real-IP $remote_addr;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        client_max_body_size 10m;  
        client_body_buffer_size 1280k;  
        proxy_connect_timeout 900;  
        proxy_send_timeout 900;  
        proxy_read_timeout 900;  
        proxy_buffer_size 40k;  
        proxy_buffers 40 320k;  
        proxy_busy_buffers_size 640k;  
        proxy_temp_file_write_size 640k;  
    }

    # 新增的location块,用于处理没有后缀名的文件
    location ~* ^/[^.]+$ {  # 匹配URL中不包含点号的路径
        root /mnt/file/blog; # 文件存放的位置
        add_header Content-Disposition "attachment; filename=$uri"; # 强制下载
        autoindex on; # 如果需要的话,也可以开启目录浏览功能
        sendfile on; # 开启高效文件传输模式
        autoindex_localtime on; # 显示的文件时间为文件的服务器时间
        autoindex_format html;  # 输入格式,可选项为html、xml、json、jsonp
        autoindex_exact_size off; # 显示精确字节大小还是显示友好可读的大小
        charset utf-8,gbk; # 避免中文乱码
        expires 24h;  # 设置文件过期时间为24小时
    }
}
EOF

## 重启 Nginx
sudo systemctl restart nginx
最后更新于 2025-02-22
本博客已稳定运行 小时 分钟
共发表 112 篇文章 · 总计 109.83 k 字
本站总访问量