2017-05-05 31 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; 
    } 
}