2017-01-03 18 views
1

私はdotnetコアを使用しています。私はRequest.HttpContext.Connection.RemoteIpAddress.ToString()という行を使ってIPアドレスを取得しています。私のIPアドレスを使用して自分のサイトにアクセスすると、クライアントのIPアドレスが正しく表示されます。しかし、私はhttpsを使いたいと思うし、nginxを使っている。だから私の場所で私は自分のIPアドレスを示しドメインを介して、私のサイトを訪問するとnginxを使用しているときにdotnet coreで実際のクライアントIPアドレスを取得します

proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-Proto https; 
    proxy_set_header X-Forwarded-For $remote_addr; 
    proxy_set_header X-Forwarded-Host $remote_addr; 

を書い::1127.0.0.1(彼らは私が更新するたびに切り替え)など。私は、.NETコアに


server { 
    server_name   www.example.com; 
    listen    443 ssl; 
    ssl_certificate  /etc/letsencrypt/live/www.example.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; 
    root    /var/www/example.com/; 
    add_header   Strict-Transport-Security "max-age=31536000"; 
    index index.html index.htm; 
    log_not_found off; 
    access_log off; 
    #try_files $uri.html $uri $uri/ =404; 
    expires max; 
    default_type text/plain; 
    include /etc/nginx/mime.types; 

    index off; 
    location ~/other/ { 
     index off; 
     autoindex on; 
    } 

    location /abc { 
     proxy_pass http://localhost:5050; 
     proxy_http_version 1.1; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-Proto https; 
     proxy_set_header X-Forwarded-For $remote_addr; 
     proxy_set_header X-Forwarded-Host $remote_addr; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection keep-alive; 
     proxy_set_header Host $host; 
     proxy_cache_bypass $http_upgrade; 
    } 
} 

答えて

1

転送されたヘッダは、デフォルトでは処理されません実際のIPアドレスを伝える方法を正確にはわからない以下の私のnginxの設定です。 HttpOverridesミドルウェアを使用する必要があります。

  • あなたConfigure方法に以下を追加しMicrosoft.AspNetCore.HttpOverrides

  • 依存関係として追加します。

    app.UseForwardedHeaders(new ForwardedHeadersOptions 
        { 
         ForwardedHeaders = ForwardedHeaders.XForwardedFor | 
         ForwardedHeaders.XForwardedProto 
        }); 
    
関連する問題