简体   繁体   中英

Get “502 - Web server received an invalid response while acting as a gateway or proxy server” error when opening register page on Azure

I have been playing around with Azure and am looking to publish my .net core 2 application there. So far, this builds on my local machine fine. I can register as a user, so I know everything is okay, locally. I can see users and have even managed to register certain users with certain claims.

I can publish the site to azure:

https://mytrade20180517093224.azurewebsites.net/

I can also log into the azure database from vs2017 using the same credentials I supplied in my appsettings.json. However, the problem I'm getting is my azure site then falls over when you go to register:

https://mytrade20180517093224.azurewebsites.net/Account/Register

I get:

502 - Web server received an invalid response while acting as a gateway or proxy server.

There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

The authentication type I have is the "individual user accounts" one, I have re-created the tables for this on the azure db I'm referencing. In the startup.cs I'm calling the "DefaultConnection" string if the environment is development and if its not (which I'm guessing Azure wont be by default) calling another connection string, the azure one:

if (HostingEnvironment.IsDevelopment())
{
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
else
{
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("azure")));
}   

Does azure require something different to be done to pick the connection string? I tried to switch on the some kind of logging on azure but this didn't seem to make any difference. Any one, any clues as to what I could be potentially doing wrong?

Yes, on azure you need to provide connection string differently Login on portal.azure.com Go to your Web App -> Settings -> Application Settings -> Connection Strings Here you add new connection string with name in your case azure

** Azure连接设置**

From your description, you would dynamiclly choose a connection string based on Environment, so I test and here is main steps, please refer to it.

  1. Set the ASPNETCORE_ENVIRONMENT value to azure in webapp>property>debug.

在此处输入图片说明

2.Follow ASP.NET Core MVC with Entity Framework Core to get started.

3.Set the appsetting.json with your two connection string.

{
  "ConnectionStrings": {
    "DefaultConnection": "connectiondefault",
    "azure": "connectionazure"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

Note :You could also set the connectionstring in database on portal to here, then you could test it in local and could use debug to troubleshooting.

Also, you could try to test with one connectionstring to ensure you have no problem with connecting to database.

4.Enable Developer exception page by using app.UseDeveloperExceptionPage(); and the app.UseExceptionHandler methods in your startup class which would display the errors.

        public Startup(IHostingEnvironment env)
        {
            Configuration = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .Build();

            HostingEnvironment = env;
        }

        public IConfigurationRoot Configuration { get; }
        public IHostingEnvironment HostingEnvironment { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            if (HostingEnvironment.IsDevelopment())
            {
                services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            }
            else
            {
                services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("azure")));
            }
            services.AddMvc();
        }

If you still have any problem, you could enable Diagnostics logs on portal and refer to this article to troubleshooting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM