nginx1.14.0版本server、location、rewrite配置

server配置demohtml

在192.168.10.140(centos7)上修改:nginx

/home/program/nginx/conf/nginx.conf 添加一個serverwindows

    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/htmlcentos

mkdir domain        //應用的靜態資源文件所在路徑瀏覽器

cd domain dom

vi index.html         //建立一個訪問頁面index.htmlurl

<h1>I'm Joyce! </h1>

cd /home/program/nginx/sbincentos7

./nginx -s reload   從新啓動spa

在windows本機上修改:code

修改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";    #直接輸出不存在
}
相關文章
相關標籤/搜索