nginx網站服務的安裝與配置
1.編寫腳本,以安裝nginx
編寫腳本前須知nginx的安裝環境
此案例提供兩個版本的nginx(nginx-0.8.55.tar.gz nginx-1.0.5.tar.gz)
其爲源碼包,需要gcc環境(gcc gcc-c++ make)以及pcrepcre-devel ssl openssl
在root家目錄下新建腳本文件install.sh
[root@localhost ~]# cat install.sh
#!/bin/bash
service httpd stop > /dev/null
--關閉http服務對端口80的佔用
chkconfig httpd off > /dev/null
cd nginx-package
--進入安裝包所在目錄
tar -zxf nginx-0.8.55.tar.gz
tar -zxf nginx-1.0.5.tar.gz
cd nginx-0.8.55
useradd -M -s /sbin/nologin www
--新建一個爲服務進行調度的用戶
yum -y install prce prce-devel >/dev/null
--安裝依賴包
yum -y install openssl* > /dev/null
./configure --prefix=/usr/local/nginx--user=www --group=www --with-http_stub_status_module --with-http_ssl_module
--prefix爲指定安裝位置with-http_stub_status_module
make
--編譯安裝
make install
2.配置基於域名的虛擬主機
www .tarena.com
bbs .tarena.com
只允許從ip 192.168.13.1主機訪問 bbs .tarena.com 的8080 端口 訪問時要提交正確的用戶adm 密碼888 方可訪問
[root@localhost ~]# cd/usr/local/nginx/sbin/ --進入執行程序目錄
[root@localhost sbin]# ./nginx --開啓服務
[root@localhost sbin]# cd/usr/local/nginx/conf/
--進入配置文件目錄
[root@localhost conf]# cp nginx.confnginx.confbak --備份
[root@localhost conf]# mkdir ../html/www
[root@localhost conf]# mkdir ../html/bbs
[root@localhost conf]# echo www >../html/www/index.html
[root@localhost conf]# echo bbs >../html/bbs/index.html
[root@localhost conf]# yum -y installhttpd --安裝http服務是爲例使用它的 htpasswd設定密碼訪問控制功能
[root@localhost conf]# htpasswd -c/usr/local/nginx/conf/authuser.txt adm
--新建用戶adm
[root@localhost conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost; --主機名有變化時需更改
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen80; --監聽端口爲80
server_namewww.tarena.com;
--服務域名
location/ {
root/usr/local/nginx/html/www;
--訪問主頁位置
indexindex.html;
--主頁名
}
}
server {
listen 192.168.13.5:8010;
--通過IP地址訪問,端口爲8010
server_name www.tarena.com;
location / {
root/usr/local/nginx/html/www;
indexindex.html;
}
}
server {
listen8080;
--監聽端口爲80
server_namebbs.tarena.com;
--服務域名
location/ {
root/usr/local/nginx/html/bbs;
--訪問主頁位置
indexindex.html;
--主頁名
allow 192.168.13.1;
--允許訪問的主機
denyall
; --拒絕所有
auth_basic" 請輸入用戶名密碼"; --訪問時的提示信息
auth_basic_user_file/usr/local/nginx/conf/authuser.txt;
--帳號密碼讀取位置
}
}
}
通過客戶機測試效果
[root@localhost 桌面]# elinks --dump http://www.tarena.com
www
通過網頁訪問bbs.tarena.com時會要求輸入用戶名與密碼,輸入後將看到其網頁文件內容
3.通過腳本進行nginx的在不關閉的請光下平滑升級
[root@localhost ~]# cat update.sh
#!/bin/bash
cd /root/nginx-package/nginx-1.0.5 --進入高版本安裝目錄
./configure --prefix=/usr/local/nginx--user=www --group=www --with-http_stub_status_module –with-http_ssl_module
--執行操作要與低版本一致
make
--編譯
mv /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginxold
--將老版本執行程序改名
mv objs/nginx /usr/local/nginx/sbin/
--將新版本執行程序移動到安裝目錄
make upgrade --進行平滑升級
4.nginx反向代理服務器的配置以及不同需求訪問的分離
案例如下:使用Ip地址是1.1.1.1的服務器做nginx反向代理服務器,當接收到用戶發給自己的連接請求http://www.baidu.com 時 代替用戶訪問內網的網站服務器。(當用戶訪問以.php結尾的文件時,代替用戶連接內網的網站服務器1.1.1.2和1.1.1.3 ,ip2機器配置比較高 讓他的響應次數比ip 3的主機多, 用戶訪問以.html結尾的文件時到本機nginx安裝目錄下html子目錄下獲取網頁文件,並對此網頁目錄做防盜鏈設置。)
http{
upstreamwebgrp {
--定義組
server 1.1.1.2:80 weight=3;
--weight定義訪問權重,默認1
server 1.1.1.3:80;
}
server {
listen 80;
server_name www.baidu.com;
--服務器名
location ~ \.php$ {
--php結尾去找下面的組
proxy_pass http://webgrp;
}
location ~ \.html$ {
--html結尾去找下面的主機
proxy_pass http://1.1.1.1;
}
location ~* \.(gif|ipg|png|swf|flv|htm)$ { --外鏈不允許的網頁格式
valid_referers none blocked www.baidu.com .baidu.com;
--允許外鏈的主機和域
if ($invalid_referer) {
rewrite ^/ http://1.1.1.1/error.html;
--如果不允許跳轉到此頁
#return 404;
}
}
web1 1.1.1.2 外鏈(賊)服務器192.168.13.3
1.1.1.1 nginx代理 192.168.13.1
web2 1.1.1.3 測試客戶機192.168.13.2
測試時在外鏈服務器新建外鏈頁指向nginx代理,如下
[root@localhosthtml]# cat index.html
<html>
<body>
<ahref="http://www.baidu.com/index.htm">fangdanlianceshi</a>
</body>
</html>
通過客戶端訪問外鏈服務器查看時候外鏈是否被禁止