nginx1.14.0版本server、location、rewrite配置
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";    #直接输出不存在
}
 
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号