構建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