• why use reverse proxy in asp.net core


    开篇论点

    Asp.net Core自带了Kestrel, 为什么我们还要使用诸如IIS、Apache或者Nginx来做反向代理呢?

    原因分析

    • Kestrel用来承载Asp.net Core的动态内容是没有任何问题的, 但是它的网页承载能力或者功能没有IIS、Apache或者Nginx那么强.
    • 使用反向代理之后, Kestrel可以把静态内容的缓存、请求的压缩以及终止等工作交给反向代理服务器来做.

    安装位置

    通常来说, 反向代理服务可以跟应用程序安装在同一个服务器, 也可以单独使用一台服务器.

    使用方法

    配置应用

    因为使用了反向代理, 请求的头部会在这个过程中发生一些转变, 比如获取请求者IP之类的就会出错, 因为需要添加如下中间件:

    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });
    
    app.UseAuthentication();
    

    然后需要将受信任的代理服务器IP添加到白名单:

    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.KnownProxies.Add(IPAddress.Parse("10.0.0.100"));
    });
    

    使用代理

    安装过程就不多说了. 下面例举一下配置示例(Nginx在Ubuntu):

    修改/etc/nginx/sites-available/default的内容如下:

    server {
        listen        80;
        server_name   example.com *.example.com;
        location / {
            proxy_pass         http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }
    

    具体请参考微软官方文档: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.0

  • 相关阅读:
    模板语法 DTL(Django Template Language )
    django基础
    day1,基本标签总结
    聚合函数
    day1
    day 3 定时任务
    day 4 tar
    day 6
    day1 mysql
    day 6
  • 原文地址:https://www.cnblogs.com/jerryqi/p/11803874.html
Copyright © 2020-2023  润新知