搜索
首页运维Nginx怎么使用Nginx反向代理与proxy_cache缓存搭建CDN服务器

碰到问题:
移动用户访问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用户直接登录系统
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-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=$?
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 #赋予文件执行权限
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空主机头
vi /usr/local/nginx/conf/nginx.conf #编辑
找到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 #建立虚拟目录
vi /usr/local/nginx/conf/nginx.conf #编辑
找到上一步添加的代码,在最后添加如下内容:
include vhost/*.conf;
例如:
##############################
server {
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~ /.ht {
deny all;
}
}
include vhost/*.conf;
##############################
(四)、添加proxy_cache参数配置包含文件
cd /usr/local/nginx/conf/ #进入目录
touch proxy.conf #建立文件
vi /usr/local/nginx/conf/nginx.conf #编辑
找到http { 在下面添加一行
include proxy.conf;
(五)、添加被代理服务器列表包含文件
cd /usr/local/nginx/conf/ #进入目录
touch mysvrhost.conf #建立文件
vi /usr/local/nginx/conf/nginx.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原创内容©版权所有,转载请注明出处及原文链
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;
#设置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
(八)、设置被代理服务器文件列表
cd /usr/local/nginx/conf/ #进入目录
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 #编辑

server {
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)?$ #列出的扩展名文件不缓存。

{
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; #设置只允许指定的ip或ip段才可以清除url缓存。
deny all;
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

怎么使用Nginx反向代理与proxy_cache缓存搭建CDN服务器

2、清除这个文件缓存:http://www.osyunwei.com/purge/images/nopic.gif

怎么使用Nginx反向代理与proxy_cache缓存搭建CDN服务器

提示:successful purge,缓存文件清除成功,如果这个文件没有被缓存过,则提示:404 not found

怎么使用Nginx反向代理与proxy_cache缓存搭建CDN服务器

备注:
1、purge是ngx_cache_pure 模块指令
2、images/nopic.gif 是要清除的缓存文件url路径

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

附件:

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

 user www www; 
worker_processes 2; 
#error_log logs/error.log; 
#error_log logs/error.log notice; 
#error_log logs/error.log info; 
#pid logs/nginx.pid; 

events { 
use epoll; 
worker_connections 65535; 
} 

http { 
include proxy.conf; 
include mysvrhost.conf; 
include 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; 

server { 
listen 80 default; 
server_name _; 
location / { 
root html; 
return 404; 
} 
location ~ /.ht { 
deny all; 
} 
} 
include vhost/*.conf; 
}

2、被代理服务器列表文件/usr/local/nginx/conf/mysvrhost.conf

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

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

 server { 
listen 80; 
server_name www.osyunwei.com osyunwei.com; 
location / 
{ 
proxy_pass http://osyunweihost; 
proxy_cache_key $host$uri$is_args$args; 
proxy_set_header host $host; 
proxy_set_header x-forwarded-for $remote_addr; 
proxy_cache_valid 200 304 12h; 
expires 2d; 
} 

location ~ /purge(/.*) 
{ 
allow 127.0.0.1; 
allow 192.168.21.0/24; 
deny all; 
proxy_cache_purge cache_one $host$1$is_args$args; 
} 

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; 
}

扩展阅读:
#################################################################
nginx修改版本等信息
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显示版本。
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反向代理与proxy_cache缓存搭建CDN服务器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
在Nginx和Apache之间进行选择:适合您的需求在Nginx和Apache之间进行选择:适合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有优劣,适合不同场景。1.NGINX适合高并发和低资源消耗场景。2.Apache适合需要复杂配置和丰富模块的场景。通过比较它们的核心特性、性能差异和最佳实践,可以帮助你选择最适合需求的服务器软件。

nginx怎么启动nginx怎么启动Apr 14, 2025 pm 01:06 PM

问题:如何启动 Nginx?答案:安装 Nginx启动 Nginx验证 Nginx 是否已启动探索其他启动选项自动启动 Nginx

怎么查看nginx是否启动怎么查看nginx是否启动Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么关闭nginx怎么关闭Apr 14, 2025 pm 01:00 PM

要关闭 Nginx 服务,请按以下步骤操作:确定安装类型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服务:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自动启动(可选):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎么配置nginx在windows中怎么配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

nginx403错误怎么解决nginx403错误怎么解决Apr 14, 2025 pm 12:54 PM

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

linux怎么启动nginxlinux怎么启动nginxApr 14, 2025 pm 12:51 PM

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

linux怎么查看nginx是否启动linux怎么查看nginx是否启动Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能