HMR
HMR

Reputation: 39340

Docker nginx can't get single page application to run

I'm using the following docker command:

docker run --name spa \
    -v "$PWD/build":/usr/share/nginx/html:ro \
    -v "$PWD/nginx.conf":/etc/nginx/nginx.conf:ro -d -p 8080:80 nginx

Then I can open localhost:8080 and get the index, clicking on links work since navigation is done on the client but then refreshing localhost:8080/en will give me a 404 from nginx. The "$PWD/nginx.conf" is:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    server {
      listen 80;
      root /usr/share/nginx/html;
      gzip on;
      gzip_types text/css application/javascript application/json image/svg+xml;
      gzip_comp_level 9;
      etag on;
      location / {
        try_files $uri $uri/ /index.html;
      }
      location /static/ {
        add_header Cache-Control max-age=31536000;
      }
      location /index.html {
        add_header Cache-Control no-cache;
      }
      location /config.json {
        add_header Cache-Control no-cache;
      }
    }
}

I got the server part form here and guess it goes in http according to the documentation but it doesn't server index.html when a route doesn't point to a file.

In ./build there is a file index.html that is served on the root path.

Upvotes: 0

Views: 635

Answers (1)

HMR
HMR

Reputation: 39340

I removed the include /etc/nginx/conf.d/*.conf; in the config and now it works.

Upvotes: 1

Related Questions