日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度

發(fā)布時間:2023/12/31 Nginx 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

碰到問題:
移動用戶訪問web服務(wù)器www.osyunwei.com很慢
解決辦法:
1、在移動機房放置一臺nginx反向代理服務(wù)器
2、通過域名DNS智能解析,所有移動用戶訪問www.osyunwei.com時解析到nginx反向代理服務(wù)器
3、nginx反向代理服務(wù)器與web服務(wù)器之間采用專線連接
系統(tǒng)運維??www.osyunwei.com? 溫馨提醒:qihang01原創(chuàng)內(nèi)容?版權(quán)所有,轉(zhuǎn)載請注明出處及原文鏈接
說明:
1、web服務(wù)器
線路:電信
IP:192.168.21.129
域名:www.osyunwei.com
2、nginx反向代理服務(wù)器
線路:移動
系統(tǒng):CentOS 6.2
IP:192.168.21.164
vi /etc/hosts?? #編輯,在文件最后添加下面一行
192.168.21.129??????????www.osyunwei.com
3、客戶端
線路:移動
系統(tǒng):Windows 7
IP:192.168.21.130
C:\Windows\System32\drivers\etc\hosts?? #用記事本打開,在文件最后添加下面一行
192.168.21.164??????????www.osyunwei.com


###################以下操作在nginx反向代理服務(wù)器上配置###################


1、關(guān)閉SELinux

