鍍金池/ 問答/Java  Linux/ Nginx反向代理的問題

Nginx反向代理的問題

問題:使用Spring Boot開發(fā),然后在服務(wù)器上使用Nginx反向代理8080端口,然后問題就出現(xiàn)了,在網(wǎng)站后臺(tái)登錄的時(shí)候,用戶名和密碼都正確,也提示登錄成功,但是重定向的時(shí)候又回到了登錄頁,這里是加了攔截器的,直接訪問后臺(tái)的鏈接也直接回到登錄頁,感覺就像是登錄成功后,session沒有創(chuàng)建一樣。如果加上8080端口的話就不會(huì)出現(xiàn)這樣的問題,大家有經(jīng)驗(yàn)的幫忙看一下,謝謝了。

相關(guān)代碼:

對(duì)登錄頁面的請(qǐng)求:

    @GetMapping(value = "/login")
    public String login(HttpSession session,Model model){
        model.addAttribute("options",HaloConst.OPTIONS);
        User user = (User) session.getAttribute("user");
        //如果session存在,跳轉(zhuǎn)到后臺(tái)首頁
        if(null!=user){
            return "redirect:/admin";
        }
        return "admin/admin_login";
    }

登錄請(qǐng)求:

    @PostMapping(value = "/getLogin")
    @ResponseBody
    public String getLogin(@ModelAttribute("loginName") String loginName,
                           @ModelAttribute("loginPwd") String loginPwd,
                           HttpSession session){
        String status = "false";
        try {
            User aUser = userService.findUser();
            User user = null;
            if("false".equals(aUser.getLoginEnable())){
                status = "disable";
            }else{
                //驗(yàn)證是否是郵箱登錄
                Pattern patternEmail = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
                Matcher matcher = patternEmail.matcher(loginName);
                if(matcher.find()){
                    user = userService.userLoginByEmail(loginName,HaloUtil.getMD5(loginPwd)).get(0);
                }else{
                    user = userService.userLoginByName(loginName,HaloUtil.getMD5(loginPwd)).get(0);
                }
                if(aUser==user){
                    session.setAttribute(HaloConst.USER_SESSION_KEY, user);
                    //重置用戶的登錄狀態(tài)為正常
                    userService.updateUserNormal();
                    userService.updateUserLoginLast(new Date());
                    logsService.saveByLogs(new Logs(LogsRecord.LOGIN,LogsRecord.LOGIN_SUCCESS,HaloUtil.getIpAddr(request), HaloUtil.getDate()));
                    status = "true";
                }
            }
        }catch (Exception e){
            Integer errorCount = userService.updateUserLoginError();
            if(errorCount>=5){
                userService.updateUserLoginEnable("false");
            }
            userService.updateUserLoginLast(new Date());
            logsService.saveByLogs(new Logs(LogsRecord.LOGIN,LogsRecord.LOGIN_ERROR+"["+loginName+","+loginPwd+"]",HaloUtil.getIpAddr(request),new Date()));
            log.error("登錄失?。。簕0}",e.getMessage());
        }
        return status;
    }

攔截器:

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object obj = request.getSession().getAttribute(HaloConst.USER_SESSION_KEY);
        //如果user不為空則放行
        if(null!=obj){
            return true;
        }
        //否則攔截并跳轉(zhuǎn)到登錄
        response.sendRedirect("/admin/login");
        return false;
    }

注冊(cè)攔截器:

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/getLogin")
                .excludePathPatterns("/static/**");
    }

nginx配置(使用的寶塔面板配置的反向代理):

server
{
    listen 80;
    server_name 域名;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/halo;
    
    #SSL-START SSL相關(guān)配置,請(qǐng)勿刪除或修改下一行帶注釋的404規(guī)則
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  錯(cuò)誤頁配置,可以注釋、刪除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注釋或修改

    #PROXY-START
    location ~ /purge(/.*) { 
        proxy_cache_purge cache_one $host$request_uri$is_args$args;
        #access_log  /www/wwwlogs/slogc.cc_purge_cache.log;
    }
    location / 
    {
        proxy_pass http://ip:8090;
        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 REMOTE-HOST $remote_addr;
        
        #緩存相關(guān)配置
        #proxy_cache cache_one;
        #proxy_cache_key $host$request_uri$is_args$args;
        #proxy_cache_valid 200 304 301 302 1h;
        
        #持久化連接相關(guān)配置
        #proxy_connect_timeout 30s;
        #proxy_read_timeout 86400s;
        #proxy_send_timeout 30s;
        #proxy_http_version 1.1;
        #proxy_set_header Upgrade $http_upgrade;
        #proxy_set_header Connection "upgrade";
        
        #add_header X-Cache $upstream_cache_status;
        
        expires 12h;
    }
    
    location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
    {
        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 REMOTE-HOST $remote_addr;
        proxy_pass http://123.207.101.207:8090;
        
    }
}
回答
編輯回答
溫衫

因?yàn)槟阌昧藃edirect, 可以與此有關(guān)

在nginx配置里增加proxy_redirect試試:

location /{
...
      proxy_redirect   / /;
...
}
2018年7月24日 20:38