nginx配置图片静态路由

系统:ubuntu20.04

1、安装nginx

bash 复制代码
sudo apt update -y # 更新源
sudo apt install nginx -y # 安装
nginx -v # 查看版本
sudo systemctl status nginx # 查看状态
sudo systemctl start/stop/restart nginx # 开启/关闭/重启nginx

2、nginx常用指令

bash 复制代码
sudo systemctl start nginx # 开始 Nginx
sudo systemctl stop nginx # 停止 Nginx
sudo systemctl restart nginx # 重启 Nginx 
sudo systemctl reload nginx # 平滑加载配置(推荐)
sudo systemctl status nginx # 查看状态
sudo systemctl enable nginx # 设置开机自启
sudo systemctl disable nginx # 关闭开机自启 
sudo nginx -t # 检查配置语法

3、防火墙端口放行

ubuntu 默认启用 ufw 防火墙,需放行 16010 端口:

bash 复制代码
sudo ufw allow 16010/tcp # 放行16010端口 
sudo ufw reload # 重载防火墙规则 
sudo ufw status # 查看已放行端口(验证)

4、nginx代理配置

4.1、资源位置

端口:16010

文件位置:/home/data/files/,映射至:img.example.com:16010/files/

图片位置:/home/data/imgs/,映射至:img.example.com:16010/imgs/

4.2、配置文件

位置:/etc/nginx/conf.d/img_com.conf

4.3、配置文件

1)不设置全局root,为/files/ 和/imgs/ 分别配置独立的 alias 映射,避免路径冲突;

2)非/files/ 和/imgs/ 的请求直接返回 404,避免无关路径暴露。

bash 复制代码
server {
    listen   0.0.0.0:16010;  # 监听所有网卡的16010端口
    server_tokens off;       # 隐藏Nginx版本号
    sendfile on;             # 提升静态文件传输效率
    tcp_nopush on;           # 合并数据包发送
    tcp_nodelay on;          # 降低网络延迟

    # 全局根目录(配合root规则使用,避免alias拼接问题)
    root /home/data/;

    # 1. 静态文件目录映射:/files/ → /home/data/files/
    # 改用root:root路径 + location路径 = 实际路径(/home/data/ + /files/ = /home/data/files/)
    location /files/ {
        try_files $uri =404;  # 找不到文件返回404

        # 修复:匹配files/下的所有静态资源(txt/log/js/css/图片等)
        location ~* \.(txt|log|jpg|jpeg|png|gif|css|js|ico)$ {
            expires 30d;
            add_header Cache-Control "public, immutable";
            add_header Access-Control-Allow-Origin "*";
            add_header Vary "Origin";
            access_log off;
            log_not_found off;
        }

        # 禁止访问隐藏文件
        location ~ /\. {
            deny all;
        }

        # 禁止执行脚本文件
        location ~* \.(php|pl|py|jsp|asp|sh|cgi)$ {
            return 444;
        }
    }

    # 2. 图片目录映射:/imgs/ → /home/data/imgs/
    # 改用root:/home/data/ + /imgs/ = /home/data/imgs/(更稳定)
    location /imgs/ {
        try_files $uri =404;

        # 图片缓存配置(1周,你之前写的30d也可以)
        location ~* \.(jpg|jpeg|png|gif|webp|avif|svg)$ {
            expires 1w;
            add_header Cache-Control "public";
            add_header Access-Control-Allow-Origin "*";
            add_header Vary "Origin";
            access_log off;
            log_not_found off;
        }

        # 禁止访问隐藏文件
        location ~ /\. {
            deny all;
        }

        # 禁止执行脚本文件
        location ~* \.(php|pl|py|jsp|asp|sh|cgi)$ {
            return 444;
        }
    }

    # OPTIONS 预检请求处理(跨域必备)
    location / {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Max-Age' '86400';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,x-requested-with,token,userType';
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' '0';
            return 204;
        }
        try_files $uri =404;  # 非/files/和/imgs/的路径返回404
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Vary' 'Origin';
    }

    # 全局禁止访问隐藏文件
    location ~ /\. {
        access_log off;
        log_not_found off;
        deny all;
    }

    # 错误页面配置(若没有404.html/50x.html,可注释,不影响核心功能)
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
        internal;
    }
}

5、公网访问必备配置(关键)

若需通过公网 IP 访问,需确保服务器放行相关端口,且公网访问需放行防火墙端口,并确保 Nginx 有目录读取权限。

bash 复制代码
# 1. 开放防火墙16010端口(CentOS7/8)
firewall-cmd --add-port=16010/tcp --permanent
firewall-cmd --reload

# 2. 若开启SELinux,需允许Nginx访问/home/data目录(可选)
setsebool -P httpd_read_user_content 1

# 3. 确保Nginx有/home/data目录的读取权限
chmod -R 755 /home/data/
chown -R nginx:nginx /home/data/  # Nginx运行用户为nginx时

6、配置生效与测试

bash 复制代码
# cp img_com.conf /etc/nginx/conf.d/ # 拷贝更新配置
nginx -t -c /etc/nginx/nginx.conf # 验证配置语法
nginx -s reload # 重新加载配置
service nginx restart # nginx重启

7、测试访问

本地测试:curl -I http://127.0.0.1:16010/imgs/001.jpg;

公网测试:curl -I http://113.200.109.245:16010/imgs/001.jpg;

返回 HTTP/1.1 200 OK 表示配置生效(文件存在时)。

文件位置:/home/data/files/,映射至:http://113.200.109.245:16010/files/

图片位置:/home/data/imgs/,映射至:http://113.200.109.245:16010/imgs/

参考资料:

1、Nginx配置静态资源和图片资源

https://blog.csdn.net/u011291844/article/details/152170547

相关推荐
明天…ling2 小时前
sql注入(1-10关)
java·数据库·sql
快快起来写代码2 小时前
Jenkins学习
数据库·学习·jenkins
chalmers_152 小时前
MongoDB实现发布订阅机制
数据库·mongodb
Ashley_Amanda2 小时前
SAP调用Web Service全流程详解
java·前端·数据库
@老蝴2 小时前
MySQL数据库 - 事务
java·数据库·mysql
Mr Robot2 小时前
数据库概述
数据库·oracle
what丶k2 小时前
Java连接人大金仓数据库(KingbaseES)全指南:从环境搭建到实战优化
java·开发语言·数据库
kida_yuan2 小时前
【Oracle】Ubuntu 部署 Oracle 10g 的完整实战复盘
数据库·ubuntu·oracle
霖霖总总2 小时前
[小技巧44]MySQL Purge 线程详解:作用、机制与性能优化
数据库·mysql