I want to serve my static website
and API service
from same machine using nginx.
Website is present in /var/www/html
API service is running on port 8000
http://localhost
should open static website
http://localhost/api
should proxy api service which is running on port 8000
With the help of http://nginx.org/en/docs/beginners_guide.html I tried this config
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://localhost:8000;
}
}
http://localhost
is working fine but http://localhost/api
is giving me error 404
.
What should be correct configuration to achive such infrastucture?
2
Answers
Your nginx reverse-proxy will pass all the requests contains (or start by, I’m not sure) "/api" to your API service.
When you send the request to http://localhost/api, the route "/api" in your code is invoked. I guess this route does not exist because you have 404.
To access your API, 1 simple solution is to prefix all your API by "/api".
For example, if you define the API GET "/version", it should become "/api/version", then you can access this API at http://localhost/api/version.
Here I am writing a
conf
that can perform the operation you need: