<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>newtypebao</title>
    <description>欢迎来到我的个人站~</description>
    <link>https://newtypebao.github.io/</link>
    <atom:link href="https://newtypebao.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 23 Sep 2024 06:38:52 +0000</pubDate>
    <lastBuildDate>Mon, 23 Sep 2024 06:38:52 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>基于tomcat服务器的CPU使用率，重启tomcat</title>
        <description>&lt;p&gt;基于tomcat部署的javaweb项目中可能会遇到下面这个场景：由于应用功能里有个大报表、大查询又或者别的复杂的逻辑，把某一个tomcat所在服务器的cpu给搞满了。此时这个tomcat上的用户都巨卡。
原则上遇到上述情况应当排查具体的程序或者接口是不是有逻辑上的优化方法。以下是退一万步而言的终极方案，一个基于tomcat服务器的CPU使用率，重启tomcat。可以结合操作系统的定时任务执行，比如每5分钟扫描一次。如果命中规则(cpu&amp;gt;90%)就重启这个tomcat.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;	#!/bin/bash
	#需要在服务器内安装一下两个组件1.sysstat   2.bc

	# 定义toncat目录
	tomcatPath=/usr/local/tomcat
	# 定义日志文件
	log_file=&quot;/tmp/tomcat_thread_monitor.log&quot;


	# 获取Tomcat进程的主进程ID
	tomcat_pid=$(pgrep -f catalina)

	# 检查是否找到了Tomcat进程
	if [ -z &quot;$tomcat_pid&quot; ]; then
		echo &quot;Tomcat process not found.&quot;
		exit 1
	fi

	# 获取当前cpu使用率
	cpu_usage=$(top -bn1 | grep &quot;Cpu(s)&quot; | awk &apos;{print $2 + $4}&apos;)

	# 获取当前日期和时间
	current_date=$(date)

	# 记录开始检查的时间
	echo &quot;Tomcat usage at $current_date is $cpu_usage&quot;

	# 检查CPU使用率是否超过90%
	if (( $(echo &quot;$cpu_usage &amp;gt; 90&quot; | bc -l) )); then
		echo &quot;CPU usage is above 90% ($cpu_usage%). Restarting Tomcat...&quot;
		echo &quot;$current_date: CPU usage is above 90% ($cpu_usage%). Restarting Tomcat&quot;&amp;gt;&amp;gt; $log_file
		# 重启Tomcat服务
		sh &quot;$tomcatPath/bin/shutdown.sh&quot;
		sleep 10
		kill -9 $tomcat_pid
		sleep 5
		#启动tomcat
		sh &quot;$tomcatPath/bin/startup.sh&quot;
	else
		echo &quot;CPU usage is below 90% ($cpu_usage%). No action taken.&quot;
	fi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Mon, 23 Sep 2024 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2024/09/tomcat_cpu_monitor/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2024/09/tomcat_cpu_monitor/</guid>
        
        <category>Tomcat</category>
        
        
      </item>
    
      <item>
        <title>Spring通过RestTemplate使用POST方式请求指定接口</title>
        <description>&lt;p&gt;java开发功能中会有需要发起请求调用外部的系统。系统运维过程中，有大批量数据需要调用指定的接口修改数据，java代码样例如下：&lt;/p&gt;

