======
以前寫過(guò)http://www.qzkangyuan.com/html/help/server/2016/0912/4185.html">Nginxhttp://www.qzkangyuan.com/html/help/server/2016/0916/4455.html">反向署理通過(guò)with-http_sub_module和substitutions4nginx模塊替換正文內(nèi)容和URL和在軍哥lnmp的情況下設(shè)置反向署理處事器的要領(lǐng)教程
本教程基于軍哥lnmp情況,其他Nginx類同。區(qū)別在于nginx conf的位置,一般編譯的在/usr/local/nginx/conf/,從源安裝的在/etc/nginx。
lnmp的安裝這里省略,下面教程是在已經(jīng)安裝好的lnmp情況下設(shè)置反向署理處事器,并實(shí)現(xiàn)替換內(nèi)容。
一、僅僅作為反向署理處事器,做cdn加快,不替換內(nèi)容
找到您的nginx conf地址位置,一般編譯的在/usr/local/nginx/conf/,從源安裝的在/etc/nginx
在nginx.conf的http層插手以下內(nèi)容:
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /home/cache/temp;
#姑且文件目次
proxy_cache_path /home/cache/path levels=1:2 keys_zone=cache_one:5m inactive=7d max_size=1g;
#5m為內(nèi)存占用,1g為最大硬盤占用,cache_one為緩存區(qū)名字,假如修改則下文的設(shè)置亦要相應(yīng)修改。
mkdir /home/cache/path -p
mkdir /home/cache/temp
chmod 777 -R /home/cache
新增虛擬主機(jī)設(shè)置:
vi /usr/local/nginx/conf/vhost/xxorg.com.conf
#xxorg.com是你要綁定的域名
設(shè)置文件內(nèi)容:{后端(ip為1.2.3.4)綁定域名xxorg.com,前端綁定域名xxorg.com,域名理會(huì)到前端,實(shí)現(xiàn)cdn加快。}
server{
listen 80;
server_name example.com www.example.com;
#綁定的域名
index index.php;
#默認(rèn)首頁(yè)
access_log off;
#off 封鎖日志
location / {
proxy_cache_key "$scheme://$host$request_uri";
#緩存key法則,用于自動(dòng)排除緩存。
proxy_cache cache_one;
#緩存區(qū)名稱,與前面界說(shuō)的溝通
proxy_cache_valid 200 304 3h;
proxy_cache_valid 301 3d;
proxy_cache_valid any 10s;
#200 304狀態(tài)緩存3小時(shí)
301狀態(tài)緩存3天
其他狀態(tài)緩存(如502 404)10秒
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#向后端通報(bào)訪客ip
proxy_set_header Referer http://example.com;
#強(qiáng)制界說(shuō)Referer,措施驗(yàn)證判定會(huì)用到
proxy_set_header Host $host;
#界說(shuō)主機(jī)頭
proxy_pass http://1.2.3.4;
#指定后端ip,可以加端口
#proxy_cache_use_stale invalid_header error timeout http_502;
#當(dāng)后端呈現(xiàn)錯(cuò)誤、超時(shí)、502狀態(tài)時(shí)啟用逾期緩存,慎用。
}
}
如無(wú)意外,重啟nginx后把xxorg.com綁定到前端就可以會(huì)見(jiàn)了
也可以用以下要領(lǐng)查察設(shè)置文件是否正確:
執(zhí)行:
/usr/local/nginx/sbin/nginx -t
查抄設(shè)置是否正常,假如顯示:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
就說(shuō)明nginx的設(shè)置文件正常,不然按錯(cuò)誤提示修改設(shè)置。
然后執(zhí)行
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
使設(shè)置生效,
/etc/init.d/nginx restart
=========================
二、下面通過(guò)Nginx反向署理別人的網(wǎng)站,并替換相關(guān)內(nèi)容
1.編譯nginX:
apt-get update#nginx-full這個(gè)包內(nèi)里包括著所有需要用到的模塊。
cd /root
apt-get update
apt-get install -y git gcc g++ make automake
#安裝依賴包,Centos將apt-get變動(dòng)為yum
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
wget http://nginx.org/download/nginx-1.2.8.tar.gz
tar zxvf nginx-1.2.8.tar.gz
cd nginx-1.2.8
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/root/ngx_http_substitutions_filter_module
make
make install
假如您用的系統(tǒng)是Debian,就不需要編譯了。