构建Nginx服务器:
1)源码安装Nginx软件
[root@svr5 ~]# yum -y install gcc pcre-devel openssl-devel //安装常见依赖包
[root@svr5 ~]# useradd -s /sbin/nologin nginx
[root@svr5 ~]# tar -zxvf nginx-0.8.55.tar.gz
[root@svr5 ~]# cd nginx-0.8.55
[root@svr5 nginx-0.8.55]# ./configure /
> --prefix=/usr/local/nginx / //指定安装路径
> --user=nginx / //指定用户
> --group=nginx / //指定组
> --with-http_stub_status_module / //开启状态统计功能
> --with-http_ssl_module //开启SSL加密功能
.. ..
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@svr5 nginx-0.8.55]# make && make install //编译并安装
2)启用Nginx服务并查看监听端口状态
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –c /usr/local/nginx/conf/nginx.conf
[root@svr5 ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx
3)为Nginx Web服务器建立测试页面文件
Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下建立一个名为index.html的文件:
[root@svr5 ~]# cat /usr/local/nginx/html/index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
从系统中随机拷贝一份jpg图片资源,供测试使用:
[root@svr5 ~]# cp /usr/share/pixmaps/faces/energy-arc.jpg /
>/usr/local/nginx/html/a.jpg
提前生成404错误页面,供测试使用:
[root@svr5 ~]# echo "<h1>~~~~^^^Error^^^~~~</h1>" > /usr/local/nginx/html/40x.html
4)修改Nginx实现防止盗链
[root@svr5 ~]# /usr/local/nginx/conf/nginx.conf
.. ..
location ~* /.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.tarena.com;
if ($invalid_referer) {
rewrite ^/ http://www.tarena.com/403.html;
}
}
5)再创建一台Nginx服务器(192.168.4.205,使用客户端模式另一台盗链服务器),并创建连接资源
[root@svr205 ~]# vim /usr/local/nginx/html/index.html
<html>
<body>
<a href=”http://www.tarena.com/a.jpg”> 图片</a>
</body>
</html>
6)客户端测试
从客户机访问192.168.4.205,验证是否可以通过链接访问www.tarena.com服务器上的资源,使用火狐浏览器访问192.168.4.205:
[root@pc205 ~]# firefox http://192.168.4.205