vi /etc/selinux/config
#SELINUX=enforcing???? #注釋掉
#SELINUXTYPE=targeted? #注釋掉
SELINUX=disabled? #增加
:wq? 保存,關(guān)閉。
shutdown -r now重啟系統(tǒng)
2、開啟防火墻80端口
vi /etc/sysconfig/iptables
添加下面的內(nèi)容
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables restart? #重啟防火墻使配置生效
3、安裝編譯工具
系統(tǒng)運維??www.osyunwei.com? 溫馨提醒:qihang01原創(chuàng)內(nèi)容?版權(quán)所有,轉(zhuǎn)載請注明出處及原文鏈接
yum install wget? make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd? kernel keyutils? patch perl
4 、系統(tǒng)約定
軟件源代碼包存放位置:/usr/local/src
源碼包編譯安裝位置:/usr/local/軟件名字
5、下載軟件
cd /usr/local/src? #進入目錄
(一)、下載nginx(目前穩(wěn)定版)
wget?http://nginx.org/download/nginx-1.0.12.tar.gz
(二)、下載pcre? (支持nginx偽靜態(tài))
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?? ?#創(chuàng)建安裝目錄
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?? #創(chuàng)建nginx運行賬戶www并加入到www組,不允許www用戶直接登錄系統(tǒng)
cd /usr/local/src
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-http_realip_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?? #設(shè)置目錄所有者
chmod 700 -R /usr/local/nginx/html??? #設(shè)置目錄權(quán)限
??vi /etc/rc.d/init.d/nginx??? #?設(shè)置nginx開啟啟動,編輯啟動文件添加下面內(nèi)容
=======================================================
#!/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=$?
echo
[ $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
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
=======================================================
:wq!保存退出
chmod 775 /etc/rc.d/init.d/nginx? #賦予文件執(zhí)行權(quán)限
chkconfig nginx on??? #設(shè)置開機啟動
/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配置文件
(一)、設(shè)置nginx運行賬號
vi /usr/local/nginx/conf/nginx.conf?? #編輯
找到user nobody;修改為
user www www;??? #在第一行
(二)、禁止nginx空主機頭
vi /usr/local/nginx/conf/nginx.conf?? #編輯
找到server,在上面一行添加如下內(nèi)容:
##############################
server {
listen?????? 80 default;
server_name? _;
location / {
root?? html;
return 404;
}
location ~ /.ht {
deny? all;
}
}
##############################
/etc/rc.d/init.d/nginx restart???? #重啟nginx
這樣設(shè)置之后,空主機頭訪問會直接跳轉(zhuǎn)到nginx404錯誤頁面。
(三)、添加nginx虛擬主機包含文件
cd /usr/local/nginx/conf/?? #進入nginx安裝目錄
mkdir vhost?? #建立虛擬目錄
vi? /usr/local/nginx/conf/nginx.conf?? #編輯
找到上一步添加的代碼,在最后添加如下內(nèi)容:
include? vhost/*.conf;
例如:
##############################
server {
listen?????? 80 default;
server_name? _;
location / {
root?? html;
return 404;
}
location ~ /.ht {
deny? all;
}
}
include? vhost/*.conf;
##############################
(四)、添加proxy_cache參數(shù)配置包含文件
cd? /usr/local/nginx/conf/?? #進入目錄
touch proxy.conf?? #建立文件
vi? /usr/local/nginx/conf/nginx.conf?? #編輯
找到http {? 在下面添加一行
include proxy.conf;
(五)、添加被代理服務(wù)器列表包含文件
cd? /usr/local/nginx/conf/?? #進入目錄
touch? mysvrhost.conf? #建立文件
vi? /usr/local/nginx/conf/nginx.conf?? #編輯
找到上一步添加的代碼,在下面添加一行
include? mysvrhost.conf;
(六)、設(shè)置nginx全局參數(shù)
vi? /usr/local/nginx/conf/nginx.conf?? #編輯
worker_processes 2;?????? # 工作進程數(shù),為CPU的核心數(shù)或者兩倍
events
{
use epoll;?? #增加
worker_connections 65535;??? #修改為65535,最大連接數(shù)。
}
#############以下代碼在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;? #增加
(七)、設(shè)置proxy_cache參數(shù)配置
cd /home? #進入目錄
mkdir -p? /home/proxy_temp_dir???? #proxy_temp_dir與proxy_cache_dir這兩個文件夾必須在同一個分區(qū)
mkdir -p? /home/proxy_cache_dir??? #proxy_cache_dir與proxy_temp_dir這兩個文件夾必須在同一個分區(qū)
chown www.www -R? proxy_cache_dir? proxy_temp_dir? #設(shè)置目錄所有者
chmod -R 777? proxy_cache_dir? proxy_temp_dir??? #設(shè)置目錄權(quán)限
系統(tǒng)運維??www.osyunwei.com? 溫馨提醒:qihang01原創(chuàng)內(nèi)容?版權(quán)所有,轉(zhuǎn)載請注明出處及原文鏈
cd? /usr/local/nginx/conf/?? #進入目錄
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;
#設(shè)置Web緩存區(qū)名稱為cache_one,內(nèi)存緩存為50MB,自動清除1天內(nèi)沒有被訪問的文件,硬盤緩存為1GB。
client_body_buffer_size? 512k; #增加緩沖區(qū)代理緩沖客戶端請求的最大字節(jié)數(shù)
proxy_connect_timeout??? 60;?? #增加連接后端服務(wù)器超時時間
proxy_read_timeout?????? 60;?? #增加后端服務(wù)器響應(yīng)請求超時時間
proxy_send_timeout?????? 60;?? #增加后端服務(wù)器發(fā)送數(shù)據(jù)超時時間
proxy_buffer_size??????? 32k;?? #增加代理請求緩存區(qū)大小
proxy_buffers?????????? 4 64k;? #增加
proxy_busy_buffers_size 128k;?? #增加系統(tǒng)繁忙時可申請的proxy_buffers大小
proxy_temp_file_write_size 128k;? #增加proxy緩存臨時文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;?? #增加故障轉(zhuǎn)移,如果后端的服務(wù)器返回502、504、執(zhí)行超時等錯誤,自動將請求轉(zhuǎn)發(fā)到upstream負載均衡池中的另一臺服務(wù)器,實現(xiàn)故障轉(zhuǎn)移。proxy_cache cache_one;? #增加使用web緩存區(qū)cache_one
(八)、設(shè)置被代理服務(wù)器文件列表
cd? /usr/local/nginx/conf/?? #進入目錄
vi mysvrhost.conf? #編輯,添加以下代碼
upstream osyunweihost {
ip_hash;
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?? #編輯

server {
listen?????? 80;
server_name??www.osyunwei.com?osyunwei.com;

location /
{
proxy_pass?http://osyunweihost;
proxy_cache_key $host$uri$is_args$args;? #增加設(shè)置web緩存的key值,nginx根據(jù)key值md5哈希存儲緩存
proxy_set_header Host? $host;
proxy_set_header X-Forwarded-For? $remote_addr;
proxy_cache_valid? 200 304 12h;
expires????? 2d;
proxy_ignore_headers?"Cache-Control"?"Expires"?"Set-Cookie";

}
location ~ .*\.(PHP|jsp|cgi|asp|aspx|flv|swf|xml)?$??? #列出的擴展名文件不緩存。

{
proxy_set_header Host? $host;
proxy_set_header X-Forwarded-For? $remote_addr;
proxy_pass?http://osyunweihost;
}
access_log? off;
}

location ~ /purge(/.*)?? #用于清除緩存
{
allow??????????? 127.0.0.1;
allow??????????? 192.168.21.0/24;?? #設(shè)置只允許指定的IP或IP段才可以清除URL緩存。
deny??????????? all;
proxy_cache_purge??? cache_one?? $host$1$is_args$args;
}
###################以上操作在nginx反向代理服務(wù)器上配置###################
9、ngx_cache_pure清除緩存模塊使用說明
????? 說明:根據(jù)配置只允許192.168.21.0/24 IP段的主機才可以清除URL緩存,現(xiàn)在我使用的客戶機IP是:192.168.21.130,有權(quán)限清除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服務(wù)器教程結(jié)束。

附件:

1、nginx配置文件/usr/local/nginx/conf/nginx.conf

1user? www www;
2worker_processes? 2;
3#error_log??logs/error.log;
4#error_log??logs/error.log? notice;
5#error_log??logs/error.log? info;
6#pid??????? logs/nginx.pid;
7?
8events {
9use?epoll;
10worker_connections? 65535;
11}
12?
13http {
14include???proxy.conf;
15include???mysvrhost.conf;
16include???????mime.types;
17default_type? application/octet-stream;
18?
19#log_format? main??'$remote_addr - $remote_user [$time_local] "$request" '
20#??????????????????'$status $body_bytes_sent "$http_referer" '
21#??????????????????'"$http_user_agent" "$http_x_forwarded_for"';
22?
23#access_log? logs/access.log? main;
24?
25server_names_hash_bucket_size 128;
26client_header_buffer_size 32k;
27large_client_header_buffers 4 32k;
28client_max_body_size 300m;
29sendfile??????? on;
30tcp_nopush???? on;
31?
32#keepalive_timeout? 0;
33keepalive_timeout? 60;
34tcp_nodelay on;
35server_tokens off;
36?
37gzip? on;
38gzip_min_length? 1k;
39gzip_buffers???? 4 16k;
40gzip_http_version 1.1;
41gzip_comp_level 2;
42gzip_types?????? text/plain application/x-javascript text/css application/xml;
43gzip_vary on;
44?
45server {
46listen?????? 80?default;
47server_name? _;
48location / {
49root?? html;
50return?404;
51}
52location ~ /.ht {
53deny? all;
54}
55}
56include??vhost/*.conf;
57}

2、被代理服務(wù)器列表文件/usr/local/nginx/conf/mysvrhost.conf

1upstream osyunweihost {
2ip_hash;
3server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s;
4}

3、proxy_cache參數(shù)配置文件/usr/local/nginx/conf/proxy.conf

1proxy_temp_path?? /home/proxy_temp_dir;
2proxy_cache_path? /home/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;
3client_body_buffer_size? 512k;
4proxy_connect_timeout??? 60;
5proxy_read_timeout?????? 60;
6proxy_send_timeout?????? 60;
7proxy_buffer_size??????? 32k;
8proxy_buffers?????????? 4 64k;
9proxy_busy_buffers_size 128k;
10proxy_temp_file_write_size 128k;
11proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;
12proxy_cache cache_one;

4、虛擬主機配置文件/usr/local/nginx/conf/vhost/www.osyunwei.com.conf

1server {
2listen?????? 80;
3server_name? www.osyunwei.com osyunwei.com;
4location /
5{
6proxy_pass http://osyunweihost;
7proxy_cache_key?$host$uri$is_args$args;
8proxy_set_header Host??$host;
9proxy_set_header X-Forwarded-For??$remote_addr;
10proxy_cache_valid? 200 304 12h;
11expires????? 2d;
12proxy_ignore_headers?"Cache-Control"?"Expires"?"Set-Cookie";
13}
14?
15location ~ /purge(/.*)
16{
17allow??????????? 127.0.0.1;
18allow??????????? 192.168.21.0/24;
19deny??????????? all;
20proxy_cache_purge??? cache_one???$host$1$is_args$args;
21}
22?
23location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
24{
25proxy_set_header Host??$host;
26proxy_set_header X-Forwarded-For??$remote_addr;
27proxy_pass http://osyunweihost;
28}
29access_log? off;
30}


擴展閱讀:

#################################################################
nginx修改版本等信息
1、vi /usr/local/src/nginx-1.0.12/src/core/nginx.h?? #編譯前編輯
#define nginx_version
#define NGINX_VERSION
#define NGINX_VER
#define NGINX_VAR
修改上面的信息,即可更改nginx顯示版本。

2、vi /usr/local/src/nginx-1.0.12/src/http/ngx_http_special_response.c?? ?#編譯前編輯
static u_char ngx_http_error_full_tail[] =
static u_char ngx_http_error_tail[] =
修改上面的信息為你自己的。


3、vi /usr/local/src/nginx-1.0.12/src/http/ngx_http_header_filter_module.c?? ?#編譯前編輯
static char ngx_http_server_string[]=
修改上面的信息為你自己的。


4、編譯完成之后,修改/usr/local/nginx/conf目錄下面
fastcgi.conf、fastcgi.conf.default、fastcgi_params、fastcgi_params.default
這四個文件里面的版本名稱
/usr/local/nginx/sbin/nginx -V ?#查看nginx版本號
#################################################################

? 本文鏈接:http://www.osyunwei.com/archives/2084.html ? 訂閱本站:http://www.osyunwei.com/feed ? 轉(zhuǎn)載請注明來源:系統(tǒng)運維???使用Nginx反向代理和proxy_cache緩存搭建CDN服務(wù)器加快Web訪問速度

總結(jié)

以上是生活随笔為你收集整理的使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。