Rachel
Rachel

Reputation: 727

Using Nginx with Create-React-App dev server and npm start

Frustrating question here. I have a react project created with created-react-app on a server that has to use Nginx for external connection/HTML server. What essentially I need to do is use nginx as a proxy server and when the URL is typed into browser, the request is routed to localhost:3000 and the react app generates the result which is served to the client by nginx.

So, here is the nginx file so far, but I can't get it to work. A number of questions:

  1. does the root designation need to be directed to the /public directory in the react project to pick up index.html?

  2. Assuming that you run npm start and after the compile, you enter the FQDN in the browser and it routes to localhost:3000

My Nginx config so far

    server_name         server-name.com;   
    listen              443 ssl;
    ssl_certificate     /etc/letsencrypt/live/site-name/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/site-name/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    proxy_read_timeout 60s;
    client_max_body_size 64M;
    
    # should this be routed to /public in the react project to pick up index.html?
    # root /var/www/html;

    index index.html index.htm;

    location / {
        #try_files $uri $uri/ =404;
        
        # Make sure React Application return is index.html so client routing remains in sync/reachable        
        #try_files $uri /index.html;
   
        proxy_pass  http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }    

    location /_api {
       rewrite ^/_api/(.+) /$1 break;
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3030;
    }
}

server {
    server_name         server-name.com   
    listen              80;

    allow 10.0.0.0/16;
    deny all;

    proxy_read_timeout 60s;
    client_max_body_size 64M;

    # Should this be routed to /public in the react project to pick up index.html?
    # root /var/www/html;

    index index.html index.htm;

    location / {
        #try_files $uri $uri/ =404;
        
        # Make sure React Application return is index.html so client routing remains in sync/reachable        
        try_files $uri /index.html;

    }    

    location /_api {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3030;
   }
} 

Upvotes: 3

Views: 4665

Answers (1)

Sumit Kumar
Sumit Kumar

Reputation: 183

open your nginx file vi sites-enabled/default if you don't know how to do that let me know.

then remove everything from that file and simply paste :

server {
    listen 80;
    client_max_body_size 10M;
    server_name YourWebsiteNameWithDomain yourIPV4;
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
     }
}

don't forget to restart your nginx

Upvotes: 1

Related Questions