参考资料

  1. Nginx error_log:错误日志配置详细说明以及案例
  2. Nginx stream模块详细说明以及案例
  3. Service Worker文件如何更新?
  4. Nginxserver_name 配置主机名称详细说明以及案例
  5. Nginx Stream(TCP/UDP)负载均衡详细说明以及案例
  6. Nginxgzip开启压缩及相关配置详细说明以及案例
  7. Nginx的应用场景
  8. Nginx基本介绍

nginx 配置静态资源

在 Nginx 中配置静态资源的步骤如下:

  1. 编辑 Nginx 配置文件
    通常位于 /etc/nginx/nginx.conf/etc/nginx/sites-available/default

  2. 添加静态资源路径
    server 块中添加以下内容:

    server {
        listen 80;
        server_name example.com;
    
        location /static/ {
            alias /path/to/your/static/files/;
            autoindex off;
        }
    
        location /media/ {
            alias /path/to/your/media/files/;
            autoindex off;
        }
    }
    • /static/ 是 URL 路径,访问时使用 http://example.com/static/

    • /path/to/your/static/files/ 是服务器上静态文件的实际路径。

    • autoindex off 禁止目录列表。

  3. 检查配置并重启 Nginx

    sudo nginx -t  # 检查配置是否正确
    sudo systemctl restart nginx  # 重启 Nginx
  4. 访问静态资源
    通过浏览器访问 http://example.com/static/yourfile.ext 即可获取静态资源。