&lt;p&gt;使用POST方式调用；&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;	public static void main(String[] args) {
		RestTemplate restTemplate = new RestTemplate();
		//  解决中文乱码
		restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
		// 构建请求头
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setBearerAuth(&quot;tokenString&quot;);
		requestHeaders.setContentType(MediaType.APPLICATION_JSON);
		// 入参为json体
		String reqJsonStr = &quot;{\&quot;code\&quot;:\&quot;testCode\&quot;, \&quot;group\&quot;:\&quot;testGroup\&quot;,\&quot;content\&quot;:\&quot;testContent\&quot;, \&quot;order\&quot;:1}&quot;;
		HttpEntity&amp;lt;String&amp;gt; requestEntity = new HttpEntity&amp;lt;&amp;gt;(null, requestHeaders);
		// 发起请求
		ResponseEntity&amp;lt;String&amp;gt; json =restTemplate.exchange(&quot;https://xxx1/yyy1/zzz1/postmethod?userId=12345&quot;, HttpMethod.GET, requestEntity,String.class);
		System.out.println(json);
	}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用GET方式调用：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;	public static void main(String[] args) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setBearerAuth(&quot;tokenString&quot;);
		HttpEntity&amp;lt;String&amp;gt; requestEntity = new HttpEntity&amp;lt;&amp;gt;(null, requestHeaders);
		ResponseEntity&amp;lt;String&amp;gt; json =restTemplate.exchange(&quot;https://xxx2/yyy2/zzz2/gettmethod?userId=12345&quot;, HttpMethod.GET, requestEntity,String.class);
	}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Wed, 19 Jan 2022 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2022/01/ManualCall/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2022/01/ManualCall/</guid>
        
        <category>java,Spring,RestTemplate</category>
        
        
      </item>
    
      <item>
        <title>Windows下bat脚本判断(tomcat)端口是否可用</title>
        <description>&lt;p&gt;考虑到目前项目组所使用的windows server服务器上部署的tomcat总是不定期的自动关闭，经过讨论初步准备的解决方案是希望有个脚本能定时扫描系统中tomcat是否开启，如果未开启，则自动启动tomcat.&lt;/p&gt;

&lt;p&gt;脚本如下：&lt;/p&gt;

&lt;p&gt;注：以上脚本中需要将tomcat的目录设置到windows的环境变量中（CATALINA_HOME）&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@echo off
echo.Check that the tomcat port is open
set num=8080
for /f &quot;tokens=3 delims=: &quot; %%a in (&apos;netstat -an&apos;) do (
echo.&quot;%%a&quot;
if &quot;%%a&quot;==&quot;%num%&quot; goto en
)
if not &quot;%%a&quot;==&quot;%num%&quot; goto en1
:en
echo.Checking For local port %NUM% is Opening...
exit
:en1
echo.Checking For local port %NUM% is Not Opening...
echo.Starting Tomcat Program...
call &quot;%CATALINA_HOME%&quot;\bin\startup.bat
echo.Starting Succes!
echo.OK!
exit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;主要命令说明：&lt;/p&gt;

&lt;p&gt;for /f “tokens=3 delims=: “ %%a in (‘netstat -an’)  ：&lt;/p&gt;

&lt;p&gt;根据netstat -an命令的返回结果进行文本的切割，切割的关键字是冒号和空格。将文本中的每行内容根据关键字切割成多列，并且只使用第三列，即端口号信息。通过循环比对目前系统使用中的端口号，监测8080端口是否开启。未开启的话就执行tomcat的启动程序。&lt;/p&gt;

&lt;p&gt;然后，将上面的脚本设置为windows的计划任务，根据项目组的实际情况，设定计划任务执行的时间间隔即可。&lt;/p&gt;
</description>
        <pubDate>Thu, 23 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/08/windowscheckport/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/08/windowscheckport/</guid>
        
        <category>Windows,bat</category>
        
        
      </item>
    
      <item>
        <title>nginx之iphash算法</title>
        <description>&lt;p&gt;今日客户生产环境中发现了个奇怪的现象：nginx的upstream中部署了10多台后端服务器，但是在实际的监控中发现用户的请求只负载到了其中3-4台服务器。虽然后来已经证实是由于nginx前还有部署WAF，导致了获取到的ip非用户的真实ip，从而导致了负载的混乱。&lt;/p&gt;

&lt;p&gt;问题发生期间，客户it方面经过讨论，原先提了一个这样的一个解决方案：他们认为现在不是配置了10来台，现在实际只负载到3-4台上吗？那么我们直接把剩下没有负载到的后端服务器从负载清单里去除掉，然后把剩下被负载到的几台服务器进行配置升级（用的阿里云，可以弹性扩展）。这样真的可以解决问题吗？我们一般会想到iphash如果后台服务器的配置总数发生了变化，那么其转发的规则也会自然发生变化，也就是说后端配置了10台服务器负载了到3台上，那么把配置修改为3台的话，很可能就转发到1-2台上了。我们来看下nginx iphash的算法，是否可以验证上面的说法。&lt;/p&gt;

&lt;p&gt;直接看代码：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;178&lt;/span&gt;	    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;179&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ngx_uint_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;181&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;113&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6271&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;182&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;183&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;184&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;185&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;186&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;187&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;188&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;189&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;190&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;191&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;192&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;193&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;194&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uintptr_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;195&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uintptr_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uintptr_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;196&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;197&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tried&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;198&lt;/span&gt;	            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;199&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;201&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;ngx_log_debug2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NGX_LOG_DEBUG_HTTP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;202&lt;/span&gt;	                       &lt;span class=&quot;s&quot;&gt;&quot;get ip hash peer, hash: %ui %04XL&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;203&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;204&lt;/span&gt;	        &lt;span class=&quot;n&quot;&gt;ngx_http_upstream_rr_peer_lock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;205&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;206&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;down&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;207&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;ngx_http_upstream_rr_peer_unlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;208&lt;/span&gt;	            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;209&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;210&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;211&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_fails&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;212&lt;/span&gt;	            &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fails&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_fails&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;213&lt;/span&gt;	            &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail_timeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;214&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;215&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;ngx_http_upstream_rr_peer_unlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;216&lt;/span&gt;	            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;217&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;218&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;219&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_conns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_conns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;220&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;ngx_http_upstream_rr_peer_unlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;221&lt;/span&gt;	            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;222&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;223&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;224&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;225&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;226&lt;/span&gt;	    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;227&lt;/span&gt;	
&lt;span class=&quot;mi&quot;&gt;228&lt;/span&gt;	        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;229&lt;/span&gt;	            &lt;span class=&quot;n&quot;&gt;ngx_http_upstream_rr_peers_unlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;230&lt;/span&gt;	            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rr_peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iphp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rrp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;231&lt;/span&gt;	        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;232&lt;/span&gt;	    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;主要代码在180-184的几行。&lt;/p&gt;

&lt;p&gt;1、for循环 i取 012三个值，而ip的点分十进制表示方法将ip分成四段（如：192.168.1.1），但是这里循环时只是将ip的前三个端作为参数加入hash函数。这样做的目的是保证ip地址前三位相同的用户经过hash计算将分配到相同的后端server。&lt;/p&gt;

&lt;p&gt;作者的这个考虑是极为可取的，因此ip地址前三位相同通常意味着来着同一个局域网或者相邻区域，使用相同的后端服务让nginx在一定程度上更具有一致性。&lt;/p&gt;

&lt;p&gt;2、哈希函数：hash = (hash * 113 + iphp-&amp;gt;addr[i]) % 6271&lt;/p&gt;

&lt;p&gt;作者使用了这样一个极为简单的hash函数，当然目的是为了性能。虽然该hash函数的效果并不是很好，产生的的分布并不是太均衡。但这在nginx选择后端server这样的应用场景已经足够，关键是其简单性。&lt;/p&gt;

&lt;p&gt;3、iphash后如何决定分配在哪台服务器上？
从184行的代码w = hash % iphp-&amp;gt;rrp.peers-&amp;gt;total_weight;可以看到：把ip地址hash以后，立刻就针对当前总共的服务器个数取模。那么也就初步证实了后端服务器的总数是会直接影响到负载的规则，证实了之前的设想。故解决之前的问题，最正确合理的做法，还是需要通过nginx的realip模块，把WAF的ip过滤掉才是正确且合理的做法。&lt;/p&gt;
</description>
        <pubDate>Sat, 28 Jul 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/07/nginx_ip_hash_arithmetic/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/07/nginx_ip_hash_arithmetic/</guid>
        
        <category>nginx</category>
        
        
      </item>
    
      <item>
        <title>【转】《王者荣耀》各职业铭文搭配概要</title>
        <description>&lt;p&gt;王者中有6种职业，不同职业对于铭文的需求不同，在30种铭文中，比较常用的也就10余种。对于铭文的使用，下面就概括性的按英雄类型给大家分析一波。&lt;/p&gt;

&lt;h4 id=&quot;1战士&quot;&gt;1、战士&lt;/h4&gt;
&lt;p&gt;战士铭文比较单一，要么狩猎百穿，要么隐匿百穿，完全不需要攻速的用隐匿百穿，其它就带狩猎百穿。当然，战士中也有比较特殊的，如老夫子、曹操需要带26攻速，宫本、铠可以走暴击流，吕布带肉铭文，还有一些法战，带的当然是法术铭文。不过大多数的战士，还是百穿这一套。&lt;/p&gt;

&lt;h4 id=&quot;2坦克&quot;&gt;2、坦克&lt;/h4&gt;
&lt;p&gt;坦克的铭文大致可以分两类，一种是打伤害的坦克，带一手隐匿百穿，能打出不俗的伤害，白起、项羽、钟无艳，甚至牛魔都可以用。还有一种是纯肉坦克，带的是调和宿命虚空，这套铭文真的是贼肉，相当于出门多了一件大防御装。当然，还有少量的法伤坦克，比如庄周，要么带贪婪怜悯梦魇打输出，要么带调和虚空宿命堆肉度。&lt;/p&gt;

&lt;h4 id=&quot;3辅助&quot;&gt;3、辅助&lt;/h4&gt;
&lt;p&gt;辅助可以分为两类，一是法伤辅助，二是物伤辅助。法伤辅助推荐带调和、怜悯、梦魇这一套，因为法伤辅助多为功能性辅助，对于冷却缩减的需求更高，比如孙膑、鬼谷子、大乔等等。物伤辅助只要堆肉就可以了，调和虚空宿命这一套就行。&lt;/p&gt;

&lt;h4 id=&quot;4刺客&quot;&gt;4、刺客&lt;/h4&gt;
&lt;p&gt;刺客对于铭文的分歧主要在红色铭文上，一种是异变党，一种是无双党，打技能伤害的就用异变，比如李白、娜可露露、兰陵王等等，打普攻伤害的就带无双，如韩信、百里玄策这些。至于蓝色铭文，要么吸血的夺萃，要么狩猎，这个不分英雄，按自己的习惯带，两种混搭也可以。&lt;/p&gt;

&lt;h4 id=&quot;5法师&quot;&gt;5、法师&lt;/h4&gt;
&lt;p&gt;大部分的法师，可以使用这两套铭文：（轮回献祭梦魇）（轮回心眼梦魇），这两套铭文可以解决大多数法师铭文的使用。还有一些特殊法师，如需要攻速的法师，那就从狩猎、凶兆、红月中选择使用；或者是需要续航的中路法师，蓝色铭文带一手调和没毛病，比如诸葛亮、高渐离等等。&lt;/p&gt;

&lt;h4 id=&quot;6射手&quot;&gt;6、射手&lt;/h4&gt;
&lt;p&gt;对于射手这个职业，两种铭文搭配基本可以解决，狩猎鹰眼无双，这套铭文适配大部分射手。李元芳、虞姬、百里守约这三个射手用狩猎鹰眼异变。当然，射手打野又另一说了，蓝色铭文在夺萃和狩猎之间选择。&lt;/p&gt;

&lt;p&gt;铭文是死的，玩家是活的，没有哪个英雄的铭文搭配是固定不变的，还是需要根据玩家的习惯喜好来使用，就比如刘备打野，有些玩家就喜欢带狩猎，跑得快；也有些玩家就喜欢带夺萃，吸的快；还有些玩家两样都带，比较均衡。一个英雄玩时间长了，知道自己更喜欢什么风格，才能搭配出对自己来说最好的铭文。&lt;/p&gt;
</description>
        <pubDate>Thu, 26 Jul 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/07/allprofessionconf/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/07/allprofessionconf/</guid>
        
        <category>王者荣耀</category>
        
        
      </item>
    
      <item>
        <title>【转】https下HttpServletResponse_sendRedirect跳转到http的解决方法</title>
        <description>&lt;h3 id=&quot;问题描述&quot;&gt;问题描述&lt;/h3&gt;
&lt;p&gt;最近全站上线SSL，架构如下：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;                 |
                 |(https)
                 |
                SLB
                 |
          WebServer (Nginx)                外网   
         /       |      \
       /         |       \                    内网
     /(http)     |(http)  \(http)
   Container1  Container2   Container3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;近日项目中使用了阿里的SLB,在外网使用SSL，然后使用反向代理到具体的Web Container，内网依然使用HTTP进行传输，这样可以节省一些资源，效率也比较高。不过这样做的时候发现所有的 HttpServletResponse.sendRedirect 方法使用相对地址的时候都会跳转到80端口，而不是443，经过查找了一些网上的资源，综合发现下面的几种解决方案。目前项目中暂时使用第一个解决方案（绝对路径），供有需要的同学们参考：&lt;/p&gt;

&lt;h3 id=&quot;解决方案&quot;&gt;解决方案&lt;/h3&gt;
&lt;p&gt;跳转的时候url使用绝对地址，而不是相对地址
关键代码如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;String url = &quot;/login&quot;;
// 将相对地址转换成https的绝对地址
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse)resp;
String scheme = request.getScheme();
String serverName = request.getServerName();
int port = request.getServerPort();
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String queryString = request.getQueryString();

