Reputation: 1678
I am trying to host my AngularJs app and my dotnet core api on nginx but I'm unable to access my api, following is my default.conf in /etc/nginx/conf.d:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/dashboard;
index index.html;
server_name example.server.com;
location / {
try_files $uri /index.html;
}
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;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Upvotes: 2
Views: 813
Reputation: 51
For the above issue, this could be the best solution.
Nginx Configuration:
location /api/ {
proxy_pass http://127.0.0.1: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;
}
# Install nginx-extras to hide server name
sudo apt install nginx-extras
# Hide server name
more_set_headers 'Server: abisec';
Service Configuration (api.service):
[Unit]
Description=Service Description
[Service]
WorkingDirectory=/var/www/yourserviceapi/
ExecStart=/usr/bin/dotnet /var/www/yourserviceapi/yourserviceapi.dll --urls=http://0.0.0.0:5000
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-web-app
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Manage Service:
# Create and manage the service
sudo nano /etc/systemd/system/api.service
sudo systemctl enable api.service # Enable the service
sudo systemctl start api.service # Start the service
sudo systemctl status api.service # Check the status of the service
Port Specification:
Specifying the port is considered best practice. If hosting only one API, it's not necessary, but for multiple APIs, define the ports in your service file.
Upvotes: 0