就是:我們打開HttpWatch看到的那個HOST值。
server {
listen 80;
server_name nginx.org www.nginx.org;
...
}
server {
listen 80;
server_name nginx.net www.nginx.net;
...
}
server {
listen 80;
server_name nginx.com www.nginx.com;
...
}
這樣的話我們就可以配置三個域名。即同一個IP綁定三個域名。如果發(fā)現(xiàn)有一個域名均不匹配的話就定義出來一個默認(rèn)的域名
server {
listen 80 default_server;
server_name nginx.net www.nginx.net;
...
}
對于這種域名我們可以這樣來處理
server {
listen 80 default_server;
server_name www.nginx.net; //這個值你得填寫一個
return 444;
}
基于域名與IP混用的虛擬主機(jī)
server {
listen 192.168.1.1:80;
server_name nginx.org www.nginx.org;
...
}
server {
listen 192.168.1.1:80;
server_name nginx.net www.nginx.net;
...
}
server {
listen 192.168.1.2:80;
server_name nginx.com www.nginx.com;
...
}
至此配置完成了有關(guān)虛擬機(jī)的配置工作!
示例:
Server {
Listen 80;
Server_name nginx.org www.nginx.org;
Root /data/www; //這個有點相當(dāng)于resin里面的root目錄
Location / {
Index index.html index.php;
}
Location ~*\.(gif|jpg|png)$ {
Expires 30d;
}
Location ~\.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
其中的location為”/” 表示的是它可以匹配任何請求的。
哦!原來location是用來檢驗URI的!
心得與筆記:
我們的server是配置HOST的即主機(jī)。
Location是配置URI的。
比如:http://www.sina.cn/blog/index.php 那這里面的HOST就是www.sina.cn
URI就是我們的/blog/index.php值了。
一個“/logo.gif”請求會先和字符location“/”匹配,然后再和正則表達(dá)式“\.(gif|jpg|png)$”匹配, 因此,它是被字符location處理的。指令“root /data/www”會使該請求指向一個文件 “/data/www/logo.gif”,之后這個文件就會發(fā)送到客戶端。
哦原來root的作用其實與resin里面的document-root是一個概念的!
一個 “/index.php”請求同樣先被字符location “/” 匹配,然后才被正則表達(dá)式“\.(php)$”匹配。 所以, 它是被字符location所處理的,并且這請求是通過一個監(jiān)聽在localhost:9000的FastCGI server被處理的. “fastcgi_param” 指令設(shè)置FastCGI的參數(shù)SCRIPT_FILENAME設(shè)置為“/data/www/index.php”, FastCGI server 執(zhí)行這個文件. $document_root 變量的值等于 “root” 指令,$fastcgi_script_name 變量等于 URI 請求的值, 也就是 “/index.php”.
筆記:nginx是讓客戶端程序找到文件的目錄位置。具體如何處理這個得讓后端來處理的
一個 “/about.html”請求只被字符location“/”匹配, 所以,它被這個location處理。 使用“root /data/www” 指令的時候,該請求會被轉(zhuǎn)到 “/data/www/about.html”, 并且文件會被發(fā)送到客戶端。
明白了!
筆記:location是得講個先后順序才行的。即先由 location / 處理讓客戶端找到所需要的文件。然后再往下找看看是否還有匹配的location項如果像php文件就會有了!
丟給了一個FAST-CGI處理程序
總結(jié):
心得與筆記:
我們的server是配置HOST的即主機(jī)。多個域名就定義多個虛擬主機(jī)即可
Location是配置URI的。
比如:http://www.sina.cn/blog/index.php 那這里面的HOST就是www.sina.cn
URI就是我們的/blog/index.php值了。
Location是多方匹配的。示例:
Location / {
Index index.html index.php;
}
Location ~*\.(gif|jpg|png)$ {
Expires 30d;
}
如果我請求一個abc.gif的話是先由第一個UIR定位找到圖片位置再由第二個URI處理得到過期時間。
當(dāng)然在location里面有以下幾個選項。
1、last 基本上用這個。表示已完成了rewrite不再匹配后面的規(guī)則了
2、break 中止rewrite不再繼續(xù)匹配
3、redirect 返回臨時重定向的HTTP狀態(tài)302
4、permanent 返回永久重定向的HTTP狀態(tài)301
注意:原有的URL支持正則,重寫的URL不支持正則
Location / {
Index index.html index.php;
Break;
}
則后面的過期限制就不生效
手工測試一下:只處理靜態(tài)文件的情況
站點目錄:
虛擬主機(jī)1:目錄放在D:\myweb\proj3 下面
虛擬主機(jī)2:目錄放在D:\myweb\proj4 下面
server {
listen 80;
server_name www.aaa.com;
root D:\myweb\proj3;
location / {
index index.html index.htm;
}
location ~*\.(gif|jpg|png)$ {
expires 30d;
}
}
server {
listen 80;
server_name www.bbb.com;
root D:\myweb\proj4;
location / {
index index.html index.htm;
}
location ~*\.(gif|jpg|png)$ {
expires 30d;
}
}
OK!配置了兩個虛擬主機(jī)了。到時只要域名一過來就可以解析。