参考资料

  1. PHP-FPM 性能优化
  2. OpenResty编译安装详细说明以及案例
  3. Nginx的启动与停止命令
  4. NginxHTTPS服务器搭建详细说明以及案例
  5. 如何配置Nginx反向代理?
  6. Nginx负载均衡策略详解
  7. NginxFastCGI模块配置详细说明以及案例
  8. Nginxlocalhost 路由匹配规则详细说明以及案例

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