loic-sharma / BaGet

A lightweight NuGet and symbol server
https://loic-sharma.github.io/BaGet/
MIT License
2.61k stars 669 forks source link

Reverse proxy and load balancer NOT supported #725

Open xiaoliyu opened 2 years ago

xiaoliyu commented 2 years ago

Environment

Windows, URLRewrite, IIS

Issues

After checked the source code, The following files should be changed. I have verified the changes and it worked well in my environment. AddBaGetWebApplication method in IServiceCollectionExtensions.cs ` public static IServiceCollection AddBaGetWebApplication( this IServiceCollection services, Action configureAction) { services .AddRouting(options => options.LowercaseUrls = true) .AddControllers() .AddApplicationPart(typeof(PackageContentController).Assembly) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; }); // Enable reverse proxy and load balancer support services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.All; });

        services.AddRazorPages();

        services.AddHttpContextAccessor();
        services.AddTransient<IUrlGenerator, BaGetUrlGenerator>();

        services.AddBaGetApplication(configureAction);

        return services;
    }`

AbsoluteUrl method in BaGetUrlGenerator.cs

` private string AbsoluteUrl(string relativePath) { var request = _httpContextAccessor.HttpContext.Request;

        string forwardedHost = request.Headers["HTTP_X-Forwarded-Host"];
        var proto = request.Headers["HTTP_X-Forwarded-Proto"].FirstOrDefault()??"https";
        if(!string.IsNullOrEmpty(forwardedHost))
        {
            return proto+"://"+forwardedHost + request.PathBase.ToUriComponent() + "/" + relativePath;
        }

        return string.Concat(
            request.Scheme,
            "://",
            request.Host.ToUriComponent(),
            request.PathBase.ToUriComponent(),
            "/",
            relativePath);
    }`