在之前的开发中,我讲开发好的程序打包放到服务器上,然后通过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加入。