Weeedooo
Weeedooo

Reputation: 557

Nginx file not found while doing simple proxy

I'm trying to configure simple Nginx reverse proxy, here is my nginx.conf file

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

      location /api {
        proxy_pass http://172.17.0.1:8081/api;
      }
  }
}

And here is my Dockerfile

FROM openresty/openresty:latest
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80/tcp
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Now, I'm executing docker build . -t my-nginx and then docker run -p 80:80 my-nginx

And I'm calling endpoint at 127.0.0.1:80/api

However, I'm getting 404 back in response and in nginx logs I can see

172.17.0.1 - - [02/Jan/2023:14:39:16 +0000] "POST /api HTTP/1.1" 404 159 "-" "Apache-HttpClient/4.5.13 (Java/17.0.5)" 2023/01/02 14:39:16 [error] 7#7: *1 open() "/usr/local/openresty/nginx/html/api" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "POST /api HTTP/1.1", host: "127.0.0.1:80"

Why is that happening? What is wrong with that configuration?

Upvotes: 0

Views: 214

Answers (1)

mello
mello

Reputation: 114

The reason is that by default the openresty docker image won't look for the nginx configuration file under /etc/nginx/nginx.conf, but on /usr/local/openresty/nginx/conf/nginx.conf. You can verify this by running:

# nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

This said, the /usr/local/openresty/nginx/conf/nginx.conf file has an include directive for all the files under /etc/nginx/conf.d folder, you can therefore place your nginx server and location configurations under this folder.

Replace Dockerfile to be:

FROM openresty/openresty:latest
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80/tcp
ENTRYPOINT ["nginx", "-g", "daemon off;"]

and remove the http key in your nginx configuration.

Make sure to read docker documentation for openresty image, as you'll find all required information: openresty docker

Upvotes: 0

Related Questions