参考资料

  1. 如何查看nginx主配置文件路径方式
  2. Nginx的配置与管理
  3. nginx 配置ssl证书
  4. FastCGI(Fast Common Gateway Interface,快速通用网关接口)详细说明以及案例
  5. Nginxlocalhost 路由匹配规则详细说明以及案例
  6. Nginxindex:首页处理模块详细说明以及案例
  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 即可获取静态资源。