nginx 反向代理

引自:反向代理
在电脑网络中,反向代理是代理服务器的一种。服务器根据客户端的请求,从其关系的一组或多组后端服务器(如Web服务器)上获取资源,然后再将这些资源返回给客户端,客户端只会得知反向代理的IP地址,而不知道在代理服务器后面的服务器簇的存在。

根据域名转发请求

这个功能适用于希望一台服务器部署多个应用,并通过不同的域名进行访问的朋友。

nginx 的http配置文件一般是nginx文件目录下的conf/nginx.conf

假设有两个应用,分别是

域名端口
app1.example.com8001
app2.example.com8002

那么,对应的 nginx http配置如下

http {
    ...
    server {
        listen 80;
        server_name app1.example.com;
        location / {
            proxy_pass http://localhost:8001;
        }
    }

    server {
        listen 80;
        server_name app2.example.com;
        location / {
            proxy_pass http://localhost:8002;
        }
    }
}

以上配置就会根据域名选择不同的端口进行请求,当访问app1.example.com就会转发到localhost:8001,访问app2.example.com就会转发到localhost:8002,就实现了反向代理。
这样的方式还可以很好的隐藏服务器开放的端口,只需要把80端口开放,对于其他端口都通过转发来访问。

注意
不要忘记对域名添加解析

根据路径转发请求

这个已有前辈整理的很好了,我就不再赘言了,以下内容转载自nginx实现请求转发

反向代理适用于很多场合,负载均衡是最普遍的用法。

nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理。

nginx 反向代理官方文档: NGINX REVERSE PROXY

当在一台主机上部署了多个不同的web服务器,并且需要能在80端口同时访问这些web服务器时,可以使用 nginx 的反向代理功能: 用 nginx 在80端口监听所有请求,并依据转发规则(比较常见的是以 URI 来转发)转发到对应的web服务器上。

例如有webmail , webcom 以及 webdefault 三个服务器分别运行在 portmail , portcom , portdefault 端口,要实现从80端口同时访问这三个web服务器,则可以在80端口运行 nginx, 然后将 /mail 下的请求转发到 webmail 服务器, 将 /com下的请求转发到 webcom 服务器, 将其他所有请求转发到 webdefault 服务器。

假设服务器域名为example.com,则对应的 nginx http配置如下:

http {
    server {
        server_name example.com;

        location /mail/ {
            proxy_pass http://example.com:protmail/;
        }

        location /com/ {
            proxy_pass http://example.com:portcom/main/;
        }

        location / {
            proxy_pass http://example.com:portdefault;
        }
    }
}

以上的配置会按以下规则转发请求( GET 和 POST 请求都会转发):

http://example.com/mail/下的请求转发到 http://example.com:portmail/
http://example.com/com/下的请求转发到 http://example.com:portcom/main/
将其它所有请求转发到 http://example.com:portdefault/
需要注意的是,在以上的配置中,webdefault 的代理服务器设置是没有指定URI的,而 webmailwebcom 的代理服务器设置是指定了URI的(分别为 //main/)。
如果代理服务器地址中是带有URI的,此URI会替换掉 location 所匹配的URI部分。
而如果代理服务器地址中是不带有URI的,则会用完整的请求URL来转发到代理服务器。

官方文档描述:

If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter.
If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

以上配置的转发示例:

  • http://example.com/mail/index.html -> http://example.com:portmail/index.html
  • http://example.com/com/index.html-> http://example.com:portcom/main/index.html
  • http://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpg
  • http://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.css
  • http://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值