I use Nginx to reverse proxy a backend API. However, I would like Nginx to serve one static file named "readme.html" when a request is made to "/".
I did extensive research. The most promising solution seemed to be that one: https://stackoverflow.com/a/15467555/2237433.
Trying to apply that solution to my case, here is my code. Here is my Dockerfile:
# syntax=docker/dockerfile:1
FROM nginx:1.22.0-alpine
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./readme.html /www/
EXPOSE 80
With my Nginx container running, I can connect to a shell into it and run cat /www/readme.html
to confirm that the file is indeed present.
Now here is my nginx.conf:
# a lot of stuff
http {
# a lot of stuff
proxy_cache_path /data/nginx/cache keys_zone=my-zone:10m;
server {
listen 80;
add_header X-Cache-Status $upstream_cache_status always;
location / {
root /www/;
try_files readme.html @backend;
}
location @backend {
proxy_pass http://api:8080;
proxy_cache my-zone;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
}
}
}
With this config, all my paths work as expected, except that when I run a request on "/", I get a 404 error. The 404 actually comes from the backend. Here is the log from the backend after the request:
[20/Jun/2022 01:51:06] "GET / HTTP/1.0" 404 -
So the "/" request was actually passed to the backend.
I tried a high number of tweaks from that configuration, but to no avail. Thank you for your help!