Nginx根据cookie值返回静态文件或代理请求



我有两个应用程序,一个是nodejs,另一个是react应用程序。我用户未登录,我返回节点应用程序,如果用户已登录,我想返回react应用程序index.html文件。

location / {
if ($cookie_isLoggedIn = "true") {
// how can I return the index.html file here ?
}
proxy_pass http://localhost:3000;
}

到目前为止我尝试了什么:

  1. rewrite ^/$ /platform-build/index.html;-什么都不做
  2. CCD_ 3和CCD_ 4在CCD_

您需要使用两个不同的内容处理程序(proxy_passtry_files(,因此需要两个不同位置。您可以通过等指令返回一些静态HTML内容

return 200 "<body>Hello, world!</body>";

但我认为它不能满足你的需要。然而,您可以使用以下技巧(取自此答案(:

map $cookie_isLoggedIn $loc {
true    react;
default node;
}
server {
...
location / {
try_files /dev/null @$loc;
}
location @react {
root /your/react/app/root;
index index.html;
try_files $uri $uri/ /index.html;
}
location @node {
proxy_pass http://localhost:3000;
}
}

原创特技的作者说它对表演没有任何影响。

最新更新