I'm trying to serve a specific Rails route for the default Nginx page. I have an app that allows users to create sites, but the site only becomes available under certain conditions, so until those conditions are met, the Nginx server block for that site is not created, meaning if they try to visit their site using their domain they end up seeing the Nginx default page (Welcome to Nginx).
What i'm trying to do is make Nginx serve one of the Rails routes to make it a bit more user friendly and avoid unnecessary support questions for those that don't read the instructions (which is basically all of them).
i've been trying different configs in the default Nginx config file, but to no avail, Nginx always ends up serving the root page of the Rails app instead of the specific route i want (/almost-there):
/etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/railsapp/public;
passenger_enabled on;
rails_env production;
index almost-there.html;
server_name _;
location / {
index almost-there.html;
}
}
Note that almost-there.html is a rails route, not an actual static html file.
I do not use this framework, but you probably need to redirect visitors to /almost-there.html but serve the /index.html as usual.
For example:
index index.html;
location = / {
return 302 /almost-there.html;
}
location / {
try_files $uri $uri/ /index.html;
}
Alternatively, to redirect all routes:
index index.html;
location / {
try_files $uri $uri/ @redirect;
}
location @redirect {
rewrite ^/almost-there\.html$ /index.html last;
return 302 /almost-there.html;
}
So what I ended up doing was removing the location block entirely, as well as the index directive and instead redirecting the / to the Rails route that I wanted to display:
rewrite ^/$ /almost-there redirect;
Important to note that this is a temporary redirect instead of a permanent one as we don't want browsers to cache this redirect to avoid problems once the real site is ready to be displayed.