Skip to content

Instantly share code, notes, and snippets.

@jsuryahyd
Created September 16, 2021 15:40
Show Gist options
  • Save jsuryahyd/685408510506f83802fc824341a3f00a to your computer and use it in GitHub Desktop.
Save jsuryahyd/685408510506f83802fc824341a3f00a to your computer and use it in GitHub Desktop.
Nginx config for SPA

Serve with nginx (no pm2)

server{
   listen 443 ssl;
   server_name surya.in;

   ssl_certificate /etc/nginx/sslcerts/surya.in.crt;
   ssl_certificate_key /etc/nginx/sslcerts/surya.in.rsa;

   root /var/www/my-react-app/build;
   index index.html index.htm;
#   autoindex off;
    location /{
        add_header X-Frame-Options SAMEORIGIN always;
        add_header Content-Security-Policy "frame-ancestors 'self';";
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        try_files $uri $uri/ /index.html;    }

}

server {
    listen 80;
    server_name surya.in;
    return 301 https://surya.in$request_uri;
}

Deploy with pm2

  • run on deployment server - npm run serve-static-pm2
  • https://pm2.keymetrics.io/docs/usage/expose/
  • nginx config -
    server{
      listen 443 ssl;
      server_name surya.in;
    
      ssl_certificate /etc/nginx/sslcerts/surya.in.crt;
      ssl_certificate_key /etc/nginx/sslcerts/surya.in.rsa;
    
      root /var/www/my-react-app/build;
      index index.html index.htm;
    #   autoindex off;
        location /{
            add_header X-Frame-Options SAMEORIGIN always;
            add_header Content-Security-Policy "frame-ancestors 'self';";
            add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
            proxy_pass http://localhost:3000;
        }
    }
    
    server {
        listen 80;
        server_name surya.in;
        return 301 https://surya.in$request_uri;
    }
    
    
server{
listen 443 ssl;
server_name surya.in;
ssl_certificate /etc/nginx/sslcerts/surya.in.crt;
ssl_certificate_key /etc/nginx/sslcerts/surya.in.rsa;
root /var/www/my-react-app/build;
index index.html index.htm;
# autoindex off;
location /{
add_header X-Frame-Options SAMEORIGIN always;
add_header Content-Security-Policy "frame-ancestors 'self';";
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
try_files $uri $uri/ /index.html; }
}
server {
listen 80;
server_name surya.in;
return 301 https://surya.in$request_uri;
}
# part snippet
# run with pm2
location ^~ /careers {
error_log /var/www/my-react-app/nginx-error.log;
access_log /var/www/my-react-app/nginx-access.log;
proxy_pass http://localhost:3000;
}
location ~ ^/cp-public{
root /var/www/my-react-app/;
try_files $uri $uri/ /index.html =404;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment