Skip to content

正向代理

用户访问不到外网,通过中间加一个 nginx 代理服务器,请求转发给代理服务器,由代理服务器访 问外网并将结果返回给用户。代理服务器是我们自己提供的。

反向代理

用户访问不到应用服务器,通过内网机房的网关将请求转发给 nginx 代理服务器,代理服务器将 请求转发给 应用服务器,获取结果后再将请求转发给网关,网关再转发给用户。代理服务器是别人 内部提供的。

上述过程被称为 nginx 隧道式模型,当遇到高并发时,性能会受到 nginx 的带宽影响。解决方法 是返回结果时直接由应用服务器返回给网关,不走代理。称为 DR 模型

nginx
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        # 填写域名或 IP 地址
        server_name  www.rainx.top;

        location / {
            # 跨域
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST';
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 3600;
                return 204;
            }

            # 反向代理,需要确保访问该网站时不会重定向,否则重定向后请求就不会经过
            # nginx 服务器了,另外不支持反向代理到 https。
            proxy_pass http://192.168.44.102;

            # 代理到本地 node 服务器
            # proxy_pass http://127.0.0.1:8080;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}