无法打开到lvh的TCP连接:[port]连接在DOCKER RAILS NGINX上被HTTParty.get拒绝

时间:2018-06-06 18:51:03

标签: ruby-on-rails docker nginx docker-compose httparty

我正在尝试使用docker-compose设置docker环境 rails(运行puma),nginx,mysql,elasticsearch。

但是当我尝试使用HTTParty.get('http://lvh.me:8888')

来调用它时

它失败了,我收到了一条错误消息

  

Errno :: ECONNREFUSED(无法打开到lvh.me:8888的TCP连接   (连接被拒绝 - 连接(2)“lvh.me”端口8888))

我的Docker-Compose文件:

version: '3.3'
services:
    beecomredis:
        image: redis:4.0.8
        ports:
            - "6379:6379"
    beecomdb:
        image: mysql:5.7.21
        volumes:
            - ./mysql_data/mysql:/var/lib/mysql
        ports:
            - "6603:3306"
    beecomec:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.2.2
        #container_name: elasticsearch
        environment:
          - http.cors.enabled=true
          - http.cors.allow-origin="*"
          - node.master=true
          - cluster.name=docker-cluster
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - xpack.security.enabled=false
        ulimits:
          memlock:
            soft: -1
            hard: -1
        volumes:
            - ./elastic_data/elasticsearch/data:/usr/share/elasticsearch/data
            - ./elastic_data/elasticsearch/logs:/usr/share/elasticsearch/logs
        ports:
            - '9200:9200'
            - '9300:9300'
    beecomnginx:
        build:
          context: .
          dockerfile: Dockerfile-nginx
        ports:
          - "8888:80"
    beecom:
        build: .
        command: foreman start
        volumes:
            - .:/beecom
         expose:
            - "3000"
        depends_on:
            - beecomdb
            - beecomredis
            - beecomec
            - beecomnginx

我的nginx.conf文件:

upstream rails_app {
   server beecom:3000;
}

server {
   # define your domain
   server_name www.example.com;

# define the public application root
   root   $RAILS_ROOT/public;
   index  index.html;

# define where Nginx should write its logs
   access_log $RAILS_ROOT/log/nginx.access.log;
   error_log $RAILS_ROOT/log/nginx.error.log;

   # deny requests for files that should never be accessed
   location ~ /\. {
      deny all;
   }

   location ~* ^.+\.(rb|log)$ {
      deny all;
   }

   # serve static (compiled) assets directly if they exist (for rails production)
   location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/   {
      try_files $uri @rails;
      access_log off;
      gzip_static on;

      # to serve pre-gzipped version
      expires max;
      add_header Cache-Control public;

      add_header Last-Modified "";
      add_header ETag "";
      break;
   }

   # send non-static file requests to the app server
   location / {
      try_files $uri @rails;
   }

   location @rails {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://rails_app;
   }
}

我的DockerFile:

FROM ruby:2.5.0-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
 build-essential \
 default-libmysqlclient-dev \
 mysql-client \
 libmagickwand-dev \
 imagemagick \
 curl \
 git \
 gnupg2 \
/sources.list.d/yarn.list \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN curl -sL https://deb.nodesource.com/setup_9.x | bash - && apt-get install -y --no-install-recommends nodejs

RUN gem update --system

RUN mkdir -p /beecom
WORKDIR /beecom

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
COPY package.json package.json

ENV RAILS_ENV development
ENV RACK_ENV development

RUN bundle install

RUN set :environment, 'development'

COPY config/puma.rb config/puma.rb
COPY . /beecom

EXPOSE 3000

CMD [ "foreman", "start" ]

最后我的Dockerfile-nginx:

# Base image:
FROM nginx

# Install dependencies
RUN apt-get update -qq && apt-get -y install apache2-utils

# establish where Nginx should look for files
ENV RAILS_ROOT /beecom

# Set our working directory inside the image
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT

# create log directory
RUN mkdir log

# copy over static assets
COPY ./public public/

# Copy Nginx config template
COPY ./config/nginx.conf /tmp/docker.nginx

# substitute variable references in the Nginx config template for real values from the environment
# put the final config in its place
 RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf
RUN rm -rf /etc/nginx/sites-available/default
ADD config/nginx.conf /etc/nginx/sites-enabled/nginx.conf

EXPOSE 80

# Use the "exec" form of CMD so Nginx shuts down gracefully on SIGTERM (i.e. `docker stop`)
CMD [ "nginx", "-g", "daemon off;" ]

哦,我忘了发布我的Puma配置:

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum, this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
#
port        ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 3 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted this block will be run, if you are using `preload_app!`
# option you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, Ruby
# cannot share connections between processes.
#
# on_worker_boot do
#   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

1 个答案:

答案 0 :(得分:0)

最后我发现在我的config/puma.rb文件中添加了一个绑定。

bind 'tcp://0.0.0.0:8888'

解决了我的问题。

更新: 最后,我发现修复此问题的最佳方法是更改​​我的nginx.config,如下所示:

upstream MyAppUpstream_beecommercial {
   server beecom:3000;
}

server {
  listen 443 ssl;
  listen      80;
  server_name lvh.me
  keepalive_timeout 900;

  ssl_certificate      /[key];
  ssl_certificate_key  /[key];

# define the public application root
   root   $RAILS_ROOT/public;
   index  index.html;

# define where Nginx should write its logs
   access_log $RAILS_ROOT/log/nginx.access.log;
   error_log $RAILS_ROOT/log/nginx.error.log;

   # deny requests for files that should never be accessed
   location ~ /\. {
      deny all;
   }

   location ~* ^.+\.(rb|log)$ {
      deny all;
   }

   # serve static (compiled) assets directly if they exist (for rails production)
   location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/   {
      try_files $uri @rails;
      access_log off;
      gzip_static on;

      # to serve pre-gzipped version
      expires max;
      add_header Cache-Control public;

      add_header Last-Modified "";
      add_header ETag "";
      break;
   }

   # send non-static file requests to the app server
   location / {
      try_files $uri @rails;
   }

   location @rails {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://MyAppUpstream_beecommercial;

   }
}