i've spent this evening trying to configure Nginx to get subdomains to work, i have been unsuccessful, i don't know how i can get it to work, can someone please help
the scheme looks like this
"www.example.com" points to my .net core mvc project.
"api1.example.com" points to my .net core api1 project.
"api2.example.com" points to my .net core api2 project
my projects are listed like bellow in my linux server.
mvc /var/www/example.com
api1 /var/www/api1.example.com
api2 /var/www/api2.example.com
CodePudding user response:
i figured it out myself, i'll explain here how i got it to work.
in my original question i metionned two projects api1 and api2, in this solution i will only use one project api1 to make this short,
1- you need to register your sub domain in the A/AAAA record, you do this in your server provider dashboard, mine is "linode",
the record i added is "api1", you will also be asked to provide an ip address, it is the public IP of your server,
2- publish your project and copy the generated files to /var/www/api1.example.com
make sure the port is different from the mvc project.
for me mvc project port is 5000.
api1 project port is 5001.
3- create a service for your api1 project, the service file should be saved in the following directory /etc/systemd/system/kestrel-api1.service
[Unit]
Description=Example My ASP.NET Core Application running on Ubuntu
[Service]
WorkingDirectory=/var/www/api1.example.com
ExecStart=/usr/bin/dotnet /var/www/api1.example.com/api1.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_URLS=http://127.0.0.1:5001
[Install]
WantedBy=multi-user.target
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
4- add a new server block in the config file of your mvc project
server {
listen 80;
listen [::]:80;
server_name api1.example.com *.example.com;
root /var/www/api1.example.com;
index index.html index.htm;
location / {
proxy_pass http://localhost:5001;
}
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
5- lastly don't forget to restart the new api1 service using this command in your SSH terminal.
sudo systemctl restart kestrel-api1.service
Also you need to restart Nginx using this command
service nginx restart
now if you navigate to example.com, the mvc project will be served.
if you navigate to api1.example.com it will serve api1 project.