myesn

myEsn2E9

hi
github

ASP.NET Core: nginx 反向代理後丟失客戶端 IP

nginx 網站配置中添加如下指令:

server {

    # 反向代理
    location / {
        proxy_pass            http://127.0.0.1:5000;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header Forwarded         $proxy_add_forwarded;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host  $host;
        proxy_set_header X-Forwarded-Port  $server_port;
    }
}

nginx 修改配置後記得 nginx -s reload

在 ASP.NET Core 程式中添加以下中間件:

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

配置完成後,按照原來的方式就可以拿到客戶端 IP 了,中間件會處理好適配問題。

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。