StringBuilder sbd = new StringBuilder();
// 强制使用https
sbd.append(&quot;https&quot;).append(&quot;://&quot;).append(serverName)
if(port != 80 &amp;amp;&amp;amp; port != 443) {
    sbd.append(&quot;:&quot;).append(port);
}
if(contextPath != null) {
    sbd.append(contextPath);
}
if(servletPath != null) {
    sbd.append(servletPath);
}
sbd.append(url);
if(queryString != null) {
    sbd.append(queryString);
}

// 绝对地址
response.sendRedirect(sbd.toString());

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;可以的参考Spring Security的 org.springframework.security.web.util.RedirectUrlBuilder 代码。&lt;/p&gt;

&lt;h3 id=&quot;创建一个强制转换的过滤器&quot;&gt;创建一个强制转换的过滤器&lt;/h3&gt;
&lt;p&gt;将上面的代码封装成一个ResponseWrapper，覆盖到sendRedirect方法中，然后在过滤器中进行调用，核心代码如下：
RedirectResponseWrapper.java&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class RedirectResponseWrapper extends HttpServletResponseWrapper {

    private final HttpServletRequest request;

    public RedirectResponseWrapper(final HttpServletRequest inRequest,
            final HttpServletResponse response) {
        super(response);
        this.request = inRequest;
    }

    @Override
    public void sendRedirect(final String pLocation) throws IOException {
        if (StringUtils.isBlank(pLocation)) {
            super.sendRedirect(pLocation);
            return;
        }

        try {
            final URI uri = new URI(pLocation);
            if (uri.getScheme() != null) {
                super.sendRedirect(pLocation);
                return;
            }
        } catch (URISyntaxException ex) {
            super.sendRedirect(pLocation);
        }

        // !!! FIX Scheme !!!
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String queryString = request.getQueryString();

        StringBuilder sbd = new StringBuilder();
        // 强制使用https
        sbd.append(&quot;https&quot;).append(&quot;://&quot;).append(serverName)
        if(port != 80 &amp;amp;&amp;amp; port != 443) {
            sbd.append(&quot;:&quot;).append(port);
        }
        if(contextPath != null) {
            sbd.append(contextPath);
        }
        if(servletPath != null) {
            sbd.append(servletPath);
        }
        sbd.append(url);
        if(queryString != null) {
            sbd.append(queryString);
        }

        super.sendRedirect(sbd.toString());
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;创建一个过滤器，并在web.xml中启用&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class AbsoluteSendRedirectFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        RedirectResponseWrapper redirectResponseWrapper = new RedirectResponseWrapper(request, response);
        filterChain.doFilter(request, redirectResponseWrapper);
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;web.xml&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;filter&amp;gt;
    &amp;lt;filter-name&amp;gt;AbsoluteSendRedirectFilter&amp;lt;/filter-name&amp;gt;  
    &amp;lt;filter-class&amp;gt;com.rensanning.core.filter.AbsoluteSendRedirectFilter&amp;lt;/filter-class&amp;gt;
&amp;lt;/filter&amp;gt;
&amp;lt;filter-mapping&amp;gt;
    &amp;lt;filter-name&amp;gt;AbsoluteSendRedirectFilter&amp;lt;/filter-name&amp;gt;
    &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;
&amp;lt;/filter-mapping&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;将spring-mvc中viewresolver的redirecthttp10compatible属性设置为false&quot;&gt;将Spring MVC中viewResolver的redirectHttp10Compatible属性设置为false&lt;/h3&gt;
&lt;p&gt;如果使用Spring的话，可以将viewResolver的redirectHttp10Compatible属性设置为false，代码如下：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;bean id=&quot;viewResolver&quot; class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&amp;gt;  
  &amp;lt;property name=&quot;viewClass&quot; value=&quot;org.springframework.web.servlet.view.JstlView&quot; /&amp;gt;  
  &amp;lt;property name=&quot;prefix&quot; value=&quot;/&quot; /&amp;gt;  
  &amp;lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&amp;gt;  
  &amp;lt;property name=&quot;redirectHttp10Compatible&quot; value=&quot;false&quot; /&amp;gt;  
&amp;lt;/bean&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;在nginx中设置x-forwarded-proto&quot;&gt;在Nginx中设置X-Forwarded-Proto&lt;/h3&gt;
&lt;p&gt;上面的方法使用了一种强制的方式，但真正传输到后端容器中的依然是HTTP协议，Spring Security等使用 ServletRequest#isSecure() 方法判断是否是SSL环境，可以使用Nginx进行反向代理的时候，设置X-Forwarded-Proto，关键设置如下：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location / {
    proxy_next_upstream http_502 http_504 error timeout invalid_header;
    proxy_set_header Host  $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    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;
    proxy_pass http://tdt_server;
    proxy_redirect off;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;将Host由$host修改为$http_host，区别是后者包含端口号，而前者不包含。
还需要后端的容器能够识别，在Tomcat中可以使用 RemoteIpValve 进行设置：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Valve className=&quot;org.apache.catalina.valves.RemoteIpValve&quot;
    internalProxies=&quot;192\.168\.0\.10|192\.168\.0\.11&quot;
    remoteIpHeader=&quot;x-forwarded-for&quot;
    proxiesHeader=&quot;x-forwarded-by&quot;
    protocolHeader=&quot;x-forwarded-proto&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Resin4.0中可以在resin.xml中通过 resin:SetRequestSecure 设置：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;web-app xmlns=&quot;http://caucho.com/ns/resin&quot;
         xmlns:resin=&quot;urn:java:com.caucho.resin&quot;&amp;gt;
    &amp;lt;resin:SetRequestSecure&amp;gt;
        &amp;lt;resin:IfHeader name=&quot;X-Forwarded-Proto&quot; value=&quot;https&quot; /&amp;gt;
    &amp;lt;/resin:SetRequestSecure&amp;gt;
&amp;lt;/web-app&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;在Resin4.0中如果想针对特定的地址使用http，可以使用下面的设置：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;web-app xmlns=&quot;http://caucho.com/ns/resin&quot;
         xmlns:resin=&quot;urn:java:com.caucho.resin&quot;&amp;gt;
    &amp;lt;resin:Redirect regexp=&quot;^/yyy/&quot; target=&quot;http://myhost.com/yyy/&quot;&amp;gt;
        &amp;lt;resin:IfSecure value=&quot;true&quot;/&amp;gt;
    &amp;lt;/resin:Redirect&amp;gt;
&amp;lt;/web-app&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;在nginx中使用rewrite&quot;&gt;在Nginx中使用rewrite&lt;/h3&gt;
&lt;p&gt;还有一种简单的方式，直接在Nginx中将所有80的请求自动设置成301跳转，设置如下：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;server {
    listen 80;
    server_name example.com;

    location / {
        rewrite ^(.*) https://$server_name$1 permanent;
    }
}
server {
    listen 443;
    server_name example.com;

    ssl on;
    ssl_certificate   cert.pem;
    ssl_certificate_key  cert.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host  $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        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;
        proxy_pass http://tdt_server;
        proxy_redirect off;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;经过这样设置，遇到跳转的时候等于需要跳转两次，先是从https跳转到http，然后因为nginx设置的原因，又从http跳转到https，虽然解决方法粗暴了一些，但是能够忽略后端不同的Java Web Container的差异。&lt;/p&gt;
</description>
        <pubDate>Thu, 19 Jul 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/07/SendRedirect_in_https/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/07/SendRedirect_in_https/</guid>
        
        <category>Servlet</category>
        
        
      </item>
    
      <item>
        <title>《我的前半生》--职场经典语录</title>
        <description>&lt;h4 id=&quot;新人篇&quot;&gt;新人篇&lt;/h4&gt;

&lt;p&gt;第1句。谁找工作时不是抱着百分百真诚的态度，但是态度有用的话，找不到工作的人就不会那么多了。经验和技能才是你的立足根本，在没有经验的时候，不要挑挑拣拣，先工作起来才是重要的。&lt;/p&gt;

&lt;p&gt;第2句。一旦你开始工作了，这就相当于是开启了闯关游戏的大门。你要去找到你的同伴。同伴你可以信任他，但是不能依赖他。&lt;/p&gt;

&lt;p&gt;第3句。我觉得吃饭的时间，也要有效利用起来做些什么和学点什么。一天3个小时，一周下来就比别人多出一天时间。&lt;/p&gt;

&lt;h4 id=&quot;交际篇&quot;&gt;交际篇&lt;/h4&gt;

&lt;p&gt;第4句。你来工作是来赚钱的，不是来交朋友的。如果能交到朋友那是惊喜，交不到朋友那才是正常的。&lt;/p&gt;

&lt;p&gt;第5句。不要去做客户没有要求你去做的事情，不要给客户免费的午餐，你是计时收费的，而且还不便宜。&lt;/p&gt;

&lt;p&gt;第6句。对待客户有有张有弛，要去体会他们，为他们考虑，但是要恰到好处的考虑。&lt;/p&gt;

&lt;h4 id=&quot;提升篇&quot;&gt;提升篇&lt;/h4&gt;

&lt;p&gt;第7句。说过的话，就不能收回，不能让步，否则你就是一个可以被讨价还价的人，后患无穷。&lt;/p&gt;

&lt;p&gt;第8句。你要知道你的上级是谁，并且要知道他们的爱好，嗜好和兴趣，因为每一个人，最喜欢的东西，往往是他的弱点所在，也是最容易被利用的突破口。&lt;/p&gt;

&lt;p&gt;第9句。实战之前，必须预演，以排除一切意外的可能。没有人会在乎你所谓的特殊情况，更没有人有心情，有时间去听你解释。&lt;/p&gt;

&lt;h4 id=&quot;终极篇&quot;&gt;终极篇&lt;/h4&gt;

&lt;p&gt;第10句。职场上你一定要做到可以取代任何人，然后再考虑做到任何人都不可以取代你。&lt;/p&gt;

&lt;p&gt;身处职场的你，最害怕的就是失去竞争力，不被同事看好，也不被领导看中，一辈子浑浑噩噩，终无成就。&lt;/p&gt;

&lt;p&gt;职场如人生，想要过好职场这一生，就要努力学习提升职场技能，这才是叫嚣的资本。&lt;/p&gt;

&lt;p&gt;路要自己一步步走，苦要自己一口口吃，只有经历痛苦我们才能明白成功的定义！&lt;/p&gt;
</description>
        <pubDate>Fri, 13 Jul 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/07/Workplace_sayings/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/07/Workplace_sayings/</guid>
        
        <category>sayings</category>
        
        
      </item>
    
      <item>
        <title>Apache Tomcat Versions</title>
        <description>&lt;p&gt;Apache Tomcat® is an open source software implementation of the Java Servlet and JavaServer Pages technologies. Different versions of Apache Tomcat are available for different versions of the Servlet and JSP specifications. The mapping between the specifications and the respective Apache Tomcat versions is:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Servlet Spec&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;JSP Spec&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;EL Spec&lt;/th&gt;
      &lt;th&gt;WebSocket Spec&lt;/th&gt;
      &lt;th&gt;JASPIC Spec&lt;/th&gt;
      &lt;th&gt;Apache Tomcat Version&lt;/th&gt;
      &lt;th&gt;Latest Released Version&lt;/th&gt;
      &lt;th&gt;Supported Java Versions&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;4.0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.3&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;3.0&lt;/td&gt;
      &lt;td&gt;1.1&lt;/td&gt;
      &lt;td&gt;1.1&lt;/td&gt;
      &lt;td&gt;9.0.x&lt;/td&gt;
      &lt;td&gt;9.0.10&lt;/td&gt;
      &lt;td&gt;8 and later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3.1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.3&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;3.0&lt;/td&gt;
      &lt;td&gt;1.1&lt;/td&gt;
      &lt;td&gt;1.1&lt;/td&gt;
      &lt;td&gt;8.5.x&lt;/td&gt;
      &lt;td&gt;8.5.32&lt;/td&gt;
      &lt;td&gt;7 and later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3.1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.3&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;3.0&lt;/td&gt;
      &lt;td&gt;1.1&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;8.0.x (superseded)&lt;/td&gt;
      &lt;td&gt;8.0.53 (superseded)&lt;/td&gt;
      &lt;td&gt;7 and later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3.0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2.2&lt;/td&gt;
      &lt;td&gt;1.1&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;7.0.x&lt;/td&gt;
      &lt;td&gt;7.0.90&lt;/td&gt;
      &lt;td&gt;6 and later (7 and later for WebSocket)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2.5&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2.1&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;6.0.x (archived)&lt;/td&gt;
      &lt;td&gt;6.0.53 (archived)&lt;/td&gt;
      &lt;td&gt;5 and later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2.4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;5.5.x (archived)&lt;/td&gt;
      &lt;td&gt;5.5.36 (archived)&lt;/td&gt;
      &lt;td&gt;1.4 and later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2.3&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1.2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;4.1.x (archived)&lt;/td&gt;
      &lt;td&gt;4.1.40 (archived)&lt;/td&gt;
      &lt;td&gt;1.3 and later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2.2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1.1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;N/A&lt;/td&gt;
      &lt;td&gt;3.3.x (archived)&lt;/td&gt;
      &lt;td&gt;3.3.2 (archived)&lt;/td&gt;
      &lt;td&gt;1.1 and later&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Each version of Tomcat is supported for any stable Java release that meets the requirements of the final column in the table above.&lt;/p&gt;

&lt;p&gt;reference:&lt;a href=&quot;http://tomcat.apache.org/whichversion.html&quot;&gt;http://tomcat.apache.org/whichversion.html&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 13 Jul 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/07/Tomcat,Jstl_Servlet-api_jsp-api_Versions/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/07/Tomcat,Jstl_Servlet-api_jsp-api_Versions/</guid>
        
        <category>Tomcat</category>
        
        
      </item>
    
      <item>
        <title>关于通过jcifs无法登陆centos7中SMB服务的问题</title>
        <description>&lt;p&gt;最近客户需要将自己部署在IDC机房的系统迁移到阿里云上，由于之前的系统是前几年开发的，当时使用 是Centos6系统，考虑到系统中主要使用到的是jdk\java\smb这些常用的服务，而这些服务在最新的CentOS中运行的还是比较稳定的，因此本次迁移到阿里云上采用的是Centos7。&lt;/p&gt;

&lt;p&gt;在系统迁移实施过程中，大部分进行的还是比较顺利的，比如原来用的是tomcat6+jdk1.6(这两个版本到各自的官网上都已经找不到了。。-_-!),从原服务器上搬到centos7上运行都还算正常。&lt;/p&gt;

&lt;p&gt;只有应用中出现的一个问题比较奇怪：
由于程序没有做过任何变动，部署到新环境中涉及到通过smb向远程文件服务器写文件的时候，总是报用户名和密码不对,像下面这个报错：&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Caused by: jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
        at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:515)
        at jcifs.smb.SmbTransport.send(SmbTransport.java:629)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;这个比较奇怪，期初我怀疑这个是我建的smb服务用户名密码和应用设置的不匹配，但是通过再三比对，确定使用的smb用户名和密码是没问题的。但这个错误依旧是那么明确unknown user name or bad password。。。&lt;/p&gt;

&lt;p&gt;通过搜索了网上资源，目前是在tomcat中调整了jvm的可选参数： &lt;em&gt;jcifs.smb.lmCompatibility=3&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;重新测试后，表现正常。其原因，目前推测还是centos6和centos7中的smb服务的版本不一致导致，具体还没了解清楚。&lt;/p&gt;

&lt;p&gt;之前baidu上搜索了很久才在一些英语资料上找到的解决方法，特此总结，以供有相似经历的人分享。&lt;/p&gt;
</description>
        <pubDate>Thu, 05 Jul 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/07/jcifs_Logon-failure_on_centos7/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/07/jcifs_Logon-failure_on_centos7/</guid>
        
        <category>smb</category>
        
        
      </item>
    
      <item>
        <title>关于nginx中iphash不生效的解决方案</title>
        <description>&lt;p&gt;最近客户需要将自己部署在阿里云上的系统从可用区A迁移到可用区E,所有服务器人工重新搭建（不愿意走镜像-_-!)，系统总体的结构是:SLB-&amp;gt;WEB SERVER(nginx)-&amp;gt;APP SERVER(TOMCAT),项目实施过程中配置nginx时遇到一个很奇怪的问题：&lt;/p&gt;

&lt;p&gt;外网访问者每次从外网SLB访问系统时，总是会路由到某2个固定的后端ECS上（后端总共有十多台ECS)。
这个比较奇怪，因为理论上根据配置，nginx会通过iphash的规则，根据不同的外网ip转发到后端服务器。然后后台总共有10多台服务器，用了100个外网地址测试，还是固定的那么2个后端ECS，感觉明显是有问题的。然后，开始了排查之旅。。&lt;/p&gt;

&lt;p&gt;首先先从阿里角度排查，怀疑是否有可能是SLB里需要什么特别的配置，比如没有把实际的客户端IP放进来？查阅了阿里的资料发现如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;四层负载均衡（TCP协议）服务可以直接在后端ECS上获取客户端的真实IP地址，无需进行额外的配置。&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;七层负载均衡（HTTP/HTTPS协议）服务需要对应用服务器进行配置，然后使用X-Forwarded-For的方式获取客户端的真实IP地址。&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;真实的客户端IP会被负载均衡放在HTTP头部的X-Forwareded-For字段，格式如下：&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X-Forwarded-For: 用户真实IP, 代理服务器1-IP， 代理服务器2-IP，...&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;当使用此方式获取客户端真实IP时，获取的第一个地址就是客户端真实IP。&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;由于原SLB是四层的（TCP协议），新环境的SLb是七层的（HTTPS协议），客户要用7层的，那么我尝试在nginx配置中告知Nginx真实客户端IP从哪个请求头获取，在server中增加配置：&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;real_ip_header X-Forwarded-For;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;重新测试后，发现效果还是老样子，无改善。继续。。发现nginx access日志中大量的访问ip大都是在一个地址段，100.x.x.x,继续查阅阿里资料，了解到这些访问的来源都是由负载均衡系统发起的，地址段为100.64.0.0/10。因此，继续在server中增加配置：&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set_real_ip_from 100.64.0.0/10;&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;real_ip_recursive on; &lt;/code&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;顺便说明一下这两个配置的作用：&lt;br /&gt;
set_real_ip_from192.168.0.0/16：告知Nginx哪些是反向代理IP，即排除后剩下的就是真实客户端IP，支持配置具体IP地址、CIDR地址； &lt;br /&gt;
real_ip_recursive on：是否递归解析，当real_ip_recursive配置为off时，Nginx会把real_ip_header指定的请求头中的最后一个IP作为真实客户端IP；当real_ip_recursive配置为on时，Nginx会递归解析real_ip_header指定的请求头，最后一个不匹配set_real_ip_from的IP作为真实客户端IP。&lt;/p&gt;

&lt;p&gt;重新测试后，表现正常。&lt;/p&gt;

&lt;p&gt;之前baidu上搜索都无法直接定位到这个情况以及原因，特此总结，以供有相似经历的人分享。&lt;/p&gt;
</description>
        <pubDate>Tue, 19 Jun 2018 00:00:00 +0000</pubDate>
        <link>https://newtypebao.github.io/2018/06/nginx_iphash_doesnt_work/</link>
        <guid isPermaLink="true">https://newtypebao.github.io/2018/06/nginx_iphash_doesnt_work/</guid>
        
        <category>nginx</category>
        
        
      </item>
    
  </channel>
</rss>
