Posted on: 2 Year(s)

Bad Gateway 502 Nginx Error How to Solve

Asp.net core is a platform that is best to develop desktop and web applications. Asp.net core is developed to develop applications for Phones, Windows, Linux, and other platforms. Asp.net core is improving day by day because millions of developers are moving to Asp.net core. Asp.net core is performing well and also faster. Asp.net core application can run on both Linux and Windows operating systems. 

Asp.Net core 7 is the latest version of Asp.net core. Windows hosting is expensive and not everyone can afford expensive windows hosting. Everyone likes Linux hosting services because of its reliability, ease of configuration and lightweight. Hardware consumption on a Linux server is much less than then a Windows server. Database and web hosting needs space and RAM but window OS consumes much of it due to which a person needs to upgrade to run its application on a faster server. 

How to Solve Bad Gateway 502 Error on Nginx

Bad Gateway 502 error is common when you host your Asp.net Core web application on linux. Asp.net core is a little tricky when you configure it on a nginx server. Nginx server is much faster and reliable and developers love to use nginx to host their web application on nginx. Asp.net core Web application often shows Bad Gateway 502 error while hosting on nginx server Linux. It's because of the proxy. Asp.Net Core hosting on Linux is tricky because you need to configure service for Asp.Net Core Web Application and nginx server. Follow these steps below to configure your nginx server and web service for your asp.net core 6.0, 5.0, 3.0 web application to solve bad Gateway 502 error.

Nginx Server configuration for Asp.net Core Web Application

Copy and edit the code below according to your application location. 

server {

    server_name   yoursite.com *.yoursite.com;

    location / {

        proxy_pass         http://localhost:7057; // you need to change port no according to your application

        proxy_http_version 1.1;

        proxy_set_header   Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

        proxy_set_header   Connection keep-alive;

        proxy_set_header   Host $host;

        proxy_cache_bypass $http_upgrade;

        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header   X-Forwarded-Proto $scheme;



Copy and paste above code into notepad and edit accordingly the highlighted code you have to change. Port Number must match to your project port no.

In your project go to the file launchsettings.json and application url the port number should be the same as in your project.

After changing your code 


Description=Todaybins Core applications running on Ubuntu



ExecStart=/usr/bin/dotnet /var/www/yoursite/applicationname.dll


# Restart service after 10 seconds if the dotnet service crashes:







Environment=ASPNETCORE_URLS=http://localhost:7057 // Url has to be same as you mentioned in server config



After all the amendments you need to restart your service and nginx server using following codes.

>>sudo systemctl restart yourservicename.service

>>sudo systemctl restart nginx

Go to your application on line Bad Gateway 502 error will be gone.


This is some sample content.

User Profile
User Name

This is a sample comment. It can be longer and contain more text.

2 hours ago