Skip to content

Instantly share code, notes, and snippets.

@qguv
Created July 29, 2020 12:43
Show Gist options
  • Save qguv/21d6c65b47b8a6e529aa5d8c2cbc49d3 to your computer and use it in GitHub Desktop.
Save qguv/21d6c65b47b8a6e529aa5d8c2cbc49d3 to your computer and use it in GitHub Desktop.
nginx host static files, add trailing slash when using implicit index.html, preserve anchors and queries
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
# ends in slash
location ~ ^([^#?]*/)([#?].*)?$ {
root /var/www/example.com;
try_files $1/index.html =404;
}
# ends in extension
location ~ ^([^#?]*\/[^#?\/][^#?\/.]*\.[a-zA-Z0-9]+)([#?].*)?$ {
root /var/www/example.com;
try_files $1 =404;
}
# ends in bare path
location ~ ^([^#?]*\/[^#?\/]+)([#?].*)?$ {
return 302 $1/$2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment