2

I have an app which is using React router, and I created a docker image for it. I am using nginx server and it works. But, refreshing a page gives me nginx 404 error. I know that I need to overwrite nginx config file to make it work, but don't know why it doesn't work.

I tried to add some kind of redirect to nginx.conf just to see if it overrides default.conf but it doesn't work. These are my files:

Dockerfile:

FROM nginx:alpine
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

server {
    listen       80 default;
    server_name  localhost;
    return 301 https://www.google.com;
 }

EDIT:

And how to check what config file is running?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
bobby
  • 445
  • 2
  • 9
  • 24

1 Answers1

5

I believe you should use

COPY ./nginx/default.conf /etc/nginx/nginx.conf 

If you want to organise things in conf.d/, add include /etc/nginx/conf.d/* in /etc/nginx/nginx.conf before adding things in conf.d/

Complete config file:

worker_processes 4;

events {
    worker_connections  1024;
}

http {
  server {
      listen       80 default;
      server_name  localhost;
      return 301 https://www.google.com$request_uri;
  }
}
Siyu
  • 11,187
  • 4
  • 43
  • 55
  • can you try this config file? maybe nginx failed to launch – Siyu Jan 15 '19 at 09:56
  • thanks for helping, but still doesnt resolve it... dont think it failed to lunch cause on refresh i get 404 error from nginx – bobby Jan 15 '19 at 10:01
  • yea my mistake when i was copying the code, i edited now, but that is not the problem, my ngix conf still doesnt overide default one – bobby Jan 15 '19 at 10:15
  • I'm having the same dockerfile as yours, mine works. Check to make sure it's not another nginx that runs, rebuild image and run it. – Siyu Jan 15 '19 at 10:34
  • i did all of that, delete all unsed images (had two diffrent nginx), then rebuild it and run it, now have only one that is running, still doesnt work – bobby Jan 15 '19 at 11:30
  • ok i cant believe what i was doing, i was starting wrong docker image, everything works fine – bobby Jan 15 '19 at 12:01