Last active
November 23, 2022 07:28
-
-
Save jerhon/827bd0a7d33784f0de90088f0536a373 to your computer and use it in GitHub Desktop.
nginx HTTP configuration for SPA, with reverse proxy for API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Typically I use this file as a boilerplate to configure an nginx docker container | |
# | |
# This goes in /etc/nginx/conf.d/default.conf | |
# If you are reverse proxying an API | |
upstream api { | |
server API_SERVER_GOES_HERE:port; | |
} | |
server { | |
listen 80; // SSL is not configured, but would be configured here | |
# If you are proxied, and the proxy doesn't support URL re-writing, A rewrite rule can be added here | |
# rewrite ^REWRITE_URL_GOES_HERE(.*)$ /$1; | |
location / { | |
root /usr/share/nginx/html; | |
# This is due to nginx and the try_files behavior below, it will always | |
# try to hit the index as part of try_files. If I set index as something | |
# that doesn't resolve, we don't have to worry about index.html being cached. | |
# | |
# If frequent updates occur, it's important that index.html not be cached | |
# in the browser. Otherwise the software update will only occur when the | |
# cached page expires. The If-Modified-Since is a better way to handle this | |
# for SPAs with frequent updates. | |
index unresolvable-file-html.html; | |
try_files $uri @index; | |
} | |
# This seperate location is so the no cache policy only applies to the index and nothing else. | |
location @index { | |
root /usr/share/nginx/html; | |
add_header Cache-Control no-cache; | |
expires 0; | |
try_files /index.html =404; | |
} | |
location /api/ { | |
proxy_set_header X-Forwarded-For $remote_addr; | |
proxy_set_header Host $http_host; | |
# If you have any long running API calls, the read timeout needs to be increased here | |
# proxy_read_timeout 120s; | |
proxy_pass http://api/; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment