nginx代理后前端找不到js和css文件,配置文件应该如何写
时间: 2024-03-09 09:45:16 浏览: 26
如果nginx代理后前端找不到js和css文件,可能是因为nginx没有正确地代理这些文件。你可以尝试在nginx的配置文件中添加以下内容:
```
location / {
# your existing configuration
proxy_pass http://your_backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# add the following lines for serving static files
proxy_set_header Accept-Encoding "";
proxy_set_header Accept-Language $http_accept_language;
proxy_set_header Referer $http_referer;
add_header Cache-Control "public, max-age=31536000, immutable";
expires 365d;
# add the following block for serving js and css files
location ~* \.(js|css)$ {
expires 1d;
add_header Cache-Control "public, max-age=86400";
proxy_pass http://your_backend_server;
}
}
```
这个配置文件中包含了对静态文件的缓存设置,以及对js和css文件的代理设置。其中,`your_backend_server`应该替换为你的后端服务器的地址。希望这可以帮助你解决问题。