简体   繁体   English

如何使用nginx作为跨域的反向代理

[英]how to use nginx as reverse proxy for cross domains

I need to achieve below test case using nginx:我需要使用 nginx 实现以下测试用例:

www.example.com/api/ should redirect to ABC.com/api , while www.example.com/api/site/login should redirect to XYZ.com/api/site/login www.example.com/api/应该重定向到ABC.com/api ,而www.example.com/api/site/login应该重定向到XYZ.com/api/site/login

But in the browser, user should only see www.example.com/api.... (and not the redirected URL).但是在浏览器中,用户应该只能看到www.example.com/api.... (而不是重定向的 URL)。

Please let me know how this can be achieved.请让我知道如何实现这一点。

The usage of ABC.com is forbidden by stackoverflow rules , so in example config I use domain names ABC.example.com and XYZ.example.com : stackoverflow 规则禁止使用 ABC.com,因此在示例配置中我使用域名ABC.example.comXYZ.example.com

server {

    ...

    server_name www.example.com;

    ...

    location /api/ {
        proxy_set_header Host ABC.example.com;
        proxy_pass http://ABC.example.com;
    }

    location /api/site/login {
        proxy_set_header Host XYZ.example.com;
        proxy_pass http://XYZ.example.com;
    }

    ...

}

(replace http:// with https:// if needed) (如果需要,将 http:// 替换为 https://)

The order of location directives is of no importance because, as the documentation states , the location with the longest matching prefix is selected. location指令的顺序并不重要,因为正如文档所述,选择具有最长匹配前缀的位置。

With the proxy_set_header parameter, nginx will behave exactly in the way you need, and the user will see www.example.com/api... Otherwise, without this parameter, nginx will generate HTTP 301 redirection to ABC.example.com or XYZ.example.com.使用proxy_set_header参数,nginx 将完全按照您需要的方式运行,用户将看到 www.example.com/api... 否则,如果没有此参数,nginx 将生成 HTTP 301 重定向到 ABC.example.com 或 XYZ .example.com。

You don't need to specify a URI in the proxy_pass parameter because, as the documentation states , if proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed.您不需要在proxy_pass参数中指定 URI,因为正如文档所述,如果在没有 URI 的情况下指定了 proxy_pass,则请求 URI 将以与客户端发送原始请求时相同的形式传递给服务器处理。

You can specify your servers ABC.example.com and XYZ.example.com as domain names or as IP addresses.您可以将服务器 ABC.example.com 和 XYZ.example.com 指定为域名或 IP 地址。 If you specify them as domain names, you need to specify the additional parameter resolver in your server config.如果将它们指定为域名,则需要在服务器配置中指定附加参数resolver You can use your local name server if you have one, or use something external like Google public DNS (8.8.8.8) or DNS provided for you by your ISP:如果您有本地名称服务器,您可以使用它,或者使用外部名称服务器,例如 Google 公共 DNS (8.8.8.8) 或您的 ISP 为您提供的 DNS:

server {

    ...

    server_name www.example.com;
    resolver 8.8.8.8;

    ...

}

Try this:尝试这个:

location /api {
    proxy_pass http://proxiedsite.com/api;
}

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client.当 NGINX 代理请求时,它将请求发送到指定的代理服务器,获取响应,并将其发送回客户端。 It is possible to proxy requests to an HTTP server (another NGINX server or any other server) or a non-HTTP server (which can run an application developed with a specific framework, such as PHP or Python) using a specified protocol.可以使用指定的协议将请求代理到 HTTP 服务器(另一个 NGINX 服务器或任何其他服务器)或非 HTTP 服务器(可以运行使用特定框架开发的应用程序,例如 PHP 或 Python)。 Supported protocols include FastCGI, uwsgi, SCGI, and memcached.支持的协议包括 FastCGI、uwsgi、SCGI 和 memcached。

To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location.为了将请求传递到 HTTP 代理服务器,proxy_pass 指令在一个位置内指定。

Resource from NGINX Docs来自NGINX 文档的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM