天天看點

反向代理原理反向代理伺服器配置解決通路加速

基本原理:

使用者a始終認為它通路的是原始伺服器b而不是代理伺服器z,但實用際上反向代理伺服器接受使用者a的應答,從原始資源伺服器b中取得使用者a的需求資源,然後發送給使用者a。由于防火牆的作用,隻允許代理伺服器z通路原始資源伺服器b。盡管在這個虛拟的環境下,防火牆和反向代理的共同作用保護了原始資源伺服器b,但使用者a并不知情。

ps:簡單的說,使用者a所請求的和響應全由代理伺服器z和真實的伺服器b做了代理工作

解決使用單線程下nginx反向代理伺服器配置(網絡資料提供參考,原文:http://www.jb51.net/article/38046.htm):

linux下通過nginx反向代理和proxy_cache緩存搭建cdn伺服器加快web通路速度的配置方法

問題場景:

移動使用者通路web伺服器www.osyunwei.com很慢

解決辦法:

1、在移動機房放置一台nginx反向代理伺服器

2、通過域名dns智能解析,所有移動使用者通路www.osyunwei.com時解析到nginx反向代理伺服器

3、nginx反向代理伺服器與web伺服器之間采用專線連接配接

說明:

1、web伺服器

線路:電信

ip:192.168.21.129

域名:www.osyunwei.com

2、nginx反向代理伺服器

線路:移動

系統:centos 6.2

ip:192.168.21.164

vi /etc/hosts #編輯,在檔案最後添加下面一行

192.168.21.129 www.osyunwei.com

3、用戶端

系統:windows 7

ip:192.168.21.130

c:\windows\system32\drivers\etc\hosts #用記事本打開,在檔案最後添加下面一行

192.168.21.164 www.osyunwei.com

###################以下操作在nginx反向代理伺服器上配置###################

1、關閉selinux

vi /etc/selinux/config

#selinux=enforcing #注釋掉

#selinuxtype=targeted #注釋掉

selinux=disabled #增加

:wq 儲存,關閉。

shutdown -r now重新開機系統

2、開啟防火牆80端口

vi /etc/sysconfig/iptables

添加下面的内容

-a input -m state --state new -m tcp -p tcp --dport 80 -j accept

/etc/init.d/iptables restart #重新開機防火牆使配置生效

3、安裝編譯工具

yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl

4 、系統約定

軟體源代碼包存放位置:/usr/local/src

源碼包編譯安裝位置:/usr/local/軟體名字

5、下載下傳軟體

cd /usr/local/src #進入目錄

(一)、下載下傳nginx(目前穩定版)

wget http://nginx.org/download/nginx-1.0.12.tar.gz

(二)、下載下傳pcre (支援nginx僞靜态)

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz

(二)、下載下傳ngx_cache_purge(清除指定url緩存)

wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz

6、安裝pcre

cd /usr/local/src

mkdir /usr/local/pcre #建立安裝目錄

tar zxvf pcre-8.21.tar.gz

cd pcre-8.21

./configure --prefix=/usr/local/pcre #配置

make

make install

7、安裝 nginx

groupadd www #添加www組

useradd -g www www -s /bin/false #建立nginx運作賬戶www并加入到www組,不允許www使用者直接登入系統

tar zxvf ngx_cache_purge-1.5.tar.gz

tar zxvf nginx-1.0.12.tar.gz

cd nginx-1.0.12

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.21 --add-module=../ngx_cache_purge-1.5

注意:--with-pcre=/usr/local/src/pcre-8.21指向的是源碼包解壓的路徑,而不是安裝的路徑,否則會報錯

make #編譯

make install #安裝

/usr/local/nginx/sbin/nginx #啟動nginx

chown www.www -r /usr/local/nginx/html #設定目錄所有者

chmod 700 -r /usr/local/nginx/html #設定目錄權限

vi /etc/rc.d/init.d/nginx # 設定nginx開啟啟動,編輯啟動檔案添加下面内容

=======================================================

#!/bin/bash

# nginx startup script for the nginx http server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: nginx is a high-performance web and proxy server.

# it has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/usr/local/nginx/logs/nginx.pid

retval=0

prog="nginx"

# source function library.

. /etc/rc.d/init.d/functions

# source networking configuration.

. /etc/sysconfig/network

# check that networking is up.

[ ${networking} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

echo "nginx already running...."

exit 1

fi

echo -n $"starting $prog: "

daemon $nginxd -c ${nginx_config}

retval=$?

echo

[ $retval = 0 ] && touch /var/lock/subsys/nginx

return $retval

}

# stop nginx daemons functions.

stop() {

echo -n $"stopping $prog: "

killproc $nginxd

[ $retval = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid

reload() {

echo -n $"reloading $prog: "

#kill -hup `cat ${nginx_pid}`

killproc $nginxd -hup

# see how we were called.

case "$1" in

start)

start

stop)

stop

reload)

reload

restart)

