Skip to content

Instantly share code, notes, and snippets.

@ilguzin
Last active June 6, 2016 13:33
Show Gist options
  • Save ilguzin/9368771 to your computer and use it in GitHub Desktop.
Save ilguzin/9368771 to your computer and use it in GitHub Desktop.
spray + nginx: serve static with storage API endpoint
server {
listen 443 ssl;
server_name hostname;
ssl on;
ssl_certificate /etc/nginx/ssl_certs/hostname.bndl.crt;
ssl_certificate_key /etc/nginx/ssl_certs/hostname.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
charset utf8;
access_log /var/log/nginx/hostname.access.log main;
error_log /var/log/nginx/hostname.error.log debug;
location @cors_response {
more_set_headers "Access-Control-Allow-Origin: $http_origin";
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers "Access-Control-Allow-Headers: $http_access_control_request_headers";
more_set_headers 'Access-Control-Max-Age: 2592000';
return 204;
}
error_page 419 = @cors_response;
set $cors "";
if ($http_origin = "https://origin_hostname") {
set $cors "true";
}
if ($request_method = OPTIONS) {
set $cors "${cors}options";
}
if ($cors = "trueoptions") {
return 419;
}
location ~* ^/avatars/(.+)$ {
root /data/filestorage/avatars;
more_set_headers "Access-Control-Allow-Origin: https://origin_hostname";
more_set_headers 'Access-Control-Allow-Credentials: true';
try_files /$1.jpeg /$1.jpg /$1.gif /$1.png @filestorage;
}
location / {
error_page 418 = @filestorage; return 418;
}
location @filestorage {
rewrite ^/v1.0/(.+)$ /filestorage/$1 break;
more_set_headers "Access-Control-Allow-Origin: $http_origin";
more_set_headers 'Access-Control-Allow-Credentials: true';
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
@agemooij
Copy link

agemooij commented Mar 6, 2014

On a complete side-note: your SSL/TLS config is outdated and not recommended. SSLv2 and v3 are definitely not recommended anymore and I would suggest moving to 1.2 as soon as possible, with 1 and 1.1 as fallback for older browsers.

Have a look at the excellent resources at SSL Labs, including their server test and best practices.

@ilguzin
Copy link
Author

ilguzin commented Mar 11, 2014

thanks!

@gkteck
Copy link

gkteck commented Dec 31, 2015

Hi,

I am getting
nginx: [emerg] unknown directive "more_set_headers" error
Any idea ?

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