Question

How to setup Nginx to display Ruby on Rails Webpage?

While I was building my virtual petsite I followed the following articles listed below to setup my new web server. https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04#step-1-installing-certbot https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04 https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-22-04 https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-22-04#option-2-installing-node-js-with-apt-using-a-nodesource-ppa https://docs.digitalocean.com/products/droplets/how-to/tag/ https://www.writesoftwarewell.com/how-http-cookies-work-rails/ https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-22-04 https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-20-04

These articles worked really well, and I have both Ruby on Rails and Nginx active. The problem is that I am having a difficulty in getting Ruby on Rails to be displayed on https://dev.duelingpets.net.

Instead of the Ruby on Rails file, I am getting this index file from Nginx to be displayed: Success! The Duelingpets server block is working!

My current config for the nginx looks like this:

server {

        root /var/www/duelingpets.net/html;
        index index.html index.htm index.nginx-debian.html;

        server_name duelingpets.net dev.duelingpets.net;

        location / {
                try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/duelingpets.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/duelingpets.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = dev.duelingpets.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = duelingpets.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name duelingpets.net dev.duelingpets.net;
    return 404; # managed by Certbot




}


duelingpets@DuelingpetsWS1:/etc/nginx/sites-available

When I try to change the root file from root /var/www/duelingpets.net/html; to root /var/www/duelingpets.net/html/Duelingpets/public, I get: 403 Forbidden:

nginx/1.24.0 (Ubuntu) Message to pop up.

I checked the permissions and they appear to be in working order. I want it to point to my Ruby on Rails website on dev.duelingpets.net but I can’t figure out how to do so. I also heard that Passenger is obsolete with the newer versions of Nginx so I was wonder what to use to get the Rails website to display in the browser?

Any advice would be greatly appreciated on how to get this to work.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
March 6, 2024

Heya @ebeecroft,

I’m starting this new thread as the other was too long and couldn’t be followed anymore.

As for the errors you’ve received:

=> Booting Puma => Rails 7.1.3 application starting in development => Run `bin/rails server --help` for more startup options Rails Error: Unable to access log file. Please ensure that /var/www/duelingpets.net/Duelingpets/log/development.log exists and is writable (i.e. make it writable for user and group: chmod 0664 /var/www/duelingpets.net/Duelingpets/log/development.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed. Exiting /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_file.rb:122:in `binread': Permission denied @ rb_sysopen - /var/www/duelingpets.net/Duelingpets/config/master.key (Errno::EACCES) from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_file.rb:122:in` binread’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_file.rb:122:in `read_key_file' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_file.rb:53:in` key’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_file.rb:71:in `read' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_configuration.rb:57:in` read’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_configuration.rb:76:in `config' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/encrypted_configuration.rb:95:in` options’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/core_ext/module/delegation.rb:332:in `method_missing' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3/lib/active_record/railtie.rb:384:in` block (2 levels) in [class:Railtie](class:Railtie)’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/lazy_load_hooks.rb:94:in `block in execute_hook' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/lazy_load_hooks.rb:87:in` with_execution_control’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/lazy_load_hooks.rb:92:in `execute_hook' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/lazy_load_hooks.rb:78:in` block in run_load_hooks’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/lazy_load_hooks.rb:77:in `each' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3/lib/active_support/lazy_load_hooks.rb:77:in` run_load_hooks’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/application/finisher.rb:93:in `block in <module:Finisher>' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/initializable.rb:32:in` instance_exec’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/initializable.rb:32:in `run' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/initializable.rb:61:in` block in run_initializers’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:231:in `block in tsort_each' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:353:in` block (2 levels) in each_strongly_connected_component’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:434:in `each_strongly_connected_component_from' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:352:in` block in each_strongly_connected_component’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:350:in `each' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:350:in` call’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:350:in `each_strongly_connected_component' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:229:in` tsort_each’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/tsort.rb:208:in `tsort_each' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/initializable.rb:60:in` run_initializers’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/application.rb:426:in `initialize!' from /var/www/duelingpets.net/Duelingpets/config/environment.rb:5:in` <main>’ from [config.ru:3](http://config.ru:3/):in `require_relative' from config.ru:3:in` block in <main>’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rack-3.0.9/lib/rack/builder.rb:103:in `eval' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rack-3.0.9/lib/rack/builder.rb:103:in` new_from_string’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rack-3.0.9/lib/rack/builder.rb:94:in `load_file' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rack-3.0.9/lib/rack/builder.rb:64:in` parse_file’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rackup-2.1.0/lib/rackup/server.rb:354:in `build_app_and_options_from_config' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rackup-2.1.0/lib/rackup/server.rb:263:in` app’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rackup-2.1.0/lib/rackup/server.rb:424:in `wrapped_app' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:76:in` log_to_stdout’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:36:in `start' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:145:in` block in perform’ from [internal:kernel](internal:kernel):90:in `tap' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:136:in` perform’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in` invoke_command’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/command/base.rb:178:in `invoke_command' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.0/lib/thor.rb:527:in` dispatch’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/command/base.rb:73:in `perform' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/command.rb:71:in` block in invoke’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/command.rb:149:in `with_argv' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/command.rb:69:in` invoke’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3/lib/rails/commands.rb:18:in `<main>' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in` require’ from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require' from /home/duelingpets/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in` require’ from bin/rails:4:in `<main>’

Application config: require_relative “boot”

require “rails/all”

# [Require the gems listed in Gemfile, including any gems](https://www.digitalocean.com/community/questions/how-to-setup-nginx-to-display-ruby-on-rails-webpage?comment=202920#require-the-gems-listed-in-gemfile-including-any-gems)[](https://www.digitalocean.com/community/questions/how-to-setup-nginx-to-display-ruby-on-rails-webpage?comment=202920#require-the-gems-listed-in-gemfile-including-any-gems)

# [you’ve limited to :test, :development, or :production.](https://www.digitalocean.com/community/questions/how-to-setup-nginx-to-display-ruby-on-rails-webpage?comment=202920#you-ve-limited-to-test-development-or-production)[](https://www.digitalocean.com/community/questions/how-to-setup-nginx-to-display-ruby-on-rails-webpage?comment=202920#you-ve-limited-to-test-development-or-production)

Bundler.require(*Rails.groups)

module Duelingpets class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.1

It seems like your permissions and ownership might be wrong. Try to set the permissions of your folders to 755 and of files to 644.

Additionally, regarding not being able to load your Ruby on Rails app via your Droplet’s IP

The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer’s network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the web.

See if you’ve allowed the App to be accessed by your Droplet’s IP address.

KFSys
Site Moderator
Site Moderator badge
February 4, 2024

Heya @ebeecroft,

If I’m not mistaken, the APP with Ruby on Rails runs on a specific port, is that correct?

Configuring Nginx as a reverse proxy for a Ruby on Rails application is a common and recommended setup, especially in production environments.

Here is an example config:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000; # Assuming Rails is running on port 3000
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Considerations

  • Environment: The specifics of your Nginx configuration can vary based on your server environment, domain setup, and specific requirements of your Rails application.

  • SSL Configuration: If you are serving your application over HTTPS, you will need to include SSL configuration in your Nginx setup.

  • Rails Server Configuration: Ensure your Rails server (like Puma or Unicorn) is configured correctly to work with Nginx as a reverse proxy.

  • Testing: After configuration, thoroughly test your application to ensure that everything works as expected, especially the routing, static assets, and any websockets or long-polling features.

Using Nginx as a reverse proxy for Ruby on Rails can significantly enhance the performance, scalability, and security of your application, making it a worthwhile setup for most production environments.

Bobby Iliev
Site Moderator
Site Moderator badge
February 4, 2024

Hi there,

Currently, your website seems to be up and running, can you confirm if this is also the case on your side as well?

Besides that, configuring Nginx to serve a Ruby on Rails application can be a bit tricky, especially when transitioning from development to production environments. Your scenario indicates that Nginx is correctly installed and serving static files, but it’s not properly configured to serve your Rails application.

Here are a few things that you can check:

  • Check Nginx and Passenger logs: Nginx’s error.log (it is usually found in /var/log/nginx/error.log) can provide insights. Passenger’s logs can also be helpful.
  • Restart Nginx after changes: Always restart Nginx after making configuration changes (sudo systemctl restart nginx).
  • Check for syntax errors: Use nginx -t to check your Nginx configuration for syntax errors.

Feel free to share the errors here!

Besides that, here are a few more things that I would recommend checking:

1. Verify Passenger Installation

Even though you’ve heard Passenger might be obsolete with newer versions of Nginx, it’s still a viable and popular option for deploying Ruby on Rails applications. First, ensure that Passenger is installed and integrated with Nginx.

  • Install Passenger (if you haven’t already) by following the official Passenger documentation. You will need to install the Passenger library and the Nginx module.
  • Verify the installation by running passenger-config validate-install and passenger-config about nginx-module.

2. Configure Nginx with Passenger

Modify your Nginx configuration to use Passenger. Here’s a simplified version of what your server block might look like:

server {
    listen 80;
    listen [::]:80;
    server_name duelingpets.net dev.duelingpets.net;
    return 301 https://$host$request_uri; # Redirect HTTP to HTTPS
}

server {
    listen 443 ssl;
    listen [::]:443 ssl ipv6only=on;
    server_name duelingpets.net dev.duelingpets.net;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/duelingpets.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/duelingpets.net/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Root directory
    root /var/www/duelingpets.net/html/Duelingpets/public;

    # Passenger
    passenger_enabled on;
    passenger_ruby /path/to/your/ruby; # Use `rbenv which ruby` to find this path if you're using rbenv

    # If you're still seeing the default Nginx page, try commenting out the default try_files line
    # try_files $uri $uri/ =404;
}

3. Permissions and Ownership

The 403 Forbidden error when changing the root directory often points to a permissions issue. Ensure that the user running Nginx (often www-data) has read access to the /var/www/duelingpets.net/html/Duelingpets/public directory and all its parents.

  • Use chmod and chown to adjust permissions and ownership if necessary.
  • Verify permissions with ls -l /var/www/duelingpets.net/html/Duelingpets and ensure that directories have at least 755 and files have 644.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel