Search code examples
linuxnginxservercentosasp.net-core-webapi

ASP.NET Core 2 Web API - 502 Bad Gate Way(Centos 7) - NGINX


I have an ASP.NET Core 2 Web API project and Centos 7 Linux server.

I ran the project in my server as a service.

It is running 7/24 in my server.

If I write to Linux terminal "wget http://localhost:[PORT]/api/users --no-check-certificate", users json file download to my server. There is no problem here.

But I can't access to my api from my local computer.

If I write "http://[SERVER_IP]:[PORT]/api/users" to a web browser, it returns 502 Bad Gateway Http Status Code.

How can I fix it?

etc/nginx/nginx.conf

http {
      ...
      server{
         listen [PORT];
         location / {
            proxy_pass http://[SERVER_IP]:[PORT];
         }
      } 
}

Solution

  • I solved the problem. My server has vesta c panel. so i must not write the server block to the etc/nginx/nginx.conf. I created a user and a subdomain which is named api.hocamnerede.com and i wrote the server block to /home/Hocamnerede/conf/web/api.hocamnerede.com.nginx.conf.

    /home/Hocamnerede/conf/web/api.hocamnerede.com.nginx.conf

    server {
    listen      195.201.150.228:80;
    server_name api.hocamnerede.com www.api.hocamnerede.com;
    root        /home/Hocamnerede/web/api.hocamnerede.com/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/api.hocamnerede.com.log combined;
    access_log  /var/log/nginx/domains/api.hocamnerede.com.bytes bytes;
    error_log   /var/log/nginx/domains/api.hocamnerede.com.error.log error;
    
        location /api {
            proxy_pass         http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_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;
        }
    }