2017-05-05 33 views
0

我有一個運行在Linux上的NGINX反向代理後面的ASP.NET Core網站。我遇到了一個問題,如果我通過代理使用附加的查詢字符串(即緩存清除)執行文件請求,我得到404錯誤,但如果我直接從應用程序請求完全相同的URL(不通過NGINX),它工作正常,如果我刪除查詢字符串,它也可以正常工作。查詢字符串在ASP.NET核心在NGINX下失敗的文件請求

下面

實例(Nginx的代理正在偵聽端口5000和應用程序偵聽端口5002)...

如果我使用一個網址,如: http://host-name:5000/path/file.json

我回到正確的得到的結果,這是對應用程序的控制檯輸出顯示的內容:

[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 
     Request starting HTTP/1.1 GET http://host-name:5000/path/file.json 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://host-name:5000/path/file.json 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2] 
     Sending file. Request path: '/path/file.json'. Physical path: '/home/coreuser/debug/wwwroot/path/file.json' 
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:Information: Sending file. Request path: '/path/file.json'. Physical path: '/home/coreuser/debug/wwwroot/path/file.json' 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] 
     Request finished in 400.9508ms 200 application/json 

如果我使用像網址: http://host-name:5002/path/file.json

我得到的結果返回正確的,這是對應用程序的控制檯輸出顯示的內容:

[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 
     Request starting HTTP/1.1 GET http://host-name:5002/path/file.json 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://host-name:5002/path/file.json 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2] 
     Sending file. Request path: '/path/file.json'. Physical path: '/home/coreuser/debug/wwwroot/path/file.json' 
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:Information: Sending file. Request path: '/path/file.json'. Physical path: '/home/coreuser/debug/wwwroot/path/file.json' 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] 
     Request finished in 28.2031ms 200 application/json 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 28.2031ms 200 application/json 

如果我使用像網址: http://host-name:5002/path/file.json?_dc=1020

我得到的結果返回正確的,這是在應用程序的控制檯輸出顯示的內容:

[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 
     Request starting HTTP/1.1 GET http://host-name:5002/path/bootstrap.json?_dc=1020 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://host-name:5002/path/bootstrap.json?_dc=1020 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2] 
     Sending file. Request path: '/path/file.json'. Physical path: '/home/coreuser/debug/wwwroot/path/file.json' 
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:Information: Sending file. Request path: '/path/file.json'. Physical path:  '/home/coreuser/debug/wwwroot/path/file.json' 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] 
     Request finished in 146.8157ms 200 application/json 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 146.8157ms 200 application/json 

如果我使用一個網址,如: http://host-name:5000/path/file.json?_dc=1020

我得到一個404錯誤,這是對應用程序的控制檯輸出顯示的內容:

[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 
     Request starting HTTP/1.1 GET http://host-name:5000/path/file.json?_dc=1020 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://host-name:5000/path/file.json?_dc=1020 
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] 
     Request finished in 379.4175ms 404 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 379.4175ms 404 

現在,我不是這是否與NGINX什麼它轉發給搞亂的問題明確ASP.NET Core應用程序,或者它不僅僅是ASP.NET Core應用程序(和/或Kestrel)由於查詢字符串和請求上顯示的代理端口號的組合而拋出的問題。

Nginx的配置的相關部分看起來是這樣的:

server { 
    server_name host-name; 
    listen 5000 default_server; 
    listen [::]:5000 default_server; 
    location/{ 
     proxy_pass http://localhost:5002; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection keep-alive; 
     proxy_set_header Host $http_host; 
     proxy_cache_bypass $http_upgrade; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forwarded-Host $http_host; 
     proxy_set_header X-Forwarded-Server $host; 
     proxy_set_header X-Forwarded-Proto $scheme; 
     proxy_set_header X-Forwarded-Path $request_uri; 
    } 
} 

任何想法?

編輯:

因爲我已經修改了我的服務器塊看起來像這樣:

server { 
    server_name host-name; 
    listen 5000 default_server; 
    listen [::]:5000 default_server; 
    root /var/www/path-to-debug/wwwroot; 
    location/{ 
     if ($query_string ~ "^(.*)_dc=(.*)$") { 
      rewrite ^(.*)$ $uri?; 
     } 
     try_files $uri @proxy; 
    } 
    location @proxy { 
     proxy_pass http://localhost:5002; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection keep-alive; 
     proxy_set_header Host $http_host; 
     proxy_cache_bypass $http_upgrade; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forwarded-Host $http_host; 
     proxy_set_header X-Forwarded-Server $host; 
     proxy_set_header X-Forwarded-Proto $scheme; 
     proxy_set_header X-Forwarded-Path $request_uri; 
    } 
} 

現在特定的文件,正在獲取正確(完全繞過紅隼),但似乎正在與我的控制器調用中的一個_dc = XXXX連接。

+0

在你的nginx配置中的任何地方是否有'try_files'指令? – Mardoxx

+0

自從最初提出這個問題以來,我已經添加了一個,但我仍然認爲它不太正確。 – jceddy

回答

0

我已經通過修改我的服務器代理塊來使這部分工作,如下所示...它對我來說就像一個黑客,但我還沒有找到更好的解決方案。我還有其他問題,但如果/當我無法解決問題時,我會爲這些問題單獨提出問題。

server { 
    server_name host-name; 
    listen 5000 default_server; 
    listen [::]:5000 default_server; 
    root /var/www/path-to-debug/wwwroot; 
    location/{ 
     if ($query_string ~ "^(.*)_dc=(.*)$") { 
      rewrite ^(.*)$ $uri?; 
     } 
     try_files $uri @proxy; 
    } 
    location @proxy { 
     proxy_pass http://localhost:5002; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection keep-alive; 
     proxy_set_header Host $http_host; 
     proxy_cache_bypass $http_upgrade; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forwarded-Host $http_host; 
     proxy_set_header X-Forwarded-Server $host; 
     proxy_set_header X-Forwarded-Proto $scheme; 
     proxy_set_header X-Forwarded-Path $uri; 
    } 
}