Nginx 动静分离

配置反向代理
http://192.168.159.104的文件目录

这个服务器只存放html文件 静态文件不存放

在负责转发的Nginx 放置静态文件(css js img fonts等)
http://192.168.159.101 文件目录如下

在这里插入图片描述


worker_processes  1;



events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;


    keepalive_timeout  65;

  

    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://192.168.159.104/;
        }

        #增加每一个location

        #css的文件目录
        location /css {
			root html;
			index index.html index.htm;
		}

		#字体的文件目录
		location /fonts {
			root html;
			index index.html index.htm;
		}

		#图片的文件目录
		location /img {
			root html;
			index index.html index.htm;
		}

		#js的文件目录
		location /js {
			root html;
			index index.html index.htm;
		}
     
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

这样简单的动静分离就配置好了

location / 要比 location /css 优先级底

使用一个location使用正则匹配


worker_processes  1;



events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;


    keepalive_timeout  65;

  

    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://192.168.159.104/;
        }

        #location 使用正则匹配

        #css|fonts|img|js的文件目录
        location ~*/(css|fonts|img|js) {
			root html;
			index index.html index.htm;
		}
		
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

location 前缀

  • / 通用匹配,任何请求都会匹配到。
  • = 精准匹配,不是以指定模式开头
  • ~ 正则匹配,区分大小写
  • ~* 正则匹配,区分大小写
  • ^~ 非正则匹配,匹配以指定模式开头的location

location匹配顺序

  • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配

  • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)

  • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配

  • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

    location ~*/(css|img|js) {
    		root /usr/local/nginx/static;
    		index index.html index.htm;
    	}
    

alias与root

location /css {
	alias /usr/local/nginx/static/css;
	index index.html index.htm;
}

root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。
1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的; 2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的
3)使用alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上"/“符号!!
4)alias虚拟目录配置中,location匹配的path目录如果后面不带”/“,那么访问的url地址中这个path目录后面加不加”/“不影响访问,访问时它会自动加上”/“; 但是如果location匹配的path目录后面加上”/“,那么访问的url地址中这个path目录必须要加上”/“,访问时它不会自动加上”/“。如果不加上”/“,访问就会失败!
5)root目录配置中,location匹配的path目录后面带不带”/",都不会影响访问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值