Apache CGI deployment not serving static files from public directory

56 views
Skip to first unread message

StarX

unread,
Oct 10, 2016, 6:50:46 AM10/10/16
to Mojolicious
Guys,

I've got a Mojolicious app running under CGI, but my css and js files are not being served from the pubic directory.  Here's my htaccess file:

 AddHandler cgi-script .pl
     Options +ExecCGI

     IndexIgnore *

     RewriteEngine on

     RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
     RewriteRule ^(.*) public/$1 [L]

     RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
     RewriteRule ^(.*) script/my_app.pl  [L] 

 RewriteEngine on
     # Rewrite only if the request isn't for a real file, directory, or symlink.
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-l
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ script/my_app.pl/$1 [L]

In MyApp.pm I have:

$self->hook( before_dispatch => sub {
       my $self = shift;
       $self->req->url->base(Mojo::URL->new(q{http://my-servername/dir/my_app/}));
  });

My URLs appear correctly in the page source as /dir/my_app/css/file.css and
/dir/my_app/js/file.js but clicking them in the source just displays the entire page content rather than the css/js.  I'm not quite sure how to configure the rewrite correctly, can anyone help?

Thanks

MJ.

Charlie Brady

unread,
Oct 11, 2016, 10:13:15 AM10/11/16
to StarX, Mojolicious

On Mon, 10 Oct 2016, StarX wrote:

> I've got a Mojolicious app running under CGI, but my css and js files are
> not being served from the pubic directory.
...
> My URLs appear correctly in the page source as /dir/my_app/css/file.css and
> /dir/my_app/js/file.js but clicking them in the source just displays the
> entire page content rather than the css/js. I'm not quite sure how to
> configure the rewrite correctly, can anyone help?

I think you should configure apache so that those files are served
directly by apache, rather than attempt to have your application executed.

StarX

unread,
Oct 12, 2016, 12:38:15 PM10/12/16
to Mojolicious, mje...@me.com, charli...@budge.apana.org.au
Thanks for the reply Charlie. 

Can you give me any pointers as to how I would set this up, I don't have a lot of experience with Apache configuration - it's managed by our systems admins so I'm currently stuck with CGI at the moment.

I managed to get it working by using:
$ENV{SCRIPT_NAME} = "/dir/my_app/" in myApp.pm

Then in htaccess I have:

AddHandler cgi-script .pl
Options +ExecCGI

IndexIgnore *

RewriteEngine on
    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ script/my_app.pl/$1 [L]

The code in my original question was based on the documentation in the Apache Deployment info here: https://github.com/kraih/mojo/wiki/Apache-deployment.  I really like Mojolicious but I find that some of the documentation can be confusing for someone that is relatively new to the framework.  I guess that this is what this group is for though.  Thanks again.

Charlie Brady

unread,
Oct 13, 2016, 2:38:31 PM10/13/16
to mje...@me.com, Mojolicious

On Wed, 12 Oct 2016, StarX wrote:

> Thanks for the reply Charlie.
>
> Can you give me any pointers as to how I would set this up,

I see under https://github.com/kraih/mojo/wiki/Apache-deployment#apachecgi
some text:

"In this way you make Apache render static files from /public folder for
you".

... which is what I am suggesting you should be doing.

> I don't have a lot of experience with Apache configuration

I am not sufficiently expert with apache configuration to tell you how to
do it, but there do seem to be specific .htaccess instructions in the
documentation which don't match what you are trying to use.
Reply all
Reply to author
Forward
0 new messages