server配置demo
在192.168.10.140(centos7)上修改:
/home/program/nginx/conf/nginx.conf 添加一個server
server {
listen 80; //監聽一個80端口的應用
server_name www.joyce.com; //應用的虛擬域名或IP(比如localhost)都可
access_log logs/my_access.log
error_log logs/my_error.log
charset utf-8;
error_page 404 = @fallback404 //語法就是@開頭
location /@fallback404 {
proxy_pass http://www.baidu.com; //當404錯誤發生時,服務降級處理
}
location / { //應用的根路徑訪問 root html/domain; //應用的靜態資源文件所在路徑 index index.html; // 如果訪問根路徑/,則訪問頁面index.html
deny all; //拒絕任何請求
allow all; //允許所有請求 } }
cd /home/program/nginx/html
mkdir domain //應用的靜態資源文件所在路徑
cd domain
vi index.html //創建一個訪問頁面index.html
<h1>I'm Joyce! </h1>
cd /home/program/nginx/sbin
./nginx -s reload 重新啟動
在windows本機上修改:
修改host文件,路徑: c:\windows\system32\drivers\etc
添加虛擬域名: 192.168.10.140 www.joyce.com
瀏覽器訪問: http://192.168.10.140 結果顯示html頁面內容:I'm Joyce!
location精准路徑和一般路徑的區別
nginx.conf里面的location配置, 精准匹配的優先級高於一般匹配 。
精准路徑配置:
location = /jingzhun { //精准路徑必須跟上root的文件夾路徑/domain才可訪問index.html root html/domain; //應用的靜態資源文件所在路徑 index index.html; // 如果訪問根路徑/,則訪問頁面index.html }
一般路徑配置:
location /yiban { //一般路徑無需跟上root的文件夾路徑/domain即可訪問index.html root html/domain; //應用的靜態資源文件所在路徑 index index.html; // 如果訪問根路徑/,則訪問頁面index.html }
uri前后都加上/或都不加/,精准匹配才優先於一般匹配。
location的rewrite配置
location /abc { rewrite '/images/[a-z]3/(.*)\.(png|jpg)' /jack?file=$2.$3; #路徑跳轉。用索引第二位參數和索引第三位參數,索引從0開始。跳轉到下一個url } location /abc2 { #跳轉到下一個url root html; try_files $uri /image404.html; } location /images404.html { return 404 "image not found exception"; #直接輸出不存在 }