参考资料

  1. 忽略非错误信息 nginx -q详细说明以及案例
  2. 如何查看要查看 Nginx 默认配置文件路径
  3. Nginx请求频率限制模块详细说明以及案例
  4. Nginx代理缓存配置详细说明以及案例
  5. nginx -p详细说明以及案例
  6. Nginx进程配置指令详解
  7. Nginx教程,一看就懂,一学就会
  8. FastCGI(Fast Common Gateway Interface,快速通用网关接口)详细说明以及案例

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 即可获取静态资源。