温馨提示×

nginx怎么配置访问静态文件

小亿
111
2024-06-06 15:50:20
栏目: 编程语言

要配置 NGINX 以访问静态文件,您需要编辑 NGINX 的配置文件,并将以下配置添加到其中:

server {
    listen 80; # 监听端口

    server_name example.com; # 您的域名

    location /static {
        alias /path/to/static/files; # 静态文件的路径
    }

    location / {
        proxy_pass http://localhost:8000; # 反向代理到您的应用程序
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

在上面的配置中,您需要将 example.com 替换为您的域名,将 /path/to/static/files 替换为存储静态文件的路径。您还可以根据需求更改端口号和其他配置选项。

保存并重新加载 NGINX 配置文件后,您的 NGINX 服务器就会配置为访问静态文件了。您可以直接通过 http://example.com/static 访问静态文件。

0