heroku + nginx on port 80

2019-05-09 05:54发布

问题:

im trying to start a nginx server on heroku free environmnent. I ready any how-tos and tutorial, but i dont get it running.

First of all, i would like to start nginx as default web-server on Port 80. Afterwards i would like configure nginx as proxy for the underline express server (other heroku instance). For 4 days i trying to start only nginx on my heroku instance. I always getting the exception that not permitted to start on port 80. I forked the nginx-buildback (https://github.com/moohkooh/nginx-buildpack) from (https://github.com/benmurden/nginx-pagespeed-buildpack) to adjust some configuration. If i run nginx via heroku bash on port 2555, nginx is starting, but i get connection refused on web-browser.

If i starting nginx via Dyno i getting error message on logs

  State changed from starting to crashed

the Procfile of my Dyno

  web: bin/start-nginx

My nginx.config.erb

 daemon off;
 #Heroku dynos have at least 4 cores.
 worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;

 events {
     use epoll;
     accept_mutex on;
     worker_connections 1024; 
 }

 http {
     gzip on;
     gzip_comp_level 2;
     gzip_min_length 512; 

     server_tokens off;

     log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
     access_log logs/nginx/access.log l2met;
     error_log logs/nginx/error.log; 

     include mime.types;
     default_type application/octet-stream;
     sendfile on;

     server {
         listen <%= ENV['PORT'] %>;
         server_name _; 
         keepalive_timeout 5; 
         root /app/www;
         index index.html;

         location / {
             autoindex on; 
         }
     }
}

I also set PORT variable to 80

 heroku config:get PORT
 80

Some other configuration:

 heroku config:get NGINX_WORKERS
 8
 heroku buildpacks
 https://github.com/heroku/heroku-buildpack-multi.git
 heroku stack
 cedar-14

My .buildpack file

https://github.com/moohkooh/nginx-buildpack
https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/ruby.tgz

I also have the guess, that heroku dont use my variable that i set to 80. Whats wrong? Big thanks for anyone.

Btw: my express server running without any error on port 1000 (for test i start it also on port 80 without any errors)

回答1:

i solved my problem with this configuration.

daemon off;
#Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;

pid nginx.pid;

events { 
    worker_connections 1024; 
}

http {
    gzip on;
    gzip_comp_level 2;
    gzip_min_length 512; 
    server_tokens off;
    log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
    access_log logs/nginx/access.log l2met;
    error_log logs/nginx/error.log; 

    include mime.types;

    server {
        listen <%= ENV['PORT'] %>;
        server_name localhost;
        port_in_redirect off;
        keepalive_timeout 5; 
        root /app/www; 
        index index.html;

        location / {
            autoindex on; 
        }
    }
}


标签: nginx heroku