需要:添加 proxy_set_header x-real-ip $remote_addr;
upstream tomcat_server { server localhost:8080 weight=1; } server { listen 80; server_name 192.168.40.116; #charset koi8-r; #access_log logs/host.access.log main; location ~* \.(jsp|do)$ { #当请求的是jsp或do文件时直接到tomcat上去取 #root html; #index index.html index.htm; #proxy_pass http://192.168.18.201/; proxy_pass http://tomcat_server; client_max_body_size 2048m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 6000; proxy_buffer_size 16k; proxy_buffers 64k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } location / { #发布目录/data/www # root /home/ding/itsoftware/tomcat/webapps/100mshcloud; # fastcgi_pass 192.168.40.116:8080; proxy_set_header x-real-ip $remote_addr; proxy_pass http://tomcat_server/; #注释默认两行,新增一行。 # index index.html index.htm; }
tomcat 获取:
public static string getremoteaddrip(httpservletrequest request) { string ipfromnginx = getheader(request, x-real-ip); system.out.println(ipfromnginx: + ipfromnginx); system.out.println(getremoteaddr: + request.getremoteaddr()); return stringutil.isempty(ipfromnginx) ? request.getremoteaddr() : ipfromnginx; } private static string getheader(httpservletrequest request, string headname) { string value = request.getheader(headname); return !stringutils.isblank(value) && !unknown.equalsignorecase(value) ? value : ; }
以上就介绍了tomcat 获取nginx的真实用户ip,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。