Asp.net Core获取客户端的远程IP总是返回127.0.0.1

时间:2019-11-21 08:59:12

标签: c# nginx asp.net-core

我的环境: Ubuntu 18.04,Asp.net Core 2.1,Nginx

我遵循了this教程。 我在Startup.cs中添加了此代码:

<T3DataStructure>
    <meta>
        <langDisable>1</langDisable>
    </meta>
    <ROOT type="array">
        <type>array</type>
        <el type="array">
        <bgimage>
            <TCEforms>
        <label>Background Image</label>
                <config>
                    <type>inline</type>

                    <foreign_field>uid_foreign</foreign_field>
                    <foreign_label>uid_local</foreign_label>
                    <foreign_match_fields type="array">
                        <fieldname>bgimage</fieldname>
                    </foreign_match_fields>
                    <foreign_selector>uid_local</foreign_selector>
                    <foreign_selector_fieldTcaOverride type="array">
                        <config type="array">
                            <appearance type="array">
                                <elementBrowserAllowed>gif,jpg,jpeg,tif,tiff,bmp,pcx,tga,png,pdf,ai</elementBrowserAllowed>
                                <elementBrowserType>file</elementBrowserType>
                            </appearance>
                        </config>
                    </foreign_selector_fieldTcaOverride>
                    <foreign_sortby>sorting_foreign</foreign_sortby>
                    <foreign_table>sys_file_reference</foreign_table>

                    <maxitems>1</maxitems>
                    <minitems>0</minitems>
                </config>
            </TCEforms>
        </bgimage>
        </el>
    </ROOT>
</T3DataStructure>

我配置了我的Nginx配置:

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

我通过以下方式获取远程IP:

listen *:443 ssl http2;

location / {
        proxy_pass https://localhost:6001;
        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-Proto-Version $http2;
        client_max_body_size 32m;
        keepalive_timeout 200;
        send_timeout 20;
        client_body_timeout 50;
        proxy_set_header  X-Forwarded-For $remote_addr;
    }

但是它总是从任何IP返回127.0.0.1。

3 个答案:

答案 0 :(得分:1)

我个人必须手动获取标头值。这是由于云设置。也许这会对您有所帮助。

if (Request.Headers.TryGetValue("X-Forwarded-For", out var forwardedIps))
    senderIpv4 = forwardedIps.First();

答案 1 :(得分:0)

您在nginx配置中忘记了proxy_set_header X-Forwarded-Proto $scheme;

答案 2 :(得分:0)

指定反向代理IP或清除。

    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders =ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        options.KnownNetworks.Clear();
        options.KnownProxies.Clear();
    });
    ...
    app.UseForwardedHeaders();

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-3.1