我的问题类似于Nginx Relative URL to Absolute Rewrite Rule? --但还有一个附加的问题。
我让nginx充当代理服务器,它代理多个应用程序,类似于以下(简化的)配置:
server {
listen 80;
server_name example.com;
location /app1 {
proxy_pass http://app1.com;
}
location /app2 {
proxy_pass http://app2.com;
}
}这很好,但与另一个问题一样,这些应用程序(app1和app2)使用相对urls,如/css/foo.css或/js/bar.js。此外,要求所有应用程序更改为类似于/app1/css/foo.css的应用程序也是一个大问题。
nginx是否有可能智能地确定哪个应用程序应该处理请求?FTR,用户访问这些应用程序的方式如下:
http://example.com/app1/fooaction或http://example.com/app2/baraction.
如果有关系,所有的应用程序都是基于Java/Tomcat的应用程序。
蒂娅!
发布于 2014-12-04 19:09:29
根据您更新的注释;如果上游后端发送引用头,您可以这样做:
location ~* ^/(css|js)/.+\.(css|js)$ {
#checking if referer is from app1
if ($http_referer ~ "^.*/app1"){
return 417;
}
#checking if referer is from app2
if ($http_referer ~ "^.*/app2"){
return 418;
}
}
error_page 417 /app1$request_uri;
error_page 418 /app2$request_uri;
location /app1 {
proxy_pass http://app1.com;
}
location /app2 {
proxy_pass http://app2.com;
}例如,如果app2.com上的后端请求test.css,如下所示:
curl 'http://example.com/css/test.css' -H 'Referer: http://app2.com/app2/some/api'请求在此着陆:
/app2/css/test.css https://stackoverflow.com/questions/27242357
复制相似问题