1

I have some static files, that I can already access, and I have an api, already at digital ocean.

But I am having some troble configuring NGINX to access both at the same time. If my api works, the static files don't. And vice-versa;

My api is running on port 5000, and I want to access the site and the api by IP adress.

Pls, help!

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    server_name _;

    location / {

        try_files $uri $uri/ =404;

    }

}

server {

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

1 Answer 1

1

If both services are being accessed from the same IP address and port, then you need to use one server block. Ideally, you would move the API into a unique URI, for example:

location / {
    try_files $uri $uri/ =404;
}
location /api/ {
    proxy_pass ...;
    ...
}

If you must use an overlapping URI space for both static files and the API, you can use try_files to check for the existence of a static file first:

location / {
    try_files $uri $uri/ @proxy;
}
location @proxy {
    proxy_pass ...;
    ...
}

See this document for more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.