favicon.ico
favicon.ico檔案是浏覽器收藏網址時顯示的圖示,當用戶端使用浏覽器頁面時,浏覽器會自己主動發起請求擷取頁面的favicion.ico檔案。這樣就會引起一些問題。解決辦法。
- 前端頁面添加以下代碼不讓浏覽器主動請求。
- 後端代碼通過filter攔截/favicon的請求傳回圖示
public class FaviconFilter extends OncePerRequestFilter { static final String FAVICON_PATH = "/favicon.ico"; private final byte[] faviconFile; public FaviconFilter(byte[] faviconFile) { this.faviconFile = faviconFile; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (FAVICON_PATH.equals(request.getRequestURI())) { response.setStatus(HttpServletResponse.SC_OK); response.getOutputStream().write(faviconFile); } else { filterChain.doFilter(request, response); } } }
- nginx配置
server { server_name www.mylinuxops.com; access_log /var/log/nginx/access.log access_json; location / { root /data/www; index index.html; } location = /favicon.ico { #精确定位ifavicon.ico檔案 log_not_found off; #找不到檔案時日志不記錄 access_log off; #關閉日志記錄 } }