Deploying React js build directory in nginx using kubernetes

11/11/2019

Deploy React js(build directory) static files in Nginx server.

Using kubernetes, i am running the nginx server to serve the React js "build" directory for static files. I could access the simple index.html in the browser. But not the index.html with react js code. The root directory has the index.html file, static directory and other files in it. Directory structure is copied to this path.

nginx.conf is like below

events {
    worker_connections  1024;
}
http {
    server {
        listen 80;
        root /usr/share/nginx/html;
    #index index.html;
        location / {
      index index.html; 
          try_files $uri $uri/index.html /index.html;
        }
    location /static/ { 
       autoindex on;
       root /usr/share/nginx/html/static/;
    }   
    }
}

In the browser, index.html is not loaded, but in source (right-click in the browser > select source), the index.html code is available. formatted index.html file copied from source in the browser is at this path. I am predicting that javascript(css,js) code is not executed or having issues loading in the browser. Otherwise, there is a problem with the nginx configuration to load the javascript files.

how to properly configure nginx to deploy Reactjs build directory files?

-- arunp
javascript
kubernetes
nginx
reactjs

2 Answers

11/12/2019

try_files does not tell nginx to serve the static file. Reaching the closing brace in the absence of any other operation causes it to serve the static file. try_files tests for the existence of the file in the local file system and may rewrite the URL.

You have syntax errors in your file i.a white spaces.

Remember to create service for nginx - type LoadBalancer and refer to it in deployment configuration file. You have to add your server if there are several servers that match the IP address and port of the request.

Here is example how you should execute this process. Make sure you followed every step from tutorial: nginx-configuration - there is example of exercise on PHP application but there are the same steps to reproduce with ReactJS app.

Your conf file should looks like this:

events {
    worker_connections  1024;
}
http {
    server {
        listen 80;
        root /usr/share/nginx/html;
        index index.html index.htm;
        server_name your_server_name_or_IP

        location / { 
            try_files $uri $uri/index.html /index.html;
        }

        location /static/ { 
            autoindex on;
            root /usr/share/nginx/html/static/;
        }   
   }
}
-- MaggieO
Source: StackOverflow

11/12/2019

The below code that works for me.

server {
    listen 80;
    root /usr/share/nginx/html;
    index index.html index.htm;
    server_name xyz.application.com;
    server_name localhost; 
    location / { 
    ssi on;
    set $inc $request_uri;
    if (!-f $request_filename) {
        rewrite ^ /index.html last;
    }
    index index.html;
        try_files $uri $uri/index.html /index.html =404;
    }

Setting the rewrite redirects to index.html page. I am not sure about the impact of ssi, set $inc $request_uri in the code.

-- arunp
Source: StackOverflow