远程的Angular + .NET Core ERR_CONNECTION_REFUSED

问题描述

我有一个.NET Core后端,一个Angular前端和NGNIX作为反向代理。我在Windows 10下运行该系统,并在其中使用FolderProfile发布了后端,将其复制到PC并通过exe启动。前端的网络服务器是IIS。我通过命令行启动NGNIX。

如果我在本地PC的浏览器中调用前端,则一切正常。我可以通过HTTP:// localhost:4200成功访问并使用该页面,正确的URL为HTTP://test.example.net:4200和HTTPS://test.example.net。

如果现在从远程PC调用页面,则会在浏览器控制台中看到“无法加载资源:net :: ERR_CONNECTION_REFUSED”。将显示“角度”页面,但与后端的通信似乎不起作用。我使用HTTP://test.example.net:4200还是HTTPS://test.example.net都没关系。防火墙中的端口(443、4200、5000)已打开。

在后端,我做了以下调整:

virtual public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy(name: MyAllowSpecificOrigins,builder =>
                            {
                                builder.AllowAnyOrigin();
                                builder.AllowAnyHeader();
                                builder.AllowAnyMethod();
                            });
    });
    
    services.AddMvc().SetCompatibilityVersion(...);
    ...
}

virtual public void Configure(IApplicationBuilder app,IHostEnvironment env)
{
    ...
    app.UseCors(MyAllowSpecificOrigins);
    app.UseHttpsRedirection();
    
    app.UseAuthentication();

    app.UseMvc();
    ...
}

这是前端中的web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Headers" value="*" />
      <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Credentials" value="true" />    
    </customHeaders>
  </httpProtocol>
  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>    

这是Nginx.conf:

worker_processes  1;

events { 
}

http {

  map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }
    
  upstream test.example.net {
    server 127.0.0.1:4200;
    #server test.example.net:4200;
    #server test.example.net;
  }
   
  server {
    listen 443 ssl; 
    
    server_name test.example.net;
    keepalive_timeout   70;
    
    ssl_certificate C:\\Nginx-1.18.0\\conf\\certs\\cert.crt;
    ssl_certificate_key C:\\Nginx-1.18.0\\conf\\certs\\cert.key;
    
    ssl_prefer_server_ciphers on;
    ssl_session_timeout 10m;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;

    # intermediate configuration. tweak to your needs.
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';

    
    location / {
        proxy_pass http://test.example.net;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
  }
}

远程PC上的浏览器显示以下内容

enter image description here

我希望有人知道问题出在哪里

解决方法

根据浏览器中显示的错误,客户端应用程序无法连接后端Web服务器。
我们需要将应用的URL更改为实际的URL地址,而不是本地主机。 尝试将角度应用程序中的URL更改为指向实际域名而不是localhost:4200,以及IIS中的配置。
https://support.aspnetzero.com/QA/Questions/6395/Failed-to-load-resource-netERRCONNECTIONREFUSED
随时让我知道问题是否仍然存在。