Skip to content

Instantly share code, notes, and snippets.

@JFK
Last active September 16, 2019 21:10
Show Gist options
  • Save JFK/4591707 to your computer and use it in GitHub Desktop.
Save JFK/4591707 to your computer and use it in GitHub Desktop.
Ngnix + Nginx-GridFS-Replicaset(mongodb) + nginx-image-filter +nginx-proxy-cache Sample Configuration
user www-data;
#worker_processes 4;
#worker_priority 0;
#worker_cpu_affinity 0001 0010 0100 1000;
#worker_rlimit_nofile 163840;
#worker_processes 8;
#worker_priority 0;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#worker_rlimit_nofile 163840;
#worker_processes 2;
#worker_priority 0;
#worker_cpu_affinity 01 10;
#worker_rlimit_nofile 163840;
pid /var/run/nginx.pid;
events {
worker_connections 40960;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile off;
tcp_nopush off;
keepalive_timeout 10;
tcp_nodelay on;
proxy_max_temp_file_size 100m;
proxy_cache_path /var/lib/nginx/ramdisk/cache levels=1:2 keys_zone=cache-space:64m max_size=1000m inactive=90m;
proxy_temp_path /var/lib/nginx/ramdisk/tmp;
client_body_temp_path /var/lib/nginx/ramdisk/client_tmp 1 2;
}
#server_names_hash_bucket_size 128;
server {
listen 80;
server_name origin.hostname.com;
gzip on;
gzip_proxied any;
gzip_http_version 1.1;
gzip_min_length 10;
gzip_buffers 16 8k;
gzip_comp_level 4;
gzip_disable msie6;
gzip_types image/png image/gif image/jpeg;
gzip_vary on;
proxy_intercept_errors on;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Protocol https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ~ ^/image {
access_log off;
expires 3h; // tell expires to cloudfront
proxy_cache cache-space;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 400 401 402 403 404 415 502 503 504 1m;
proxy_cache_key $uri;
proxy_pass http://localhost:8080;
error_page 400 401 402 403 404 415 502 503 504 = @noimg;
}
location @noimg {
access_log off;
rewrite ^ http://snapdi.sh/img/notfound.png break;
}
}
server {
listen 8080;
server_name localhost;
location ~ ^/image/dish/([a-f0-9]+)/crop/([0-9][0-9][0-9])$ {
access_log off;
image_filter crop $2 $2;
image_filter_jpeg_quality 95;
proxy_pass http://localhost:8088;
error_page 400 401 402 403 404 415 502 503 504 = @noimg;
}
location ~ ^/image/user/([a-f0-9]+)/crop/([0-9][0-9][0-9])$ {
access_log off;
image_filter crop $2 $2;
image_filter_jpeg_quality 95;
proxy_pass http://localhost:8088;
error_page 400 401 402 403 404 415 502 503 504 = @nouserimg;
}
location ~ ^/image/user/([a-f0-9]+)$ {
access_log off;
set $oid $1;
rewrite ^ /user/$oid last;
}
location /user/ {
access_log off;
gridfs mongodb_for_image root_collection=user_image_collection;
mongo "rplsetname" rpl1.mongodb.host:27017 rpl2.mongodb.host:27017;
error_page 400 401 402 403 404 415 502 503 504 = @noimg;
}
location ~ ^/image/dish/([a-f0-9]+)$ {
access_log off;
set $oid $1;
rewrite ^ /dish/$oid last;
}
location /dish/ {
access_log off;
gridfs mongodb_for_image root_collection=dish_image_collection;
mongo "rplsetname" rpl1.mongodb.host:27017 rpl2.mongodb.host:27017 rpl3.mongodb.host:27017;
error_page 400 401 402 403 404 415 502 503 504 = @noimg;
}
location @noimg {
access_log off;
rewrite ^ http://snapdi.sh/img/notfound.png break;
}
}
server {
listen 8088;
server_name localhost;
location ~ ^/image/dish/([a-f0-9]+)/crop/([0-9][0-9][0-9]) {
access_log off;
set $oid $1;
rewrite ^ /dish/$oid last;
}
location /dish/ {
access_log off;
gridfs mongodb_for_image root_collection=dish_image_collection;
mongo "rplsetname" rpl1.mongodb.host:27017 rpl2.mongodb.host:27017 rpl3.mongodb.host:27017;
error_page 400 401 402 403 404 415 502 503 504 = @noimg;
}
location ~ ^/image/user/([a-f0-9]+)/crop/([0-9][0-9][0-9]) {
set $oid $1;
rewrite ^ /user/$oid last;
}
location /user/ {
access_log off;
gridfs mongodb_for_image root_collection=user_image_collection;
mongo "rplsetname" rpl1.mongodb.host:27017 rpl2.mongodb.host:27017 rpl3.mongodb.host:27017;
error_page 400 401 402 403 404 415 502 503 504 = @noimg;
}
location @noimg {
access_log off;
rewrite ^ http://snapdi.sh/img/notfound.png break;
}
}
@squamous
Copy link

This looks really interesting. I've recently started using mongodb/nginx/gridfs on a large-scale project and am learning a lot of really cool useful things you can do with that stack, so I get the "gist" of what you're doing but could you explain it a little more?

It looks like you're redirecting requests for images in nginx across mongo gridfs replicasets.

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