Skip to content

Commit 2b73310

Browse files
committed
Try an nginx proxy
1 parent 74bf007 commit 2b73310

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

.buildpacks

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ https://github.com/rcaught/heroku-buildpack-cmake
22
https://github.com/alexcrichton/heroku-buildpack-zlib-pkgconfig
33
https://github.com/alexcrichton/heroku-buildpack-rust
44
https://github.com/tonycoco/heroku-buildpack-ember-cli.git
5-
5+
https://github.com/ryandotsmith/nginx-buildpack.git

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: ./target/release/cargo-registry
1+
web: bin/start-nginx ./target/release/cargo-registry

config/nginx.conf.erb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
daemon off;
2+
#Heroku dynos have at least 4 cores.
3+
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
4+
5+
events {
6+
use epoll;
7+
accept_mutex on;
8+
worker_connections 1024;
9+
}
10+
11+
http {
12+
gzip on;
13+
gzip_comp_level 2;
14+
gzip_proxied any;
15+
gzip_min_length 512;
16+
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml image/svg+xml;
17+
18+
server_tokens off;
19+
20+
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
21+
access_log logs/nginx/access.log l2met;
22+
error_log logs/nginx/error.log;
23+
24+
include mime.types;
25+
default_type application/octet-stream;
26+
sendfile on;
27+
28+
#Must read the body in 5 seconds.
29+
client_body_timeout 5;
30+
31+
upstream app_server {
32+
server localhost:8888 fail_timeout=0;
33+
}
34+
35+
server {
36+
listen <%= ENV["PORT"] %>;
37+
server_name _;
38+
keepalive_timeout 5;
39+
40+
location ~ ^/assets/ {
41+
add_header Cache-Control public;
42+
root dist;
43+
expires max;
44+
}
45+
location / {
46+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
47+
proxy_set_header Host $http_host;
48+
proxy_redirect off;
49+
proxy_pass http://app_server;
50+
}
51+
}
52+
}

src/main.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern crate git2;
66

77
use std::os;
88
use std::sync::Arc;
9-
use std::io::{mod, fs};
9+
use std::io::{mod, fs, File};
1010
use civet::Server;
1111

1212
fn main() {
@@ -51,10 +51,18 @@ fn main() {
5151
}
5252
let app = cargo_registry::middleware(Arc::new(app));
5353

54-
let port = os::getenv("PORT").and_then(|s| from_str(s.as_slice()))
55-
.unwrap_or(8888);
54+
let heroku = os::getenv("HEROKU").is_some();
55+
let port = if heroku {
56+
8888
57+
} else {
58+
os::getenv("PORT").and_then(|s| from_str(s.as_slice()))
59+
.unwrap_or(8888)
60+
};
5661
let _a = Server::start(civet::Config { port: port, threads: 8 }, app);
5762
println!("listening on port {}", port);
63+
if heroku {
64+
File::create(&Path::new("/tmp/app-initialized")).unwrap();
65+
}
5866
wait_for_sigint();
5967
}
6068

0 commit comments

Comments
 (0)