nginx代理tomcat后,tomcat获取服务端ip端口的解决方案
1.注意修改nginx配置代理,标红地方
#user nginx;
worker_processes ; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; gzip on;
gzip_disable "MSIE [1-6]."; client_header_buffer_size 128k;
large_client_header_buffers 128k;
############你自己的项目#######
upstream dj-tomcatwebapp {
# sticky;
server 127.0.0.1:;
}
###### 你自己的项目system#######
server {
listen ;
server_name 127.0.0.1; charset utf-8; #access_log logs/host.access.log main; location / {
client_max_body_size 1000m;
proxy_pass http://dj-tomcatwebapp;
proxy_redirect off;
78 proxy_set_header Host $host:$server_port;
79 proxy_set_header X-Real-IP $remote_addr;
80 proxy_set_header X-Real-PORT $remote_port;
82 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; } }
}
2.jsp获取真实服务器 nginx访问 ip 端口
<%
String scheme = request.getScheme();
String serverName = request.getServerName();
int port = request.getServerPort();
String requestURI = scheme+"://"+serverName+":"+port;
%>
<c:set var="hyrt" value="<%=requestURI%>" />
以上经过实际本地测试,可以正常获取