Why do I have to manually start a passenger instance for nginx sub domain to see my app?

October 4, 2019 87 views
Ruby on Rails Nginx

I have a rails api app and a static react front-end app and I have been trying to get the rails app to run on a subdomain for over 24 hrs now. I got it all hooked up yesterday and ended with a 302 bad gateway error, I got back to it this morning and did a number of things including changing the owner of the app files and finally tried manually starting a passenger instance which worked.

I have another rails app deployed on another droplet and it doesn’t seem to need me to manually start passenger, why is it that for my api I need to do so?

Here is the config files in case someone has a better way to set all of this up:

upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }

server {
    listen   80;
    root /var/www/my-rails-app/public;
    server_name api.my-domain.net;
#    index index.htm index.html;

    passenger_enabled on;
    passenger_ruby /usr/local/rvm/gems/ruby-2.6.4/wrappers/ruby;

    passenger_env_var POSTGRES_USER 'database_username';
    passenger_env_var POSTGRES_PASSWORD 'database_password';
    passenger_env_var POSTGRES_HOST 'localhost';
    passenger_env_var POSTGRES_DB 'database_name';
    passenger_env_var SECRET_KEY_BASE secret_key;


    location / {
        try_files $uri/index.html $uri.html $uri @app;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                    try_files $uri @app;
    }

    location @app {
        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;
    }
}

server {
    listen   80;
    root /var/www/my-rails-app/client/build;
    server_name my_domain.net www.my-domain.net;
    index index.htm index.html;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

I finally got the app to run with this config once I cd’d into my app folder and manually started passenger using this command:

passenger start -a 127.0.0.1 -p 8080 -d -e production

Any Idea why that was necessary when it’s not necessary on my other rails app? Is there a better way to do this?

1 Answer

Hello,

Passenger checks whether the virtual host is a Passenger-supported application. If it fails then Passenger will let Nginx serve the virtual host root as if Passenger is not activated.

The good thing is that you could override the autodetection so that it matches your needs.

For more information I would recommend going through the documentation here:

https://www.phusionpassenger.com/library/indepth/meteor/app_autodetection/nginx/#autodetection-algorithm

Regards,
Bobby

Have another answer? Share your knowledge.