Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

NGINX is not catching the request in production with Ruby on rails + Docker

I have deployed a Ruby on Rails API to AWS ECS with Docker + NGINX configured. I have followed the link deploy-web-apps-nginx-to-ecs-with-docker

I did everything mentioned in above link successfully. When I am trying to hit a request to deployed URI, Getting nothing. I have tried to look at nginx logs but files are not exists at server (connect the AWS instance by ssh).

Can any one help me that

  1. How can I find nginx logs at server accessed by ssh?
  2. How can I find application's code at server?

Below are the file's content related to docker and nginx configurations.

  1. Dockerfile (app)
# Retrieve the ruby image
FROM ruby:2.7.1

# Install basic required libs
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN apt-get install -y libpq-dev

# Install bundler
RUN gem install bundler --version "2.1.4"

# Set an environment variable for the Rails app root folder
ENV RAILS_ROOT /var/www/vbdapp-assignment

# Create the working directory
RUN mkdir -p $RAILS_ROOT

# Set working directory
WORKDIR $RAILS_ROOT

# Set production env variables and enabling the option to serve static files
ENV RAILS_ENV='production'
ENV RACK_ENV='production'

# Add Gemfile
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock

# Install gems. -j $(nproc) runs bundle with to total amount of CPU cores/threads available
RUN bundle install -j $(nproc)

# Add Rails project files
COPY . .

# Expose port 3000 to be available for NGINX reverse proxy
EXPOSE 3000

# Start the main process with Puma
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

  1. Dockerfile (web)
# Retrieve the nginx base image
FROM nginx

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

# Set env variable where NGINX should look for project files
ENV RAILS_ROOT /var/www/vbdapp-assignment

# Set working directory
WORKDIR $RAILS_ROOT

# Create log directory
RUN mkdir log

# copy over static assets
COPY public public/

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

RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.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;" ]
  1. nginx.conf
# 1.Upstream used to define groups of servers, in this case the rails app, that can be referenced by the proxy_pass
upstream rails_app {
    server app:3000;
}

# 2.Server part
server {
   # 2.2.Specify your public IP Address.
   server_name 35.166.220.71;

   # 2.1.Listen to incoming connection on port 80
    listen 80;
    listen [::]:80;
    return 302 http://$server_name$request_uri;


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

   # 2.4.Specify where Nginx should write its logs
   access_log /var/log/nginx/access.log;
   error_log /var/log/nginx/error.log;

   # 2.5.Deny requests for files that should never be accessed such as .rb or .log files
   location ~ /. {
      deny all;
   }
   location ~* ^.+.(rb|log)$ {
      deny all;
   }

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

   # 2.7.reverse proxy redirecting the request to the rails app, port 3000.
   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;
      proxy_read_timeout 900;
   }
}
  1. docker-compose.yml
# 1.Specify the compose file format; we're using the last one.
#   If you're curious, at https://docs.docker.com/compose/compose-file/compose-versioning/
#   you can find an extensive description on the difference among versions
version: '3'
# 2.Specify the list of services that will be built. In our case, app (Ruby on Rails) and web (NGINX)
services:
  db:
    image: postgres:12-alpine
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      DB_USERNAME: postgres
      DB_PASSWORD: postgres

  app:
    # Repository URI for our app image
    image: 264434790367.dkr.ecr.us-west-2.amazonaws.com/docker-demo
    build:
      # 2.1.Specify the root path
      context: .
      # 2.2.From the root path defined in context, here we define where the Dockerfile resides
      dockerfile: ./docker/app/Dockerfile
    depends_on:
      - db

  web:
    # Repository URI for our web image
    image: 264434790367.dkr.ecr.us-west-2.amazonaws.com/docker-demo-web
    build:
      context: .
      dockerfile: ./docker/web/Dockerfile
    # 2.4.Binds port 80 for public connections
    depends_on:
      - app
    ports:
      - "80:80"
question from:https://stackoverflow.com/questions/65623581/nginx-is-not-catching-the-request-in-production-with-ruby-on-rails-docker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...