NeuroAgent

How nginx determines the homepage without index.html

Learn how nginx determines the homepage when there's no index.html file. The role of Passenger and Rails in request processing. Step-by-step diagnostics and troubleshooting.

Where does nginx get the main page of my site in my case?

I can’t understand where nginx gets the content to display the site. Here is my configuration:

Main nginx config (/etc/nginx/nginx.conf):

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 20M;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_disable "msie6";
    include /etc/nginx/passenger.conf;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

My site config (/etc/nginx/conf.d/site.conf):

server {
    listen 80;
    server_name www.site.ru site.ru;
    root /home/deploy/projects/site.ru/current/public;
    passenger_enabled on;
    rails_env production;
    location ~ ^/assets/ {
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";
        break;
    }
}

Contents of the site directory (/home/deploy/projects/site.ru/current/public):

404.html  500.html  favicon.ico  robots.txt  uploads
422.html  assets    favicon.png  system      yandex_54a321xz98c2cb13.html

There is no explicit index.html file in the site directory, but the site still displays. How does nginx determine which page to show as the main page? Where am I wrong in my understanding of how nginx works?

NeuroAgent

If there is no index.html file in your /home/deploy/projects/site.ru/current/public directory, but the site still displays, then nginx passes control to Passenger, which in turn passes control to the Rails application to handle the routes.


Contents


How nginx determines the main page

When nginx receives a request for the root URL (/), it performs the following actions:

  1. Checks for static files in the directory specified in the root directive (/home/deploy/projects/site.ru/current/public)
  2. Looks for files specified in the index directive (by default: index.html index.htm)
  3. If it finds index.html - serves it as a static file
  4. If it doesn’t find index.html - passes control to Passenger

In your case, since there is no index.html file in the public directory, nginx passes control to Passenger, which then starts your Rails application.


Role of Passenger in request processing

Passenger works as an nginx module and automatically handles Rails applications:

Once you have obtained the value, open your Nginx configuration file and insert a passenger_root option somewhere with that value. This option enables or disables Passenger for that particular context. source

When Passenger receives a request, it:

  1. Starts the Rails application (if it’s not already running)
  2. Passes the request to the Rails router through the Rack interface
  3. Rails determines the route via the config/routes.rb file
  4. Calls the appropriate controller and action

In your case, Rails most likely has a route like root 'pages#home' or similar, which specifies which controller action should handle the root URL.


Correct configuration for Rails applications

Your configuration is correct for a Rails application:

nginx
server {
    listen 80;
    server_name www.site.ru site.ru;
    root /home/deploy/projects/site.ru/current/public;
    passenger_enabled on;
    rails_env production;
}

Important points:

  • The root directive should point to the public directory of your Rails application
  • passenger_enabled on; enables Passenger support for this server
  • rails_env production; sets the Rails environment

In the above app we have created a variable response in pages_controller.rb which has the value "Hello World!. This variable is used to display the Hello World from the file app/views/pages/home.html.erb source


Step-by-step diagnosis

To understand exactly where the homepage content is coming from, perform the following steps:

1. Check the routes file

Open the config/routes.rb file in your Rails project. Look for a line like:

ruby
root 'pages#home'
# or
root 'home#index'

2. Check the corresponding controller

If the routes specify pages#home, check the app/controllers/pages_controller.rb file:

ruby
class PagesController < ApplicationController
  def home
    @response = "Hello World!"
  end
end

3. Check the view

Check the app/views/pages/home.html.erb file or the corresponding file for your route.


Solving display problems

If the site displays content you don’t expect, possible solutions include:

1. Check Rails logs

Review Rails logs for diagnostics:

bash
tail -f log/production.log

2. Check Passenger configuration

Ensure Passenger is properly configured:

Inside the location block, set passenger_app_root source

3. Check access permissions

Ensure the user running Passenger has access to the application directories.


Best practices for Rails + Nginx + Passenger

1. Directory structure

/home/deploy/projects/site.ru/current/
├── app/
│   ├── controllers/
│   ├── views/
│   └── assets/
├── config/
│   └── routes.rb
└── public/          # nginx root points here
    ├── assets/
    ├── 404.html
    ├── 500.html
    └── robots.txt

2. Optimal nginx configuration

nginx
server {
    listen 80;
    server_name www.site.ru site.ru;
    
    # Important: point to the public directory
    root /home/deploy/projects/site.ru/current/public;
    
    passenger_enabled on;
    rails_env production;
    
    # Optimization for static files
    location ~ ^/assets/ {
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";
        break;
    }
    
    # Error handling
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}

3. Verification

The settings are explained below. In this file, we enable listening on port 80, set your domain name, enable Passenger, and set the root to the public directory of our new project source

To verify that the homepage is configured correctly:

  1. Temporarily create an index.html file in the public directory
  2. Verify that nginx serves it
  3. Remove index.html and ensure Passenger passes control to Rails

Sources

  1. How to configure nginx with passenger with rails application - Stack Overflow
  2. Configuration reference - Nginx - Passenger Library
  3. Deploying a Ruby application - Nginx - Passenger Library
  4. How To Deploy a Rails App with Passenger and Nginx on Ubuntu 14.04 - DigitalOcean
  5. Setup Rails with Nginx using Passenger - TO THE NEW Blog

Conclusion

The homepage of your site is determined by Rails routing, not by a static index.html file. Since you don’t have an index.html in the public directory, nginx passes control to Passenger, which in turn accesses your Rails application.

To check which page is displayed as the homepage:

  1. Look at the config/routes.rb file - there should be a route for the root URL
  2. Check the corresponding controller and view
  3. Review Rails logs to diagnose issues

Your nginx configuration is correct - it points to the public directory and enables Passenger. If the content doesn’t display as expected, the issue is most likely in your Rails application code, not in the nginx configuration.