nginx虛擬主機配置
所謂虛擬主機,在Web服務裏就是一個獨立的網站站點,這個站點對應獨立的域名(也可能是ip或端口)具有獨立的程序及資源目錄,可以獨立地對外提供服務供用戶訪問。
這個獨立的站點在配置裏是由一定格式的標籤段標記的,Nginx軟件則使用一個server{}標籤來標示一個虛擬主機。一個Web服務裏可以有多個虛擬主機標籤對,即可以同時支持多個虛擬主機站點
配置虛擬主機的步驟
修改配置文件
nginx配置多個虛擬主機也就是在配置文件裏添加多個server標籤,在server標籤裏指定不同的域名或IP地址以及監聽不同的端口。(下面的示例是基於域名的虛擬主機)
[root@anuo conf]# vim /usr/local/nginx/conf/nginx.conf
……
10 server {
11 listen 80; (--基於端口的虛擬主機就修改監聽的不同端口)
12 server_name www.anuo1.com; --第一的虛擬主機的域名
13 location / {
14 root html/www.anuo1.com; --指定網站存放的目錄
15 index index.html index.htm;
16 }
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 server {
23 listen 80; (--基於IP的虛擬主機也就是在這裏加上虛擬主機的IP地址)
24 server_name www.anuo2.com; --第二個虛擬主機的域名
25 location / {
26 root html/www.anuo2.com; --指定網站存放的目錄
27 index index.html index.htm;
28 }
29 error_page 500 502 503 504 /50x.html;
30 location = /50x.html {
31 root html;
32 }
33
34 }
檢查配置修改的語法是否正確 (這一步最好要養成個習慣,提前檢測錯誤總是比後面焦頭爛額的去排錯要好)
[root@anuo conf]# nginx -t
nginx: the configuration file /usr/local/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.2/conf/nginx.conf test is successful
重啓服務
[root@anuo conf]# nginx -s reload --這裏重新加載即可
注意:配置和IP地址相關的都要(-s stop)重啓才生效
[root@anuo conf]# nginx -s stop
創建站點目錄和主頁文件
[root@anuo conf]# mkdir /usr/local/nginx/html/www.anuo{1..2}.com -p
創建主頁文件
[root@anuo conf]# echo "hello anuo2" > /usr/local/nginx/html/www.anuo2.com/index.html
[root@anuo conf]# echo "hello anuo1" > /usr/local/nginx/html/www.anuo1.com/index.html
檢查主頁內容信息
[root@anuo conf]# cat /usr/local/nginx/html/www.anuo1.com/index.html
hello anuo1
[root@anuo conf]# cat /usr/local/nginx/html/www.anuo2.com/index.html
hello anuo2
修改主機hosts文件
[root@anuo conf]# vim /etc/hosts
10.0.0.10 www.anuo1.com www.anuo2.com
測試
[root@anuo conf]# curl www.anuo1.com
hello anuo1
[root@anuo conf]# curl www.anuo2.com
hello anuo2
配置多個虛擬主機的文件規範
虛擬主機多了配置文件規範方便於管理
創建虛擬主機配置文件存儲目錄
[root@anuo conf]# pwd
/usr/local/nginx/conf
[root@anuo conf]# mkdir xnzj --在配置文件下創建存放虛擬主機的目錄(目錄名可隨便定義)
給單個虛擬主機創建配置文件
[root@anuo conf]# sed -n '10,21p' nginx.conf > xnzj/anuo1.conf
[root@anuo conf]# sed -n '22,34p' nginx.conf > xnzj/anuo2.conf
--就是把配置文件裏的每個server標籤做出單個獨立的文件
修改nginx配置文件使之加載識別虛擬主機配置文件
[root@anuo conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include xnzj/*; --指定虛擬主機配置文件的目錄( *表示該目錄下的所有)
}
檢測配置語法、重啓服務、檢查監聽端口
[root@anuo conf]# nginx -t
[root@anuo conf]# nginx -s stop
[root@anuo conf]# nginx
[root@anuo conf]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 8978 root 6u IPv4 21170 0t0 TCP *:http (LISTEN)
nginx 8979 nginx 6u IPv4 21170 0t0 TCP *:http (LISTEN)
查看配置文件的加載順序
[root@web01 logs]# /application/nginx/sbin/nginx -T
參數說明:
-T : test configuration, dump it and exit
測試配置文件,並且加載一遍,並顯示加載的順序
需要指定加載順序時候就改成下面的配置
[root@anuo conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include xnzj/anuo1.conf; --通過IP訪問默認是加載第一個
include xnzj/anuo2.conf;
}
重啓、測試結果
[root@anuo conf]# nginx -t
[root@anuo conf]# nginx -s reload
[root@anuo conf]# curl www.anuo1.com
hello anuo1
[root@anuo conf]# curl www.anuo2.com
hello anuo2
[root@anuo conf]# curl 10.0.0.10
hello anuo1
當報403錯誤時,是因爲沒有首頁文件信息
nginx服務搭建文件共享服務器
修改配置文件
[root@anuo html]# vim ../conf/xnzj/anuo1.conf
server {
listen 80;
server_name www.anuo1.com;
location / {
root html/www.anuo1.com;
autoindex on; --配置autoindex on參數會以目錄的形式顯示站點下的文件信息(站點下的首頁文件要刪除)
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx 的訪問認證
修改nginx的相關配置文件
[root@anuo html]# vim ../conf/xnzj/anuo1.conf
server {
listen 80;
server_name www.anuo1.com;
location / {
root html/www.anuo1.com;
autoindex on;
auth_basic "haha"; --指定用戶
auth_basic_user_file /usr/local/nginx/conf/htpasswd;--指定用戶和密碼的存放文件目錄
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重啓系統訪問測試
[root@anuo html]# curl 10.0.0.10
<html>
<head><title>401 Authorization Required</title></head> --報401錯誤
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
(401錯誤說明: 需要認證,但是沒有認證)
下面給用戶創建密碼文件
[root@anuo html]# yum install httpd-tools -y --下載htpasswd命令
[root@anuo html]# touch /usr/local/nginx/conf/htpasswd
[root@anuo html]# htpasswd -c /usr/local/nginx/conf/htpasswd haha ## -c 創建一個新的密碼文
New password:
Re-type new password:
Adding password for user haha
更改密碼文件權限
[root@anuo html]# chmod 400 /usr/local/nginx/conf/htpasswd --修改密碼文件的權限
[root@anuo html]# chown nginx:nginx /usr/local/nginx/conf/htpasswd -R --修改密碼文件的屬主、組爲nginx
[root@anuo html]# cat /usr/local/nginx/conf/htpasswd
haha:7S5ayh6C4PmLk
訪問測試
交互式輸入密碼
[root@anuo html]# curl 10.0.0.10 -uhaha -- -u指定用戶
Enter host password for user 'haha': --輸入用戶的密碼
[root@anuo html]# curl 10.0.0.10 -uhaha:123456 --免交互輸入密碼 (注意用戶和密碼的格式)
rewrite 模塊
rewrite模塊兩個功能
1.實現網站地址信息跳轉
2.實現僞靜態