<?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>How to Config &#187; Reverse Proxy</title>
	<atom:link href="http://www.howtoconfig.com/tag/reverse-proxy/feed" rel="self" type="application/rss+xml" />
	<link>http://www.howtoconfig.com</link>
	<description>how to config - how to configure</description>
	<lastBuildDate>Fri, 29 Apr 2011 09:52:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>แก้ปัญหา REMOTE_ADDR ใน Reverse Proxy</title>
		<link>http://www.howtoconfig.com/development/remote_addr-http_x_forwarded_for-reverse-proxy.html</link>
		<comments>http://www.howtoconfig.com/development/remote_addr-http_x_forwarded_for-reverse-proxy.html#comments</comments>
		<pubDate>Wed, 18 Feb 2009 17:58:37 +0000</pubDate>
		<dc:creator>อัทศักดิ์ วงศ์กิตตะ</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[Reverse Proxy]]></category>
		<category><![CDATA[Web Server]]></category>

		<guid isPermaLink="false">http://www.howtoconfig.com/?p=74</guid>
		<description><![CDATA[สวัสดีครับ วันนี้จิบเบียร์แล้วก็มานั้งทบทวนเรื่องงานที่ทำ  พอดีมีเรื่องเล็กๆน้่อยๆ ที่ผมเองทำและน่าจะเป็นประโยช์กับเพื่อนๆ  เกี่ยวกับการตั้ง Reverse Proxy กับปัญหา เรื่อง IP ที่ show ใน app ของเรา ปกติเวลาเราเขียน Apps หรือ พวกฟรี Webboard ทั้งหลายจะมีการ Check IP จาก $_SERVER['REMOTE_ADDR'] และ $_SERVER['HTTP_X_FORWARDED_FOR']  หากเจอตัวแปล HTTP_X_FORWARDED_FOR   ระบบจะทำการ Record  IP จาก  HTTP_X_FORWARDED_FOR ด้วย   แต่หากว่าบางโปรแกรมที่เขียน จะเก็บแค่   REMOTE_ADDR  ทำให้ IP ที่แสดงในโปรแกรมของเรา  เป็น IP ของ Reverse Proxy แทน Client IP:  203.146.1.1 Proxy IP: 192.168.1.100 Web IP: [...]]]></description>
			<content:encoded><![CDATA[<p>สวัสดีครับ วันนี้จิบเบียร์แล้วก็มานั้งทบทวนเรื่องงานที่ทำ  พอดีมีเรื่องเล็กๆน้่อยๆ ที่ผมเองทำและน่าจะเป็นประโยช์กับเพื่อนๆ  เกี่ยวกับการตั้ง Reverse Proxy กับปัญหา เรื่อง IP ที่ show ใน app ของเรา</p>
<p>ปกติเวลาเราเขียน Apps หรือ พวกฟรี Webboard ทั้งหลายจะมีการ Check IP จาก</p>
<p>$_SERVER['REMOTE_ADDR'] และ $_SERVER['HTTP_X_FORWARDED_FOR']  หากเจอตัวแปล HTTP_X_FORWARDED_FOR   ระบบจะทำการ Record  IP จาก  HTTP_X_FORWARDED_FOR ด้วย   แต่หากว่าบางโปรแกรมที่เขียน จะเก็บแค่   REMOTE_ADDR  ทำให้ IP ที่แสดงในโปรแกรมของเรา  เป็น IP ของ Reverse Proxy แทน<br />
<span id="more-74"></span></p>
<p>Client IP:  203.146.1.1<br />
Proxy IP: 192.168.1.100<br />
Web IP: 192.168.1.1</p>
<p>[Client] ===&gt; [Revert Proxy] ===&gt; [Web Server]</p>
<p>เมื่อ Web Server  ได้รับ Request   Header จะได้ดั่งนี้</p>
<p>$_SERVER['REMOTE_ADDR'] = 192.168.1.100<br />
$_SERVER['HTTP_X_FORWARDED_FOR']  = 203.146.1.1</p>
<p>ทางแก้ง่ายนิดเดียวครับ  แก้ที่ System ไม่ต้องไปขอร้องให้ Programmer แก้ไขโค๊ดแต่อย่างใด  ทางออกอยู่ที่ PHP ครับ    โดย PHP จะมี Parameter อยู่ 2 ตัวคือauto_prepend_file และ auto_append_file</p>
<p>auto_prepend_file จะกำหนดว่า ก่อนที่จะรันไฟล์ php ทุกๆครั้ง จะทำการรันไฟล์นี้ก่อน ส่วน auto_append_file  คือเมื่อทำการรันไฟล์ php ใดๆเสร็จแล้ว จะมารันไฟล์ ที่เรากำหนด      เมื่อเรารู้ดั่งนี้แล้ว  เ้ราก็มาดูโค๊ดแก้ปัญหาเรื่อง Rever Proxy &amp; REMOTE_IP  ได้เลยครับ</p>
<p>1.  ให้เราไปกำหนดที่  php.ini   ใส่พารามิเตอร์  auto_prepend_file &#8220;/etc/php-prepend.php&#8221;</p>
<p>2. แก้ไขไฟล์  /etc/php-prepend.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$real_ip</span>    <span style="color: #339933;">=</span> session_getip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ip</span>        <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;REMOTE_ADDR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;REMOTE_ADDR&quot;</span><span style="color: #009900;">&#93;</span>                    <span style="color: #339933;">=</span> <span style="color: #000088;">$real_ip</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> session_getip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_CLIENT_IP&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_CLIENT_IP&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_X_FORWARDED_FOR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_X_FORWARDED&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_X_FORWARDED&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_FORWARDED_FOR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_FORWARDED_FOR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_FORWARDED&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_FORWARDED&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_X_FORWARDED&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_X_FORWARDED&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;REMOTE_ADDR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Code ดั่งกล่าว  จะทำการ ดึง IP ที่  HTTP_X_FORWARDED   มาไว้ยัง $real_ip และทำการกำหนดให้  REMOTE_ADDR = $real_ip   ทำให้ app ทั่วไปทีเราเขียนขึ้น ไม่ต้องทำการแก้ไข Code แต่อย่างใด</p>
<p>ป.ล.    ส่วนของ prepend และ append  สามารถนำไปใช้งานได้หลายรูปแบบมากกว่านี้เอาไว้โอกาศหน้า ผมจะมาเขียนบอกเล่าให้เพื่อนๆ ได้อ่านต่อไปครับ <img src='http://www.howtoconfig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.howtoconfig.com%2Fdevelopment%2Fremote_addr-http_x_forwarded_for-reverse-proxy.html&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.howtoconfig.com/development/remote_addr-http_x_forwarded_for-reverse-proxy.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>วิธีติดตั้ง Reverse Proxy ด้วย Squid</title>
		<link>http://www.howtoconfig.com/os/ubuntu/how-to-implement-squid-reverse-proxy.html</link>
		<comments>http://www.howtoconfig.com/os/ubuntu/how-to-implement-squid-reverse-proxy.html#comments</comments>
		<pubDate>Fri, 11 Jul 2008 12:25:40 +0000</pubDate>
		<dc:creator>กวิน ชัยเลิศ</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[HTTP traffic]]></category>
		<category><![CDATA[httpd-accelerator]]></category>
		<category><![CDATA[Implement]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[Reverse Proxy]]></category>
		<category><![CDATA[Squid]]></category>
		<category><![CDATA[transparent cache]]></category>

		<guid isPermaLink="false">http://www.howtoconfig.com/?p=32</guid>
		<description><![CDATA[ในเบื่องต้นผมขอแนะนำให้รู้จัก Squid กันก่อนครับ Squid เป็น Open Source Software ที่ให้บริการ Proxy caches ซึ่ง Squid สามารถติดตั้งให้ทำงานใน 3 รูปแบบหลักดังนี้ 1. Standard Proxy Cache ใช้สำหรับเก็บ cache ของ static web pages จำพวก html และรูปภาพ โดยทั่วไปมักจะถูกใช้งานใน network ภายในองค์กร โดยที่ web pages ต่างๆ ที่ถูกเรียกผ่าน local network เป็นครั้งที่สอง web browser จะแสดงผล web pages นั้นผ่าน proxy cache แทนที่ไปดึงข้อมูลจาก web server มาแสดงผล (ประหยัด bandwidth และเพิ่มความเร็วในการแสดงผล) [...]]]></description>
			<content:encoded><![CDATA[<p>ในเบื่องต้นผมขอแนะนำให้รู้จัก Squid กันก่อนครับ</p>
<p>Squid เป็น Open Source Software ที่ให้บริการ Proxy caches ซึ่ง Squid สามารถติดตั้งให้ทำงานใน 3 รูปแบบหลักดังนี้</p>
<p><strong>1. Standard Proxy Cache</strong><br />
ใช้สำหรับเก็บ cache ของ static web pages จำพวก html และรูปภาพ โดยทั่วไปมักจะถูกใช้งานใน network ภายในองค์กร โดยที่ web pages ต่างๆ ที่ถูกเรียกผ่าน local network เป็นครั้งที่สอง web browser จะแสดงผล web pages นั้นผ่าน proxy cache แทนที่ไปดึงข้อมูลจาก web server มาแสดงผล (ประหยัด bandwidth และเพิ่มความเร็วในการแสดงผล)</p>
<p><strong>2. Transparent Cache</strong><br />
ความสามารถหลักๆ เหมือนกับการติดตั้งแบบ standard proxy cache จะแตกต่างกันที่การติดตั้งแบบ transparent cache ไม่จำเป็นต้องปรับแต่ง web browser ให้สามารถใช้งาน proxy cache โดยที่ transparent cache จะทำหน้าที่คอยกรอง HTTP traffic (on port 80) แล้วตรวจสอบว่า request นั้นมีอยู่ใน cahce หรือไม่ ถ้าไม่มีก็จะทำการส่งต่อไปยัง web server ของ web pages นั้น (สำหรับ Linux การใช้งาน transparent cache จะใช้ควบคู่ไปกับ iptables ในการกรอง HTTP traffic)</p>
<p><strong>3. Reverse Proxy Cache</strong><br />
สำหรับ reverse proxy cache จะทำหน้าที่แตกต่างกับ standard และ transparent caches โดยที่ reverse proxy cache จะทำหน้าที่ลดภาระของ web server แทนที่การลด network bandwidth ของฝั่ง client กล่าวคือ reverse proxy cache ถูกติดตั้งอยู่หน้า web server (ระหว่าง internet และ web server) คอยจัดการ traffic ที่เกิดขึ้นทั้งหมดก่อนจะถึง web server ป้องกัน traffic ที่เพิ่มขึ้นโดยไม่พีงประสงค์ (ซึ่งอาจจะโดนโจมตีจาก hacker เป็นต้น) อีกทั้งยังลดภาระของ web server อีกทางนึง<br />
<img class="alignnone size-full image-33" title="reverse-proxy" src="http://www.howtoconfig.com/content/uploads/2008/07/reverse-proxy.gif" alt="" width="436" height="451" /><br />
<span id="more-32"></span><br />
สำหรับวิธีการติดตั้ง Reverse Proxy ด้วย Squid สามารถทำตามขั้นตอนต่อไปนี้ได้เลยครับ<br />
(ทำการติดตั้งบน CentOS 4.6)</p>
<p>ตรวจสอบว่าได้ติดตั้ง squid แล้วหรือยัง</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rpm <span style="color: #660033;">-qa</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> squid</pre></div></div>

<p>ถ้าได้ทำการติดตั้ง squid ไว้แล้วจะแสดงผลของคำสั้งตามตัวอย่างครับ</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[root@reverse ~]# rpm -qa|grep squid
squid-2.5.STABLE14-1.4E.el4_6.2
[root@reverse ~]#</pre></div></div>

<p>กรณียังไม่ได้ติดตั้งให้ใช้คำสั่ง yum install squid ได้เลยครับ</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yum <span style="color: #c20cb9; font-weight: bold;">install</span> squid</pre></div></div>

<p>หลังจากทำการติดตั้ง squid เรียบร้อยแล้วให้ทำการปรับแต่งไฟล์ /etc/squid/squid.conf โดยแก้ไขเพิ่มเติม options เหล่านี้</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http_port 80 #Port ของ Squid
httpd_accel_host 192.168.0.2 #IP address ของ Web Server (แทนที่ public ip 203.101.102.1 ด้วย private ip 192.168.0.2)
httpd_accel_port 80 #Port ของ Web Server
httpd_accel_single_host on #Option on ใช้ในกรณีที่ต้องการติดตั้ง Reverse Proxy ไว้หน้า Web Server เดี่ยวๆ
httpd_accel_with_proxy on
httpd_accel_uses_host_header off</pre></div></div>

<p>เพียงเท่านี้ก็สามารถใช้งาน Reverse Proxy ได้แล้ว รับรองว่าสามารถลดภาระการทำงานของ Web Server แน่นอน<br />
หากเพื่อนๆ มีข้อสงสัยหรือข้อเสนอแนะ &#8230; ก็ comment ไว้ด้วยนะครับ</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.howtoconfig.com%2Fos%2Fubuntu%2Fhow-to-implement-squid-reverse-proxy.html&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.howtoconfig.com/os/ubuntu/how-to-implement-squid-reverse-proxy.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

