nginx代理tomcat后,tomcat获取真实(非proxy,非别名)nginx服务端ip端口的解决方案
nginx代理tomcat后,tomcat获取服务端ip端口的解决方案
1.注意修改nginx配置代理,标红地方
1 #user nginx; 2 worker_processes 4; 3 4 error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; 13 } 14 15 16 http { 17 include mime.types; 18 default_type application/octet-stream; 19 20 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 '$status $body_bytes_sent "$http_referer" ' 22 '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 access_log logs/access.log main; 25 26 sendfile on; 27 #tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; 31 32 gzip on; 33 gzip_disable "MSIE [1-6]."; 34 35 client_header_buffer_size 128k; 36 large_client_header_buffers 4 128k; 37 ############你自己的项目####### 38 upstream dj-tomcatwebapp { 39 # sticky; 40 server 127.0.0.1:8085; 41 } 65 ###### 你自己的项目system####### 66 server { 67 listen 8095; 68 server_name 127.0.0.1; 70 charset utf-8; 71 72 #access_log logs/host.access.log main; 73 74 location / { 75 client_max_body_size 1000m; 76 proxy_pass http://dj-tomcatwebapp; 77 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; 86 87 88 proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; 89 90 } 91 92118 } 119 162204 }
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%>" />
以上经过实际本地测试,可以正常获取