C
C#2y ago
oe

❔ Why can't I host my ASP.NET WebAPI on Ubuntu ?

1) I made a new user sudo adduser aspsites 2) I created a folder called surf and cloned my repo inside it git clone myurl (now the full path is: /home/aspsites/surf/SurfFinder/SurfFinderAPI/SurfFinderAPI.csproj) 3) Ran dotnet build (all good) 4) Added the following configuration file in /etc/nginx/sites-enabled/aspsites:
server {
listen 80;

server_name www.mywebsite.com;

#root /var/www/html; # Replace with your application's root directory
root /home/aspsites;

location /surf/SurfFinder {
proxy_pass http://localhost:5000; # Replace with the port where your ASP.NET API is running on
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;
}
}
server {
listen 80;

server_name www.mywebsite.com;

#root /var/www/html; # Replace with your application's root directory
root /home/aspsites;

location /surf/SurfFinder {
proxy_pass http://localhost:5000; # Replace with the port where your ASP.NET API is running on
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;
}
}
5) Ran dotnet run and then the CLI got stuck? Now I can't run any command. And of course, when I visit www.mywebsite.com/surf/SurfFinder/WeatherForecast nothing happens 🤔. I'm sure I'm doing a bunch of things wrong. I am completely new to Linux & ASP.NET, but I'm willing to learn, so please help me if you can.
9 Replies
x0rld
x0rld2y ago
the port is 5088 no ?
oe
oe2y ago
Thanks, I changed it.
No web page was found for the web address: https://asp.mywebsite.com/surf/SurfFinder/WeatherForecast
No web page was found for the web address: https://asp.mywebsite.com/surf/SurfFinder/WeatherForecast
Now I'm getting this issue Plus this in the terminal:
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
This is my /aspsites/ file:
server {
listen 80;

server_name asp.mywebsite.com;

root /home/aspsites;

location / {
try_files $uri $uri/ =404;
}

location /surf/SurfFinder {
proxy_pass https://localhost:5088;
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;
}
}
server {
listen 80;

server_name asp.mywebsite.com;

root /home/aspsites;

location / {
try_files $uri $uri/ =404;
}

location /surf/SurfFinder {
proxy_pass https://localhost:5088;
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;
}
}
Pheubel
Pheubel2y ago
I assume you already got yourself a URL.
oe
oe2y ago
Yes
gerard
gerard2y ago
Probs because it's sending the complete path to the proxy /surf/SurfFinder/WeatherForecast instead of /WeatherForecast You can fix this by adding a / to the proxy pass URL: proxy_pass https://localhost:5088/; from what I found. https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Jester
Jester2y ago
yeah i had that path issue as well
oe
oe2y ago
Works perfectly, thanks. Is it possible to run another API now on the same Nginx server? For example, both /surf/SurfFinder/ and /surf/Football/ simultaneously. And if so, is there any way to automate the process so I don't have to modify sites-enabled each time I upload an ASP.NET project in a different folder? @gerard Also, when I close my Putty terminal, the API is no longer running 🤔 ?
gerard
gerard2y ago
Yes, if you give the other ASP.NET Core application a different port you can map that to Nginx. I'm not sure if there is already a way to automate this (since I'm not using Nginx myself). That's because you probs started the ASP.NET Core process in your session. If you disconnect, your session gets closed; and so does the ASP.NET Core server. Again, I'm not a Linux pro but from my knowledge you have the following options: 1. Use screen to start the process. If you detach from the virtual screen/session the process keeps running. This is the easiest, but if your server ever restarts you have to start everything manually yourself. So not really recommended. 2. Make a service No clue what distro you're using, but there are docs about systemd here: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-7.0#create-the-service-file 3. Run it in Docker
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.