【问题标题】:Dokku Nginx Serve Static FilesDokku Nginx 提供静态文件
【发布时间】:2016-01-10 07:46:29
【问题描述】:

我正在尝试在 dokku 上设置 node.js webapp 的部署。但是,我的应用程序的根路径只需要提供静态 html、css 和 javascript,所有其他路由“/api/...”都应该发送到我在端口 5000 上运行的节点应用程序。我试过使用以下 nginx.ssl.conf.template 进行设置:

http {
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
    proxy_temp_path /var/tmp;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;

    upstream ceresshop_api {
      server 127.0.0.1:5000;
      keepalive 64;
}
server {
  listen      [::]:$NGINX_PORT;
  listen      $NGINX_PORT;
  server_name $NOSSL_SERVER_NAME;
  access_log  /var/log/nginx/${APP}-access.log;
  error_log   /var/log/nginx/${APP}-error.log;
  return 301 https://\$host:$NGINX_SSL_PORT\$request_uri;
}

server {
  listen      [::]:$NGINX_SSL_PORT ssl spdy;
  listen      $NGINX_SSL_PORT ssl spdy;
  server_name $SSL_SERVER_NAME;
  access_log  /var/log/nginx/${APP}-access.log;
  error_log   /var/log/nginx/${APP}-error.log;
$SSL_DIRECTIVES

  keepalive_timeout   70;
  add_header          Alternate-Protocol  $NGINX_SSL_PORT:npn-spdy/2;
  location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
         root /data/public;
         access_log off;
         expires max;
       }
  location    / {

    gzip on;
    gzip_min_length  1100;
    gzip_buffers  4 32k;
    gzip_types    text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml  application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
    gzip_vary on;
    gzip_comp_level  6;

    proxy_pass  http://$APP;
    proxy_http_version 1.1;
    proxy_set_header Upgrade \$http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host \$http_host;
    proxy_set_header X-Forwarded-Proto \$scheme;
    proxy_set_header X-Forwarded-For \$remote_addr;
    proxy_set_header X-Forwarded-Port \$server_port;
    proxy_set_header X-Request-Start \$msec;
  }
  include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf;
}

但是,我不确定将“根”参数指向哪里

location ~ ^/(images/|img/|javascripts/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico|index.html) {
         root /data/public;
         access_log off;
         expires max;
       }

因为应用程序是使用 dokku 部署的。对于上下文,我尝试使用 ngix 静态提供的文件位于我的应用程序目录的“公共”目录中。

【问题讨论】:

标签: node.js nginx dokku


【解决方案1】:

nginx 配置在容器外部,因此无法访问容器内部的静态文件。

如果需要,您可以将这些文件移动到容器中的已安装卷中,然后将 nginx 指向该已安装卷。否则,您的应用程序将需要以节点应用程序通常这样做的任何方式为它们提供服务。我会研究一下 heroku 的 nodejs buildpack 规定的最佳选择。

【讨论】:

  • 我尝试使用 dokku 的 docker 选项功能将公用文件夹挂载到我的应用程序目录中,以便 docker 选项等同于:Deploy options: -v /home/dokku/public:/app/public Run options: -v /home/dokku/public:/app/public 但是当我部署我的 /home/dokku/public 文件夹后项目,目录为空。
  • 当您使用 docker 选项时,该目录中已经存在的文件不会从该目录复制到主机上。您需要在部署过程中以某种方式处理将文件复制到该目录。
猜你喜欢
  • 2018-10-23
  • 2021-09-03
  • 2018-05-18
  • 2015-07-30
  • 2021-12-02
  • 2020-09-14
  • 2019-06-27
  • 2018-05-17
  • 2013-02-19
相关资源
最近更新 更多