;;

status)

status $prog

*)

echo $"usage: $prog {start|stop|restart|reload|status|help}"

esac

exit $retval

:wq!儲存退出

chmod 775 /etc/rc.d/init.d/nginx #賦予檔案執行權限

chkconfig nginx on #設定開機啟動

/etc/rc.d/init.d/nginx restart

service nginx restart

8、配置nginx

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.confbak #備份nginx配置檔案

(一)、設定nginx運作賬号

vi /usr/local/nginx/conf/nginx.conf #編輯

找到user nobody;修改為

user www www; #在第一行

(二)、禁止nginx空主機頭

找到server,在上面一行添加如下内容:

##############################

server {

listen 80 default;

server_name _;

location / {

root html;

return 404;

location ~ /.ht {

deny all;

/etc/rc.d/init.d/nginx restart #重新開機nginx

這樣設定之後,空主機頭通路會直接跳轉到nginx404錯誤頁面。

(三)、添加nginx虛拟主機包含檔案

cd /usr/local/nginx/conf/ #進入nginx安裝目錄

mkdir vhost #建立虛拟目錄

找到上一步添加的代碼,在最後添加如下内容:

include vhost/*.conf;

例如:

(四)、添加proxy_cache參數配置包含檔案

cd /usr/local/nginx/conf/ #進入目錄

touch proxy.conf #建立檔案

找到http { 在下面添加一行

include proxy.conf;

(五)、添加被代理伺服器清單包含檔案

touch mysvrhost.conf #建立檔案

找到上一步添加的代碼,在下面添加一行

include mysvrhost.conf;

(六)、設定nginx全局參數

vi /usr/local/nginx/conf/nginx.conf #編輯 

worker_processes 2; # 工作程序數,為cpu的核心數或者兩倍

events

{

use epoll; #增加

worker_connections 65535; #修改為65535,最大連接配接數。

#############以下代碼在http { 部分增加與修改##############

server_names_hash_bucket_size 128; #增加

client_header_buffer_size 32k; #增加

large_client_header_buffers 4 32k; #增加

client_max_body_size 300m; #增加

tcp_nopush on; #修改為on

keepalive_timeout 60; #修改為60

tcp_nodelay on; #增加

server_tokens off; #增加,不顯示nginx版本資訊

gzip on; #修改為on

gzip_min_length 1k; #增加

gzip_buffers 4 16k; #增加

gzip_http_version 1.1; #增加

gzip_comp_level 2; #增加

gzip_types text/plain application/x-javascript text/css application/xml; #增加

gzip_vary on; #增加

(七)、設定proxy_cache參數配置

cd /home #進入目錄

mkdir -p /home/proxy_temp_dir #proxy_temp_dir與proxy_cache_dir這兩個檔案夾必須在同一個分區

mkdir -p /home/proxy_cache_dir #proxy_cache_dir與proxy_temp_dir這兩個檔案夾必須在同一個分區

chown www.www -r proxy_cache_dir proxy_temp_dir #設定目錄所有者

chmod -r 777 proxy_cache_dir proxy_temp_dir #設定目錄權限

系統運維 www.osyunwei.com 溫馨提醒:qihang01原創内容©版權所有,轉載請注明出處及原文鍊

vi proxy.conf #編輯,添加以下代碼

proxy_temp_path /home/proxy_temp_dir; #指定臨時檔案目錄

proxy_cache_path /home/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g;

#設定web緩存區名稱為cache_one,記憶體緩存為50mb,自動清除1天内沒有被通路的檔案,硬碟緩存為1gb。

client_body_buffer_size 512k; #增加緩沖區代理緩沖用戶端請求的最大位元組數

proxy_connect_timeout 60; #增加連接配接後端伺服器逾時時間

proxy_read_timeout 60; #增加後端伺服器響應請求逾時時間

proxy_send_timeout 60; #增加後端伺服器發送資料逾時時間

proxy_buffer_size 32k; #增加代理請求緩存區大小

proxy_buffers 4 64k; #增加

proxy_busy_buffers_size 128k; #增加系統繁忙時可申請的proxy_buffers大小

proxy_temp_file_write_size 128k; #增加proxy緩存臨時檔案的大小

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; #增加故障轉移,如果後端的伺服器傳回502、504、執行逾時等錯誤,自動将請求轉發到upstream負載均衡池中的另一台伺服器,實作故障轉移。proxy_cache cache_one; #增加使用web緩存區cache_one

(八)、設定被代理伺服器檔案清單

vi mysvrhost.conf #編輯,添加以下代碼

upstream osyunweihost {

server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s;

(九)、建立虛拟主機配置檔案

cd /usr/local/nginx/conf/vhost #進入虛拟主機目錄

touch www.osyunwei.com.conf #建立虛拟主機配置檔案

vi www.osyunwei.com.conf #編輯

listen 80;

server_name www.osyunwei.com osyunwei.com;

location /

proxy_pass http://osyunweihost;

proxy_cache_key $host$uri$is_args$args; #增加設定web緩存的key值,nginx根據key值md5哈希存儲緩存

proxy_set_header host $host;

proxy_set_header x-forwarded-for $remote_addr;

proxy_cache_valid 200 304 12h;

expires 2d;

location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$ #列出的擴充名檔案不緩存。

access_log off;

location ~ /purge(/.*) #用于清除緩存

allow 127.0.0.1;

allow 192.168.21.0/24; #設定隻允許指定的ip或ip段才可以清除url緩存。

proxy_cache_purge cache_one $host$1$is_args$args;

###################以上操作在nginx反向代理伺服器上配置###################

9、ngx_cache_pure清除緩存子產品使用說明

說明:根據配置隻允許192.168.21.0/24 ip段的主機才可以清除url緩存,現在我使用的客戶機ip是:192.168.21.130,有權限清除url緩存。

1、浏覽圖檔檔案:http://www.osyunwei.com/images/nopic.gif

反向代理原理反向代理伺服器配置解決通路加速

2、清除這個檔案緩存:http://www.osyunwei.com/purge/images/nopic.gif

反向代理原理反向代理伺服器配置解決通路加速

提示:successful purge,緩存檔案清除成功,如果這個檔案沒有被緩存過,則提示:404 not found

反向代理原理反向代理伺服器配置解決通路加速

備注:

1、purge是ngx_cache_pure 子產品指令

2、images/nopic.gif 是要清除的緩存檔案url路徑

至此,使用nginx反向代理和proxy_cache緩存功能配置cdn伺服器教程結束。

附件:

1、nginx配置檔案/usr/local/nginx/conf/nginx.conf

<a target="_blank" href="http://www.jb51.net/article/38046.htm#">?</a>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

user www www;

worker_processes 2;

#error_log

logs/error.log;

logs/error.log notice;

logs/error.log info;

#pid logs/nginx.pid;

events {

use

epoll;

worker_connections 65535;

http {

include

proxy.conf;

mysvrhost.conf;

mime.types;

default_type application/octet-stream;

#log_format main

'$remote_addr - $remote_user [$time_local]

"$request" '

#

'$status $body_bytes_sent "$http_referer"

'

'"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 300m;

sendfile on;

tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 60;

tcp_nodelay on;

server_tokens off;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.1;

gzip_comp_level 2;

gzip_types text/plain application/x-javascript text/css application/xml;

gzip_vary on;

listen 80

default;

return

404;

vhost/*.conf;

2、被代理伺服器清單檔案/usr/local/nginx/conf/mysvrhost.conf

3、proxy_cache參數配置檔案/usr/local/nginx/conf/proxy.conf

proxy_temp_path /home/proxy_temp_dir;

proxy_cache_path /home/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m

inactive=1d max_size=30g;

client_body_buffer_size 512k;

proxy_connect_timeout 60;

proxy_read_timeout 60;

proxy_send_timeout 60;

proxy_buffer_size 32k;

proxy_buffers 4 64k;

proxy_busy_buffers_size 128k;

proxy_temp_file_write_size 128k;

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

proxy_cache cache_one;

4、虛拟主機配置檔案/usr/local/nginx/conf/vhost/www.osyunwei.com.conf

proxy_cache_key

$host$uri$is_args$args;

proxy_set_header host

$host;

proxy_set_header x-forwarded-for

$remote_addr;

location ~ /purge(/.*)

allow 192.168.21.0/24;

proxy_cache_purge cache_one

$host$1$is_args$args;

location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$

擴充閱讀:

#################################################################

nginx修改版本等資訊

vi /usr/local/src/nginx-1.0.12/src/core/nginx.h #編譯前編輯

#define nginx_version

#define nginx_ver

#define nginx_var

修改上面的資訊,即可更改nginx顯示版本。

vi /usr/local/src/http/ngx_http_special_response.c #編譯前編輯

static u_char ngx_http_error_full_tail[] =

static u_char ngx_http_error_tail[] =

修改上面的資訊為你自己的。

其他解決方案:

抛棄nginx使用nodejs做反向代理伺服器

 時下不少場景,都是申請一個 vps 主機來托管運作 web 項目的,小弟我也不例外,購買了一個小型的 win 03 vps 使用着。在使用的過程中,面臨一個問題,就是同一類型的服務端環境還好——但如果是一個 php、一個 asp、 一個 jsp 的三種類型的服務端項目并存着,該怎麼配置設定唯一的 80 端口呢?因為商業 www 網站的話,往往隻能占用 80  端口,——當然,如果隻是做服務的話,如接口之類的,使用其他端口就不會與 80 端口沖突了。許多開發者都會面臨到 80 端口這個問題,并且實際情況會受到成本的限制。因為單獨為一個項目就買一個

vps,也不太經濟、不太合算,管理起來也不友善。于是,我們就應該好好考慮一下,怎麼在提供一個 80 端口的情況下,分發到多種服務端那裡去,讓不同的主機執行各自的 web 項目。

親,那這項需求我們說可以實作嗎?是的,這并不是什麼“神奇的技術”,也不是什麼複雜的技術。不知你是否有了解,網絡服務中的“反向代理(reverse proxy)”,其中的一個功能就是可以完成端口的分發的。我們不妨以域名為路由分發:凡是 aa.com 域名請求的,分發到 php 82 端口執行;凡是 bb.com 域名請求的,分發到 asp 83 端口執行;…… 如此類推。當然這裡的端口隻說說明用而已,您可以任意配置,反正就是從 80 端口接收回來的請求,先作一次處理,進而分發。反向代理,通俗地講,就是左手轉右手而已。

每當提起反向代理器,人們通常一想到的就是 nginx,但是今天我們暫時忽略大名鼎鼎的 nginx,采用同樣也是使用單線程、事件循環的服務端小弟——nodejs 來達成。首先 node 采用 js 作服務端程式設計,而不是 nginx 寫配置或 lua,比較符合我的味口,其次自己對 node 也比較熟悉,配置各方面什麼的更為順手。

完成該項功能的是 node-http-proxy 包。下載下傳、安裝請鍵入:

?

npm install http-proxy

安裝完畢後,建立一個 proxy.js 檔案,輸入:

<code>var</code>

<code>http = require(</code><code>'http'</code><code>), httpproxy = require(</code><code>'http-proxy'</code><code>);</code>

<code>// 建立一個代理 proxy server 對象</code>

<code>proxy = httpproxy.createproxyserver({});</code>

<code>// 捕獲異常</code>

<code>proxy.on(</code><code>'error'</code><code>,</code><code>function</code>

<code>(err, req, res) {</code>

<code> </code><code>res.writehead(500, {</code>

<code> </code><code>'content-type'</code><code>:</code><code>'text/plain'</code>

<code> </code><code>});</code>

<code> </code><code>res.end(</code><code>'something went wrong. and we are reporting a custom error message.'</code><code>);</code>

<code>});</code>

<code>// 另外建立一個 http 80 端口的伺服器,也就是正常 node 建立 http 伺服器的方法。</code>

<code>// 在每次請求中,調用 proxy.web(req, res config) 方法進行請求分發create your custom server and just call `proxy.web()` to proxy</code>

<code>// a web request to the target passed in the options</code>

<code>// also you can use `proxy.ws()` to proxy a websockets request</code>

<code>//</code>

<code>server = require(</code><code>'http'</code><code>).createserver(</code><code>function</code><code>(req, res) {</code>

<code> </code><code>// you can define here your custom logic to handle the request</code>

<code> </code><code>// and then proxy the request.</code>

<code> </code><code>var</code>

<code>host = req.url;</code>

<code> </code><code>host = url.parse(host); host = host.host;</code>

<code> </code> 

<code> </code><code>console.log(</code><code>"host:"</code>

<code>+ req.headers.host);</code>

<code> </code><code>console.log(</code><code>"client ip:"</code> <code>+ (req.headers[</code><code>'x-forwarded-for'</code><code>] || req.connection.remoteaddress));</code>

<code>console.log(</code><code>"listening on port 80"</code><code>)</code>

<code>server.listen(80);</code>

若說使用代理伺服器的代價,可能就是會比不用消耗多的資源,消耗多的 cpu 運算罷了。

使用問題:不能指定檔案夾 proxy.web(req, res, { target: 'http://jb51.net:81/foo/' });

繼續閱讀