[英]How to serve two web applications behind an nginx reverse proxy
I have two web applications (node.js express apps), web1
and web2
. 我有两个Web应用程序(node.js表达应用程序), web1
和web2
。 These web apps expect to be hosted on sites that are typically something like http://www.web1.com
and http://www.web2.com
. 这些网络应用程序希望托管在通常类似http://www.web1.com
和http://www.web2.com
。 I'd like to host them behind an nginx reverse proxy as https://www.example.com/web1
and https://www.example.com/web2
. 我想将它们作为https://www.example.com/web1
和https://www.example.com/web2
托管在nginx反向代理后面。 I do not want to expose the two web apps as two subdomains on example.com
. 我不想将这两个网络应用程序公开为example.com
上的两个子域。
Here is a snippet of my nginx configuration (without SSL termination details) that I had hoped would accomplish this: 这是我的nginx配置的片段(没有SSL终止详细信息),我希望可以实现这一点:
server {
listen 443;
server_name .example.com;
location /web1 {
proxy_pass http://www.web1.com:80;
}
location /web2 {
proxy_pass http://www.web2.com:80;
}
}
This works, except for the relative links that the web apps use. 除了Web应用程序使用的相对链接外,此方法有效。 So web app web1
will have a relative link like /js/script.js
which won't be handled correctly. 因此,web应用程序web1
将具有像/js/script.js
这样的相对链接,这将无法正确处理。
What is the best/standard way to accomplish this? 完成此任务的最佳/标准方法是什么?
I think something like this: server { listen 443; server_name .example.com; location /web1 { proxy_pass http://www.web1.com:80 ; } location /web2 { proxy_pass http://www.web2.com:80 ; } location / { if ($http_referer ~* (/web1) ) { proxy_pass http://www.web1.com:80 ; } if ($http_referer ~* (/web2) ) { proxy_pass http://www.web2.com:80 ; } } }
我认为这样的事情: server { listen 443; server_name .example.com; location /web1 { proxy_pass http://www.web1.com:80 ; } location /web2 { proxy_pass http://www.web2.com:80 ; } location / { if ($http_referer ~* (/web1) ) { proxy_pass http://www.web1.com:80 ; } if ($http_referer ~* (/web2) ) { proxy_pass http://www.web2.com:80 ; } } }
server { listen 443; server_name .example.com; location /web1 { proxy_pass http://www.web1.com:80 ; } location /web2 { proxy_pass http://www.web2.com:80 ; } location / { if ($http_referer ~* (/web1) ) { proxy_pass http://www.web1.com:80 ; } if ($http_referer ~* (/web2) ) { proxy_pass http://www.web2.com:80 ; } } }
You should be able to do this by checking the $http_referer
, something like: 您应该可以通过检查$http_referer
来执行此$http_referer
,例如:
location / {
if ($http_referer ~ ^http://(www.)?example.com/web1) {
proxy_pass http://www.web1.com:80;
}
if ($http_referer ~ ^http://(www.)?example.com/web2) {
proxy_pass http://www.web2.com:80;
}
}
The browser would be setting the referer to http://example.com/web1/some/page
when it requests /js/script.js
so the apps shouldn't need to change, unless they need to process or care about the referer internally. 当浏览器请求/js/script.js
时,浏览器会将引用设置为http://example.com/web1/some/page
,因此应用程序不需要更改,除非他们需要处理或关注引用者内部。
The $http_referer does not seem to be easy to find in nginx docs, but is mentioned in a few sites: $ http_referer在nginx文档中似乎不容易找到,但在一些网站中提到:
How about using cookie and ngx_http_map_module
? 使用cookie和ngx_http_map_module
怎么样?
Add add_header Set-Cookie "site=web1;Path=/;Domain=.example.com;";
添加add_header Set-Cookie "site=web1;Path=/;Domain=.example.com;";
to location /web1 {...}
(web2 too). 到location /web1 {...}
(也是web2)。
Add map
to under http
将map
添加到http
map $cookie_site $site {
default http://www.web1.com:80;
"web2" http://www.web2.com:80;
}
Default location is this 默认位置是这个
location / {
proxy_pass $site;
}
You can pass the value of cookie to proxy_pass
directly. 您可以直接将cookie的值传递给proxy_pass
。 But, using map is more secure way. 但是,使用地图是更安全的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.