<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>服务器安全维护工作室 &#187; ECS反向代理设置</title>
	<atom:link href="https://www.fuwuqiok.com/tag/ecs%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e8%ae%be%e7%bd%ae/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fuwuqiok.com</link>
	<description></description>
	<lastBuildDate>Sun, 01 Mar 2020 07:28:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.26</generator>
	<item>
		<title>ECS反向代理设置</title>
		<link>https://www.fuwuqiok.com/ecs%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e8%ae%be%e7%bd%ae/</link>
		<comments>https://www.fuwuqiok.com/ecs%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e8%ae%be%e7%bd%ae/#comments</comments>
		<pubDate>Tue, 01 Dec 2015 14:00:29 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[服务器迁移]]></category>
		<category><![CDATA[网站代维]]></category>
		<category><![CDATA[ECS反向代理设置]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=2779</guid>
		<description><![CDATA[<p>阿里云GPU物理机本身不能被外网直接访问，需要通过ECS反向代理。本文档将指导用户如何设置代理服务器。 1.  [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/ecs%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e8%ae%be%e7%bd%ae/">ECS反向代理设置</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>阿里云GPU物理机本身不能被外网直接访问，需要通过ECS反向代理。本文档将指导用户如何设置代理服务器。</p>
<h2 id="1-ip-">1. 确定IP地址</h2>
<p>用户应首先确认这几个IP地址： ECS外网IP（不便于透露，本文用XXX.XXX.XXX.XXX表示）和内网IP（实验用10.10.10.10）； GPU物理机内网IP（实验用10.239.23.4）；</p>
<h2 id="2-ecs-">2. 登录ECS跳板机</h2>
<p>用户可以用PUTTY工具（Windows环境）或SSH命令（Linux环境）登录ECS，注意应使用ECS外网IP登入。</p>
<pre><code class="lang-sh">ssh -l login_name XXX.XXX.XXX.XXX（ECS外网IP）
</code></pre>
<p>登录成功后，可以在ECS跳板机上用SSH命令登录GPU物理机：</p>
<pre><code class="lang-sh">ssh -l root 10.239.23.4（GPU物理机内网IP）
</code></pre>
<h2 id="3-ecs-">3. ECS跳板机上部署代理服务器</h2>
<p>这里选择Tengine，它是在NGINX的基础上由淘宝网发起的开源Web服务器项目。用户应注意，NGINX做前向代理服务器是不支持HTTPS连接的，所以客户端只能访问HTTP服务。</p>
<h3 id="3-1-tengine">3.1 安装Tengine</h3>
<p>重新开一个终端，登录到ECS跳板机。 获取Tengine源码：</p>
<pre><code class="lang-sh">wget http://tengine.taobao.org/download/tengine-2.1.1.tar.gz
</code></pre>
<p>解压：</p>
<pre><code class="lang-sh">tar zxvf tengine-2.1.1.tar.gz
cd tengine-2.1.1/
</code></pre>
<p>配置和编译：</p>
<pre><code class="lang-sh">./configure
make
sudo make install
</code></pre>
<p>默认情况下安装位置在 <code>/usr/local/nginx/</code> 出于测试目的，我们需要在GPU物理机上也安装Tengine，步骤与ECS上安装基本一致，安装路径也在/usr/local/nginx/。</p>
<h3 id="3-2-ecs-tengine-">3.2 编辑ECS Tengine配置文件</h3>
<p>登录ECS跳板机，用root权限打开 <code>/usr/local/nginx/conf/nginx.conf</code> 文件，增加一个server块，作用为监听本机的10000端口，将所有请求转发给GPU物理机（10.239.23.4:10000），配置内容如下：(&#8220;//”后面为注释，真正的conf文件中应删除）</p>
<pre><code class="lang-sh">server {
        listen       10000;   // 监听本机的10000端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://10.239.23.4:10000;
          // 10.239.23.4为本实验物理机内网IP，请根据需要修改
          // 这里实现了将来自外网的请求转发至物理机
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
</code></pre>
<p>保存该文件。</p>
<h3 id="3-3-gpu-tengine-">3.3 编辑GPU物理机Tengine配置文件和主页</h3>
<p>登录GPU物理机，用root权限编辑 <code>/usr/local/nginx/conf/nginx.conf</code>：</p>
<pre><code class="lang-sh">server {
        listen       10000;
          // 本文实验将默认的80改为10000，用户可根据需要修改
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        ..........略.............
    }
</code></pre>
<p>为了区分GPU物理机和ECS跳板机的主页内容，我们修改物理机的主页（ <code>/usr/local/nginx/html/index.html</code>）代码如下：</p>
<pre><code class="lang-sh">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to GPU tengine!&lt;/title&gt;
&lt;style&gt;
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to GPU tengine!&lt;/h1&gt;
&lt;p&gt;If you see this page, the tengine web server is successfully installed and
working. Further configuration is required.&lt;/p&gt;

&lt;p&gt;For online documentation and support please refer to
&lt;a href="http://tengine.taobao.org/"&gt;tengine.taobao.org&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for using tengine.&lt;/em&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>ECS上主页保持不变即可。</p>
<h3 id="3-4-gpu-tengine">3.4 启动GPU物理机上的Tengine</h3>
<p>登录到GPU物理机，用root权限运行：<code>sudo /usr/local/nginx/sbin/nginx</code> 如果报错，请根据报错信息对3.2节中的nginx.conf配置文件做必要的修改。 查看NGINX进程：</p>
<pre><code class="lang-sh"># ps -ef | grep nginx
root     17205     1  0 10:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   17206 17205  0 10:58 ?        00:00:00 nginx: worker process
root     20108 17042  0 11:54 pts/1    00:00:00 grep --color=auto nginx
</code></pre>
<p>可以看到NGINX主进程的pid为17205，worker进程的pid为17206，在需要关闭web server时可以直接以root权限运行：<code>kill master_pid</code></p>
<h3 id="3-5-gpu-">3.5 设置GPU物理机防火墙</h3>
<h4 id="3-5-1-gpu-">3.5.1 开启GPU物理机防火墙</h4>
<pre><code class="lang-sh">CentOS6: service iptables start
CentOS7: systemctl start firewalld
</code></pre>
<h4 id="3-5-2-">3.5.2 添加防火墙规则</h4>
<p>GPU物理机服务器需要使用10000端口，故添加防火墙例外：</p>
<pre><code class="lang-sh">iptables -I INPUT -p TCP --dport 10000 -j ACCEPT
</code></pre>
<h2 id="4-ecs-">4. 启动ECS反向代理服务</h2>
<p>回到ECS跳板机终端，启动代理服务。 在启动ECS跳板机上的Tengine之前，我们需要确定ECS跳板机能访问物理机上的web server。在ECS跳板机上运行： curl 物理机内网IP:端口号 这里物理机内网IP为10.239.23.4，端口号在3.2节配置为10000，因此命令如下：</p>
<pre><code class="lang-sh"># curl 10.239.23.4:10000
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to GPU tengine!&lt;/title&gt;
&lt;style&gt;
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to GPU tengine!&lt;/h1&gt;
&lt;p&gt;If you see this page, the tengine web server is successfully installed and
working. Further configuration is required.&lt;/p&gt;

&lt;p&gt;For online documentation and support please refer to
&lt;a href="http://tengine.taobao.org/"&gt;tengine.taobao.org&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for using tengine.&lt;/em&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>根据返回内容，对照3.3节的主页修改情况可以确认返回内容来自GPU物理机。 确认ECS跳板机可以访问HPC物理机上的web server之后，开启ECS跳板机上的Tengine：（root权限执行）<code>/usr/local/nginx/sbin/nginx</code> 如果启动时报错，请根据报错信息修改。 查看ECS跳板机上的Tengine进程：</p>
<pre><code class="lang-sh"># ps -ef | grep nginx
root     16487     1  0 11:33 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   16488 16487  0 11:33 ?        00:00:12 nginx: worker process
root     16565 16419  0 12:06 pts/8    00:00:00 grep nginx
</code></pre>
<p>可以看到相应主进程和worker进程的pid。注意不要与物理机上的pid混淆。</p>
<h2 id="5-">5. 测试</h2>
<h3 id="5-1-ecs-">5.1 ECS上本地回环测试</h3>
<p>可以在ECS跳板机上直接用curl进行本地回环测试：</p>
<pre><code class="lang-sh"># curl http://localhost:10000
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to GPU tengine!&lt;/title&gt;
&lt;style&gt;
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to GPU tengine!&lt;/h1&gt;
&lt;p&gt;If you see this page, the tengine web server is successfully installed and
working. Further configuration is required.&lt;/p&gt;

&lt;p&gt;For online documentation and support please refer to
&lt;a href="http://tengine.taobao.org/"&gt;tengine.taobao.org&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for using tengine.&lt;/em&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>对比3.3节的主页内容，可见返回的确实是GPU物理机上web server主页。</p>
<h3 id="5-2-web-">5.2 远程web页面测试</h3>
<p>在浏览器的地址栏输入：<a href="http://XXX.XXX.XXX.XXX:10000/">http://XXX.XXX.XXX.XXX:10000/</a> （这里XXX.XXX.XXX.XXX是ECS跳板机的公网IP），显示结果如下图所示：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2015/12/ECS_proxy_1.png"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2015/12/ECS_proxy_1.png" alt="ECS_proxy_1" width="738" height="335" /></a></p>
<p>可见，实现了远程访问HPC物理机上的web server，也验证了通过ECS进行反向代理的有效性。</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/ecs%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e8%ae%be%e7%bd%ae/">ECS反向代理设置</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/ecs%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e8%ae%be%e7%bd%ae/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
