[英]heroku + nginx on port 80
我正在嘗試在 heroku 免費環境上啟動 nginx 服務器。 我准備好了任何操作方法和教程,但我沒有讓它運行。
首先,我想在端口 80 上啟動 nginx 作為默認 Web 服務器。之后我想將 nginx 配置為 underline express 服務器(其他 heroku 實例)的代理。 4 天來,我試圖在我的 heroku 實例上僅啟動 nginx。 我總是得到不允許在端口 80 上啟動的異常。我從 ( https ://github.com/benmurden/nginx-pagespeed) 分叉了 nginx-buildback ( https://github.com/moohkooh/nginx-buildpack ) -buildpack ) 來調整一些配置。 如果我通過 heroku bash 在端口 2555 上運行 nginx,nginx 正在啟動,但我在網絡瀏覽器上連接被拒絕。
如果我通過 Dyno 啟動 nginx,我會在日志中收到錯誤消息
State changed from starting to crashed
我的 Dyno 的 Procfile
web: bin/start-nginx
我的 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;
}
}
}
我還將 PORT 變量設置為 80
heroku config:get PORT
80
一些其他配置:
heroku config:get NGINX_WORKERS
8
heroku buildpacks
https://github.com/heroku/heroku-buildpack-multi.git
heroku stack
cedar-14
我的.buildpack文件
https://github.com/moohkooh/nginx-buildpack
https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/ruby.tgz
我也有猜測,即 heroku 不使用我設置為 80 的變量。有什么問題嗎? 非常感謝任何人。
順便說一句:我的快速服務器在端口 1000 上運行時沒有任何錯誤(為了測試我也在端口 80 上啟動它沒有任何錯誤)
我用這種配置解決了我的問題。
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 容器的人(在我的例子中是 React App):
在這張 docker 圖像的幫助下,我能夠做到這一點。 您將需要在容器內名為/etc/nginx/conf.d/default.conf.template的文件中包含以下內容:
server {
listen $PORT;
listen [::]:$PORT;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
然后在您的Dockerfile中,使用:
CMD /bin/sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'
現在當你運行時:
> heroku container:push
> heroku container:release
NGINX 將使用 Heroku 分配的端口來分發您的應用程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.