Skip to content

Instantly share code, notes, and snippets.

@slackday
Last active June 27, 2019 00:47
Show Gist options
  • Save slackday/2f5065003312429616f5711f06af465d to your computer and use it in GitHub Desktop.
Save slackday/2f5065003312429616f5711f06af465d to your computer and use it in GitHub Desktop.
Serve static app like Create React App from Nginx subdirectory
# Example config for serving a static page under nginx subdirectory
# Include this config in your server block and change the paths
#
# This is useful because we can server the same app from /subdirectory/subsubdirectory
# Where the subsubdirectory could represent :slug or :id in the client side routing.
location ^~ /subdirectory/ {
alias /path/to/static/webroot/;
if (!-e $request_filename) {
rewrite ^ /subdirectory/index.html last;
}
location ~ \.html$ {
if (!-f $request_filename) {
return 404;
}
}
# Replace source paths with domain prefixed ones.
subs_filter href="/ href="https://appdomain.domain/;
subs_filter src="/ src="https://appdomain.domain/;
# If you're using create-react-app it can also be useful to set
# PUBLIC_URL=https://appdomain.domain/ in .env before building
}
# Note: This is only useful for serving client side apps.
#
# To run Node on the server look into pm2
# http://pm2.keymetrics.io/
#
# And use reverse proxy approach (proxy_pass) for the Nginx config
# https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment