NGINX 可以同时作为 Web 服务器和代理服务器吗?
Can NGINX work as Webserver and Proxy Server At Same Time?
我们有这样一种情况,我们需要从 server_A (NGINX) 提供登录页面,从这里对用户进行身份验证,一旦通过身份验证,就将请求路由到驻留在不同服务器中的页面,即 Server_B。所有后续请求都会到达 Server_A,它会首先检查用户和会话有效性,然后路由到 Server_B。这样,会话和安全性由服务器 A 维护,其余工作由服务器 B 完成。
我的问题是我们可以从 NGINX 服务器实现这个吗?
My question here is that can we acheive this from NGINX server ?
是的,当然。
你是怎么做到的?
以修改后的 Tomcat 为例,我手头有 NGINX 应用程序配置:
server {
listen 80;
server_name www.example.com;
location /{
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://servera.example.com:8080/app;
client_max_body_size 10M;
}
location /login{
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://serverb.example.com:8080/login;
client_max_body_size 10M;
}
}
这会将请求代理到 TomCat 服务器,并像任何 HTTP 服务器一样为它们提供服务。
同样适用于您可能想要代理的任何应用程序,只需相应地修改上述配置的 proxy_pass
行!
我们有这样一种情况,我们需要从 server_A (NGINX) 提供登录页面,从这里对用户进行身份验证,一旦通过身份验证,就将请求路由到驻留在不同服务器中的页面,即 Server_B。所有后续请求都会到达 Server_A,它会首先检查用户和会话有效性,然后路由到 Server_B。这样,会话和安全性由服务器 A 维护,其余工作由服务器 B 完成。
我的问题是我们可以从 NGINX 服务器实现这个吗?
My question here is that can we acheive this from NGINX server ?
是的,当然。
你是怎么做到的?
以修改后的 Tomcat 为例,我手头有 NGINX 应用程序配置:
server {
listen 80;
server_name www.example.com;
location /{
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://servera.example.com:8080/app;
client_max_body_size 10M;
}
location /login{
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://serverb.example.com:8080/login;
client_max_body_size 10M;
}
}
这会将请求代理到 TomCat 服务器,并像任何 HTTP 服务器一样为它们提供服务。
同样适用于您可能想要代理的任何应用程序,只需相应地修改上述配置的 proxy_pass
行!