Skip to content

Instantly share code, notes, and snippets.

@stefanfoulis
Created July 19, 2011 07:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stefanfoulis/1091532 to your computer and use it in GitHub Desktop.
Save stefanfoulis/1091532 to your computer and use it in GitHub Desktop.
An example nginx conf for mogilefs and gunicorn

Private media

Browser accesses URL of Application:

http://www.example.com/private_media/my_file.pdf

The Permissions are checked by the application backend. If Access is denied the application backend returns a 403 and thats the end of it. If Access is granted the application backend returns a special http header:

X-Accel-Redirect: /my_private_location/path/to/my_file.pdf;

Nginx in turn serves whatever is accessible from that url, bypassing the internal safeguard on the location directives. In this case it would serve:

/path/to/real/location/in/filesystem/path/to/my_file.pdf

Private media served from MogileFS

Same as general idea as above. The header might look like this:

X-Accel-Redirect: /my_private_mogilefs_location/path/to/my_file.pdf;

And nginx will serve the content from MogileFS using the path as file key:

/path/to/my_file.pdf
# This config has not been tested yet!
server {
listen 80;
server_name www.example.com;
access_log /var/log/nginx/example.com.log;
location / {
proxy_pass http://127.0.0.1:1337;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
location /static {
alias /home/examble/apps/example/live/static_collected/;
}
location /media {
alias /home/examble/apps/example/live/uploads/public/;
}
# secure location for use with X-Accel-Redirect (http://wiki.nginx.org/XSendfile)
location /my_private_location/ {
internal;
alias /path/to/real/location/in/filesystem/;
}
# secure location for use with X-Accel-Redirect (http://wiki.nginx.org/XSendfile)
# using MogileFS (http://www.grid.net.ru/nginx/mogilefs.en.html)
location /my_private_mogilefs_location/ {
internal;
mogilefs_tracker 192.168.2.2;
mogilefs_domain example_domain;
mogilefs_pass {
proxy_pass $mogilefs_path;
proxy_hide_header Content-Type;
proxy_buffering off;
}
}
}
server {
server_name example.com whatever.other.domain.com
rewrite ^(.*) http://www.example.com$1 permanent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment