Skip to content

Instantly share code, notes, and snippets.

@r17x
Last active June 30, 2018 10:17
Show Gist options
  • Select an option

  • Save r17x/8e5eb80bc2f9c83edda473266c3d4333 to your computer and use it in GitHub Desktop.

Select an option

Save r17x/8e5eb80bc2f9c83edda473266c3d4333 to your computer and use it in GitHub Desktop.

Pagespeed Configuration Nginx for SPA [Single Page Application]

Example Case use for ReactJS with Python Flask

upstream BackEnd {
	ip_hash;
	server 127.0.0.1:8085;
	# port untuk applikasi backend
}

server {

	listen 80;
	server_name www.domain.id;
	index index.html;
	# Web Static 
	# contoh path untuk CRA
	root /home/user/frontend/build;

	charset     utf-8; 

	location / {
		try_files $uri $uri/ /index.html; 
	}
	
	location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
		add_header "" "";
	}
	
	location ~ "^/pagespeed_static/" { }
	location ~ "^/ngx_pagespeed_beacon$" { }
	
	# Jika menggunakan PWA 
	location index.html {
		add_header Last-Modified $date_gmt;
		add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
		if_modified_since off;
		expires off;
		etag off;
	}
	# Jika menggunakan PWA 
	location service-worker.js {
		add_header Last-Modified $date_gmt;
		add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
		if_modified_since off;
		expires off;
		etag off;
	}
	# Contoh untuk ReactJS
	location ^~ /static/ {
		autoindex on;
		gzip_static on;
		include /etc/nginx/mime.types;
	}	 	

	# Contoh handle url untuk api yang menggunakan uwsgi/wsgi
	# contoh dibawah saya gunakan untuk Backend menggunakan 
	# python Flask (Supervisor + Flask + wsgi)
	location ~ ^/(api|media|auth)/{
		root /home/user/backend/apps;
		include uwsgi_params;
		uwsgi_pass BackEnd;
		uwsgi_read_timeout 100000;
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment