nginx中location配置resful显示css、js

在之前的开发中,我讲开发好的程序打包放到服务器上,然后通过nginx代理一下,在浏览器输入“http://hellobyte.cn”可以直接访问到我的程序,我之前的nginx是这么配置的:

server {
	listen 80;
	server_name www.hellobyte.cn hellobyte.cn;
	location / {
			proxy_pass http://127.0.0.1:8081/;
	}
}

但是现在呢现在想增加一个二级域名“focus.hellobyte.cn”,想访问到“http://127.0.0.1:8081/edu”这个路径下,然后我又将上面的配置复制了一遍增加上去,

server {
	listen 80;
	server_name www.hellobyte.cn hellobyte.cn;
	location / {
			proxy_pass http://127.0.0.1:8081/;
	}
}

server {
	listen 80;
	server_name focus.hellobyte.cn
	location / {
			proxy_pass http://127.0.0.1:8081/edu;
	}
}

在浏览器“http://focus.hellobyte.cn”发现程序是能访问的,但是css、js却访问不了,经过排查发现我的css、js的跟路径是“/“,但是我在做proxy_pass的时候将跟路径指向了”/edu“,因此就找不到css、js了,后来在文章http://outofmemory.cn/code-snippet/742/nginx-location-configuration-xiangxi-explain发现location中”/“表示的是通用规则(原来我一直都是配置错误的),经过改成后的配置如下:

server {
	listen 80;
	server_name www.hellobyte.cn hellobyte.cn;
	location = / {
			proxy_pass http://127.0.0.1:8081/;
	}
}

 server {
	listen 80;
	server_name focus.hellobyte.cn;
	location = / {
		proxy_pass http://127.0.0.1:8081/edu;
	}
	location / {
		proxy_pass http://127.0.0.1:8081/;
	}
}

第一个server很容易理解,当访问“http://hellobyte.cn”的时候跳转到“http://127.0.0.1:8081/”页面;第二个的意思是当访问“focus.hellobyte.cn”的时候跳转到“http://127.0.0.1:8081/edu”页面,但是当页面做请求的css、js没有的时候,再从“http://127.0.0.1:8081/”这个路径下去找,这样配置就ok了,css、js全部正常显示。点击查看更多


---------------------------------------------------

参考文章:http://outofmemory.cn/code-snippet/742/nginx-location-configuration-xiangxi-explain

群:【518397333】欢迎求职者、HR、C#,PHP,Java,C++,IOS,Android,.NET,DBA,UI加入。


转载于:https://my.oschina.net/funmo/blog/614404

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值