Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Created July 15, 2017 02:47
Show Gist options
  • Select an option

  • Save sandcastle/b3898bade6107a43501ab9623e556927 to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/b3898bade6107a43501ab9623e556927 to your computer and use it in GitHub Desktop.
A default for nginx configuration file.
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
charset utf-8;
index index.html;
# Root
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html =404;
}
# Enable pre-compressed copies (.gz) of static files available.
location ~* \.(js)$ {
gzip_static on;
}
# API
location /api/ {
# This variable will be replaced
proxy_pass API_URLapi/;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Methods "GET,POST,OPTIONS,PUT,DELETE,PATCH";
add_header Access-Control-Allow-Headers 'Authorization,Content-Type,Accept,Origin,Cache-Control,Keep-Alive,If-Modified-Since';
add_header Access-Control-Allow-Credentials "true";
add_header Access-Control-Max-Age 1728000;
add_header Content-Length 0;
add_header Content-Type text/plain;
add_header Cache-Control 'max-age=0';
return 204;
}
}
# Images, Scripts & Styles
location ~* \.(png|jpg|jpeg|gif|mp3|js|css|ico|svg|gz|mp4|webm)$ {
expires 4h;
access_log off;
add_header Cache-Control "public";
}
# Fonts
location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# Prevent dot file access
location ~* (?:^|/)\. {
deny all;
access_log off;
log_not_found off;
}
# Prevent Backup / Log / Script access
location ~* (?:\.(?:bak|conf|log|sh)|~)$ {
deny all;
access_log off;
log_not_found off;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment