Home > Software engineering >  nginx returning 404 not found for GET request
nginx returning 404 not found for GET request

Time:01-07

I have the following nginx.conf for my nginx server

http {
    server {
        listen 80;
        location /service_data {
            root /opt;
            index service_state;
        }
    }
}

events {}

When I do

curl ip_address:80

nginx returns its standard output, which is OK. But when I try

curl ip_address:80/service_data

it returns the following

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>

however the contents of my file /opt/service_state

First line
Second line

which is just a plain text.

I expected the following behavior:

curl ip_address:80/service_data

which returns

First line
Second line

Am I wrong with my nginx.conf? How do I get the expected behavior?

EDIT:

nginx -T prints the following

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
http {
    server {
        listen 80;
        location /service_data {
            root /opt;
            index service_state;
        }
    }
}

events {}

CodePudding user response:

According to the nginx documentation:

The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive.

So you could change the location to:

location /service_state {
        root /opt;
       # index service_state;
    }

Or change the file name:

service_state -> service_data
  • Related