skip to Main Content

I’m trying to deploy my Rails app to a staging environment for the first time. I’m pretty sure this is an Apache/Passenger issue, but I’m not sure where to even look to fix it. I probably just need to add a rule to my conf file, but I don’t know what that rule would be. Maybe some sort of rewrite rule, since it appears to be looking for a file instead of parsing the route?

The issue is: for EVERY SINGLE route, it appears to be converting it “behind the scenes” into “/index.html” – whether I try “/api/v1” or “/api/v1/users” or “/api/v1/channels/authorize” (or any others).

The apache2/access.log file seems to show the route being passed properly:

[07/Dec/2019:18:23:15 +0000] "GET /api/v1/channels/authorize HTTP/1.1" 500 41024 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"

And the apache2/error.log file doesn’t show any errors.


Here is my conf file. I’m using an alias to parse between two separate apps (/api/* goes to the Rails backend, everything else goes to the VueJS front-end — I know I could embed the client into the Rails public directory, but I’m doing it this way for my own reasons).

<VirtualHost *:443>
    ServerName <my url>
    DocumentRoot /path/to/client

     # configure error logs
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

     # Passenger config stuff
    PassengerEnabled off
    PassengerRuby /path/to/ruby
    PassengerAppEnv development
    PassengerStartTimeout 400


     # if the route matches https://<domain>/api/*
      # then point to the Rails API app
      # (and use Passenger to serve it)
    Alias /api /path/to/railsapp/public
     # configuration for the /api routes
      # basically just enable Passenger and tell it where to point to
    <Location /api>
        PassengerEnabled on
        PassengerBaseURI /
        PassengerAppRoot /path/to/railsapp
    </Location>
    <Directory /path/to/railsapp/public>
        Allow from all
        Options -MultiViews
        Require all granted

#       RailsEnv test
    </Directory>

     # for EVERYTHING ELSE, point to the VueJS client app
    <Location />
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule . /index.html [L]
    </Location>

     # and of course enable SSL and point to the certs
    SSLEngine on
    SSLCertificateKeyFile /path/to/key
    SSLCertificateFile /path/to/cert
    SSLCertificateChainFile /path/to/chain-file

</VirtualHost>

This is the first time I’ve done anything with Passenger. I tried to just cobble together something from the examples I found online, but it’s very possible I’ve missed something along the way.

2

Answers


  1. Chosen as BEST ANSWER

    So I've figured it out. The RewriteRules in the following block are getting applied to everything:

    <Location / >
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule . /index.html [L]
    </Location>
    

    (every route in Vue gets rewritten back to the index.html file -- but Rails doesn't do that).

    So I tried a few things that different work before eventually figuring out that I can simply add a RewriteCond to the rule set to exclude all calls to api/*

    So the new block is:

    <Location / >
        RewriteEngine on
        RewriteCond %{REQUEST_URI} !/api/*        ## THIS IS THE LINE TO ADD!
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule . /index.html [L]
    </Location>
    

    That one line is all I had to add to the apache conf file to get it working perfectly.


  2. Try changing PassengerBaseURI / under <Location /api> to PassengerBaseURI /api.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search