Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit e95211d

Browse files
authored
adding documentation for forcing ssl and for nextjs (#68)
1 parent 53b03b0 commit e95211d

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 <%= ENV['NGINX_WORKER_CONNECTIONS'] || 1024 %>;
9+
}
10+
11+
http {
12+
gzip on;
13+
gzip_comp_level 2;
14+
gzip_min_length 512;
15+
16+
server_tokens off;
17+
18+
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
19+
access_log <%= ENV['NGINX_ACCESS_LOG_PATH'] || 'logs/nginx/access.log' %> l2met;
20+
error_log <%= ENV['NGINX_ERROR_LOG_PATH'] || 'logs/nginx/error.log' %>;
21+
22+
include mime.types;
23+
default_type application/octet-stream;
24+
sendfile on;
25+
26+
#Must read the body in 5 seconds.
27+
client_body_timeout 5;
28+
29+
server {
30+
listen <%= ENV["PORT"] %>;
31+
server_name _;
32+
keepalive_timeout 5;
33+
34+
location / {
35+
36+
if ($http_x_forwarded_proto != "https") {
37+
return 301 https://$host$request_uri;
38+
}
39+
40+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
41+
proxy_set_header Host $http_host;
42+
proxy_redirect off;
43+
proxy_pass http://localhost:3000; #next serve listens here and receives nginx requests
44+
}
45+
}
46+
}

config/nginx.conf.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ http {
3636
keepalive_timeout 5;
3737

3838
location / {
39+
# Uncomment this if statement to force SSL/redirect http -> https
40+
#if ($http_x_forwarded_proto != "https") {
41+
# return 301 https://$host$request_uri;
42+
#}
43+
3944
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4045
proxy_set_header Host $http_host;
4146
proxy_redirect off;

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ $ heroku config:set NGINX_WORKER_CONNECTIONS=2048
108108

109109
You can provide your own NGINX config by creating a file named `nginx.conf.erb` in the config directory of your app. Start by copying the buildpack's [default config file](config/nginx.conf.erb).
110110

111+
### Force SSL
112+
113+
You can add a redirect/force SSL based on Heroku headers. Full, commented example in the [default config file](config/nginx.conf.erb) or in the [nextjs with forceSSL config file](config/nginx-nextjs-with-forcessl.conf.erb).
114+
115+
```
116+
if ($http_x_forwarded_proto != "https") {
117+
return 301 https://$host$request_uri;
118+
}
119+
```
120+
111121
### Customizable NGINX Compile Options
112122

113123
This requires a clone of this repository and [Docker](https://www.docker.com/). All you need to do is have Docker setup and running on your machine. The [`Makefile`](Makefile) will take care of the rest.

0 commit comments

Comments
 (0)