[RUBY] After deploying a Rails app on EC2, I can access it with a public IP address but not with a public DNS

As the title says.

Conclusion

Only the IP address was described in server_name of the nginx configuration file

Event

I deployed the Rails app on AWS EC2 by referring to the article below. Reference: The most polite AWS commentary in the world. Until you bring your Rails app to AWS using EC2

Check and access the public IPv4 address from the AWS management console. The top page was displayed safely!

Then check the public IPv4 DNS and access!

スクリーンショット 2020-12-19 19.33.32.png

that.

Gugu

The displayed screen is not like an error message ...

After checking, it seems that the default page is displayed when accessed with a host name that is not set in nginx. In the case of AWS EC2, the above default page prepared by AWS is displayed instead of the default page of nginx. Since nginx can be accessed without problems and it can be accessed by IP address, it is assumed that there is a problem with the setting to connect from nginx to EC2.

afae5f09fa553c5c88a81979f3345565.png

Solution procedure

After checking, it is said that the above event will occur if the host name is not described in the nginx configuration file, so first check the configuration file.

Ruby:/etc/nginx/conf.d/Trading2.conf


# log directory
error_log  /var/www/rails/Trading2/log/nginx.error.log;
access_log /var/www/rails/Trading2/log/nginx.access.log;
# max body size
client_max_body_size 2G;
upstream app_server {
  # for UNIX domain socket setups
  server unix:/var/www/rails/Trading2/tmp/sockets/.unicorn.sock fail_timeout=0;
}
server {
  listen 80;
  server_name xx.xx.xx.xx; #The host name must be listed here
  # nginx so increasing this is generally safe...
  keepalive_timeout 5;
  # path for static files
  root /var/www/rails/Trading2/public;
  # page cache loading
  try_files $uri/index.html $uri.html $uri @app;
  location @app {
    # HTTP headers
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
  # Rails error pages
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /var/www/rails/Trading2/public;
  }
}

Certainly only the IP address was listed. Immediately change the relevant part to the public host name, type the command and restart nginx.

Ruby:/etc/nginx/conf.d/Trading2.conf


  server_name ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com; #Enter the host name
sudo nginx -s reload
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64

This time nginx failed to restart. A message was displayed asking me to add'server_names_hash_bucket_size: 64'. Apparently, if the server name is too long, it cannot be hashed with the specified hash value, so it seems that you should increase the length of the hash value.

I tried to describe it as I was told. Is this strange?

Ruby:/etc/nginx/conf.d/Trading2.conf


  server_name ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com; #Enter the host name
  server_names_hash_bucket_size: 64
sudo nginx -s reload
ginx: [emerg] unknown directive "server_names_hash_bucket_size:" in/etc/nginx/conf.d/Trading2.conf:17

Another error. I wonder if the place where it is listed is different ...

After that, I tried to describe it in various places, but I got the same error, so I think that the conf file in the application folder is not good, so I will also describe it in /etc/nginx/nginx.conf.

/etc/nginx/nginx.conf


    keepalive_timeout   65;
    server_names_hash_bucket_size 128;# I tried to describe it here
    types_hash_max_size 2048;

I was able to reboot this time!

sudo nginx -s reload

When I accessed it with the host name, the top page of the app was displayed safely! Also, I rewrote the IP address in the configuration file to the host name, so I confirmed that I could not access with the IP address.

Lesson

Don't assume that you've copied someone else's config file in its entirety.

Recommended Posts

After deploying a Rails app on EC2, I can access it with a public IP address but not with a public DNS
Try deploying a Rails app on EC2-Part 1-
I made a portfolio with Ruby On Rails
[Rails] I tried to create a mini app with FullCalendar
When I used Slick on Rails, it competed with Turbolinks.
[Rails] I made a simple calendar mini app with customized specifications.
I tried deploying a Docker container on Lambda with Serverless Framework