2014-04-05 46 views
0

我該如何讓我的Servlet拋出一個錯誤404,它將被我的Nginx代理重定向?讓Servlet通過Nginx反向代理拋出錯誤404?

對於Nginx引發的404,它工作正常。但是,如果請求通過Tomcat進行代理,則錯誤404頁面來自Tomcat,而不是Nginx。

我可以以某種方式返回一個錯誤404,然後由Nginx重定向選取嗎?或者我需要在web.xml中配置一個並行的404錯誤頁面?

,我有以下包含在我的nginx的配置:

location ~ \.do$ { 
    proxy_pass    http://127.0.0.1:8080; 
    proxy_set_header  Host $http_host; 
    proxy_set_header  X-Real-IP $remote_addr; 
    proxy_set_header  X-Forwarded-Host $host; 
    proxy_set_header  X-Forwarded-Server $host; 
    proxy_set_header  X-Forwarded-Proto $scheme; 
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
} 

error_page 404 = @page-not-found; 
location @page-not-found { 
    rewrite .* /search?status=page-not-found permanent; 
} 

而像一個servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    String handle = request.getParameter("handle");  
    Page page = pageDao.getPageByHandle(handle);   
    if(page.getId()!=null){ 
     request.setAttribute("page", page); 
     request.getRequestDispatcher("/site/page.jsp").forward(request , response); 
    } 
    response.sendError(HttpServletResponse.SC_NOT_FOUND); 
} 

回答

0

看來你只是在nginx的配置添加proxy_intercept_errors on ...

由於在:

proxy_intercept_errors on; 

error_page 404 = @page-not-found; 
location @page-not-found { 
    rewrite .* /search?status=page-not-found permanent; 
}