添加nginx配置文件,解决nginx代理后无法获取客户端真实IP的问题

This commit is contained in:
2018-03-16 13:04:39 +08:00
parent 271934de3c
commit b55b735bb2
9 changed files with 118 additions and 8 deletions

44
cms.nginx - 副本.conf Normal file
View File

@@ -0,0 +1,44 @@
upstream cms-host{
server cms.fjy8018.xin:8085;
}
server {
listen 80;
listen 443 ssl;
server_name cms.fjy8018.xin; #域名
index index.html; #指定的server的root的访问页面
root /index; #指定的server的root目录
#SSL-START SSL相关配置请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /www/wwwroot/cms/full_chain.pem;
ssl_certificate_key /www/wwwroot/cms/private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;
#SSL-END
#charset koi8-r;
#access_log logs/host.access.log main;
#我工程的context-path=mytest
location /cms {
proxy_pass http://cms.fjy8018.xin:8085;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
# root html;
# index index.html index.htm;
}
}

44
cms.nginx.conf Normal file
View File

@@ -0,0 +1,44 @@
upstream cms-host{
server cms.fjy8018.xin:8085;
}
server {
listen 80;
listen 443 ssl;
server_name cms.fjy8018.xin; #域名
index index.html; #指定的server的root的访问页面
root /index; #指定的server的root目录
#SSL-START SSL相关配置请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /www/wwwroot/cms/full_chain.pem;
ssl_certificate_key /www/wwwroot/cms/private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;
#SSL-END
#charset koi8-r;
#access_log logs/host.access.log main;
#我工程的context-path=mytest
location /cms {
proxy_pass http://cms.fjy8018.xin:8085;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
# root html;
# index index.html index.htm;
}
}

View File

@@ -5,7 +5,7 @@
<groupId>com.fjy</groupId> <groupId>com.fjy</groupId>
<artifactId>spring</artifactId> <artifactId>spring</artifactId>
<version>V2.6.4</version> <version>V2.6.7</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>spring</name> <name>spring</name>

View File

@@ -8,6 +8,7 @@ import com.fjy.spring.service.AdminService;
import com.fjy.spring.service.LogService; import com.fjy.spring.service.LogService;
import com.fjy.spring.service.UserService; import com.fjy.spring.service.UserService;
import com.fjy.spring.untils.CodingUtil; import com.fjy.spring.untils.CodingUtil;
import com.fjy.spring.untils.GetIPAddrUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@@ -57,7 +58,9 @@ public class LoginController {
log.setUserid(user.getColuserid()); log.setUserid(user.getColuserid());
log.setColtime(dateNowStr); log.setColtime(dateNowStr);
log.setColheader(request.getHeader("user-agent")); log.setColheader(request.getHeader("user-agent"));
log.setColip(request.getRemoteAddr()); //解决nginx代理后IP地址获取问题
//log.setColip(request.getRemoteAddr());
log.setColip(GetIPAddrUtil.getIpAddr(request));
logService.addLogRec(log); logService.addLogRec(log);
return "redirect:" + request.getScheme() + "://" + request.getServerName() + ":" return "redirect:" + request.getScheme() + "://" + request.getServerName() + ":"

View File

@@ -2,7 +2,6 @@ package com.fjy.spring.interceptor;
import com.fjy.spring.constant.GlobalConstant; import com.fjy.spring.constant.GlobalConstant;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;

View File

@@ -0,0 +1,22 @@
package com.fjy.spring.untils;
import javax.servlet.http.HttpServletRequest;
public class GetIPAddrUtil {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader(" Proxy-Client-IP ");
//System.out.println("【Proxy-Client-IP】");
}
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader(" WL-Proxy-Client-IP ");
//System.out.println("【WL-Proxy-Client-IP】");
}
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
//System.out.println("【unknown】");
}
return ip;
}
}

View File

@@ -7,7 +7,6 @@
src: url('../fonts/element-icons.ttf'), src: url('../fonts/element-icons.ttf'),
url('../fonts/element-icons.woff'); /* IE9 */ url('../fonts/element-icons.woff'); /* IE9 */
} }
*{padding:0;margin:0;}
html{height:100%;} html{height:100%;}
body{min-height:100%;} body{min-height:100%;}
.loginTitle{ .loginTitle{
@@ -60,7 +59,6 @@ a{
.footer{ .footer{
width:100%; width:100%;
font-size:14px; font-size:14px;
position:absolute;
bottom:20px; bottom:20px;
left:0; left:0;
text-align: center; text-align: center;

View File

@@ -108,9 +108,8 @@
<el-footer th:fragment="footer"> <el-footer th:fragment="footer">
<div class="footer"> <div class="footer">
© 2018 作业提交系统 Copyright © 2018 作业提交系统 All Rights Reserved|
<br> <a href="http://www.miitbeian.gov.cn/">粤ICP备18026008号</a> 粤ICP备18026008号-1
<a href="http://gdcainfo.miitbeian.gov.cn/">©fjy8018.xin 粤ICP备18026008号 粤ICP备18026008号-1 </a>
</div> </div>
</el-footer> </el-footer>
</body> </body>

View File

@@ -32,6 +32,7 @@
<li>具备完整开发和维护文档,提供错误码、数据字典</li> <li>具备完整开发和维护文档,提供错误码、数据字典</li>
<li>URL为RESTful API风格便于开发和维护</li> <li>URL为RESTful API风格便于开发和维护</li>
<li>异常统一管理</li> <li>异常统一管理</li>
<li><a href="https://blog.fjy8018.top/index.php/archives/100/">更多</a></li>
</ul> </ul>
<br> <br>
<ul> <ul>