笔者以笔者自己的项目为例。想达到的目的:
http://by-syk.com:8081/nanoiconpack/watchdog
>>> http://nano.by-syk.com/watchdog
并同时保证原来的访问接口依然能用。
/etc/nginx/sites-enabled/default
旧配置:
server {
listen 8081;
server_name by-syk.com www.by-syk.com;
location / {
proxy_pass http://nodejs_nanoiconpack_servers;
}
}
/etc/nginx/sites-enabled/default
新配置:
server {
listen 80;
server_name nano.by-syk.com;
location / {
proxy_pass http://nodejs_nanoiconpack_servers;
}
}
server {
listen 8081;
server_name by-syk.com www.by-syk.com;
location / {
proxy_pass http://nodejs_nanoiconpack_servers;
}
// 兼容旧路径
location /nanoiconpack/ {
# 例子说明末尾神奇的“/”:
# 对于 http://by-syk.com:8081/nanoiconpack/page/query
# 不加则代理为 http://by-syk.com:8081/nanoiconpack/page/query
# 加上则代理为 http://by-syk.com:8081/page/query
proxy_pass http://nodejs_nanoiconpack_servers/;
# rewrite '^/nanoiconpack(.*)$' $1;
}
}
同时调整服务器接口定义:
/*
app.get('/nanoiconpack/watchdog', function(req, res) {
logger.info('GET /nanoiconpack/watchdog');
res.jsonp(utils.getResRes(0, undefined, {
port: serverPort,
time: Date.now()
}));
});
*/
app.get('/watchdog', function(req, res) {
logger.info('GET /watchdog');
res.jsonp(utils.getResRes(0, undefined, {
port: serverPort,
time: Date.now()
}));
});
调整 HTML 页面内部的调用:
/*
$.get("/nanoiconpack/watchdog", function(data) {
if (data && data.status == 0) {
// ...
}
});
*/
$.get("/watchdog", function(data) {
if (data && data.status == 0) {
// ...
}
});
TODO 这样配置带来了一个附加影响,增加了一个访问通道:http://by-syk.com:8081/watchdog
网友评论