Search code examples
ruby-on-railsajaxapachepassengerhttpd.conf

Ajax call triggering base URL in Apache + Passenger integration mode


I am trying to configure multiple RAILS apps through httpd configuration file. Everything is working fine but the AJAX calls are triggering the wrong URL, for example if the application is configured as http://localhost/helloapp/ and it has AJAX call as get "/say_hello" it is trying to get "localhost/say_hello" instead of "localhost/helloapp/say_hello". Below is my httpd configuration file located at '/etc/httpd/conf/httpd.conf '. I am using centOS.

<VirtualHost *:80>
 ServerName localhost
    <Directory /var/www/html >
        Allow from all
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        #Require all granted
    </Directory>
   Alias /helloapp /var/www/html/hello_application/public
    <Location /helloapp>
        PassengerBaseURI /helloapp
        PassengerAppRoot /var/www/html/hello_application
    </Location>
    <Directory /var/www/html/hello_application/public>
      # MultiViews must be turned off.
        Allow from all
        Options -MultiViews
    </Directory>
</VirtualHost>


Solution

  • Any time you make an HTTP request to a url beginning with /, for example /say_hello, it is implied that you are sending a request to the path you specify from the root URL. The root URL is always your domain, which in your case is http://localhost/.

    So no matter how you configure your Apache server, when you make a request to /say_hello, your browser will direct it to http://localhost/say_hello. If you want to make a request to http://localhost/helloapp/say_hello, you will have to tell ajax to go to /helloapp/say_hello in your website code.

    If your rails app is named "helloapp", you could get this in one of the ways described here to access your application name in your rails code. Once you have it you could store it in an instance variable, like @app_name, for your template or javascript (or wherever you are making the ajax request from). I think what you are looking for is something along these lines:

    var url = "/<%= @app_name %>/say_hello";
    // do your ajax request with your new url variable