Main  /  Edit version 9  /  Edit version 10  /   /  Users Area

Difference "How to run the application at the root of the web server" ver. 9 versus ver. 10

Content:

<!--toc-->
Most of the time, one would want to run the cppcms application so that accessing the root of the web server (the root of your domain name) would display the root content of your application.
However, the simplest set up requires the required path to be prefixed by the application name, which is not usually pretty. The fix requires to set up the web server with some URL rewriting mechanism. The exact set up depends on the web server used.
## NOTE!
## Introduction
Remember that the content of the wiki is contributed by CPPCMS community members. If you find inaccuracies in the instructions provided below, please register/login and edit the page for the benefit of future users.
In most of the examples in this wiki the applications
run from some basic URL - defined by the script name,
for example "Hello World" application runs
at "/hello" URL message board application runs
from "/mb" URL.
## Apache2
It is simple and useful as all static files served
as is and the application's URL use its own paths:
Use mod_rewrite.
For example:
.htaccess:
Static files:
<IfModule mod_rewrite.c>
RewriteEngine on
/media/style.css
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ my_app.cgi/$1 [L,QSA]
</IfModule>
Application:
/mb
/mb/tree/1
/mb/comment/10
Where "/tree/1" and "/comment/10" are application's
sub path.
Of course, Apache2 must be properly configured to allow .htaccess override of mod_rewrite.
However we frequently may want to run the application
in the "root" of the web service such that the main
URL would be "/"
The naive approach - redirecting all requests to the
application would not work as static files should
be served from other location starting from "/" and
it is not application's job to serve static files.
Or use a script alias:
So to do this you need to use URL Rewriting rules
that exist in any modern web server.
# Content of /etc/apache2/sites-enabled/tatoebacpp
So if the application should start at the root "/"
and all the media files should be served from "/media/"
directory we would want to live "/media/*" URL untouched
and rewrite all other URLs like "/tree/1" to "/mb.fcgi/tree/1" where "/mb.fcgi" is out script.
FastCGIConfig -maxProcesses 1 -processSlack 1
<VirtualHost *:80>
ServerName tato.sysko.fr
ServerAlias *.tato.sysko.fr
# Usually it is given in fastcgi.conf by default
FastCgiServer /blablabla/tatoebacpp_git/build/tatoeba -initial-env CPPCMS_CONFIG=/blablabla/tatoebacpp_git/config.js -socket /tmp/hello-fcgi-socket
ScriptAliasMatch ^(.*)$ /blablabla/tatoebacpp_git/build/tatoeba$1
AddHandler fastcgi-script /blablabla/tatoebacpp_git/build/tatoeba
</VirtualHost>
So we would show how to create correct rewrite rules
for the web server to serve the application in the
host root.
## lighttpd
We would assume that:
The rewrite configuration looks like:
- All static files are placed in `/media/` directory
relatively the the web root of the application.
- Our FastCGI handler is mapped to "script" `/mb.fcgi`.
See [How to configure the web server to run CppCMS applications](/wikipp/en/page/cppcms_1x_tut_web_server_config) tutorial.
## Rewrite rules for different web servers
### Apache2
We should enable `mod_rewrite` and put in
our `<VirtualHost *>` location following rewrite rules
RewriteEngine On
RewriteRule ^(/media/.*)$ $1 [PT]
RewriteRule ^/(.*)$ /mb.fcgi/$1 [QSA,L]
All the "/media/" files would be served as-is as files `[PT]` flag and the rest of the URL's like `/some/url` would be mapped into /mb.fcgi/some/url`
### lighttpd
We should enable `mod_rewrite` and provide following rule:
url.rewrite-once = (
"^(/media/.*)" => "$1",
"^/(.*)" => "/mb.fcgi/$1"
)
fastcgi.server = ( "/mb.fcgi" => ( "localhost" => (
"check-local" => "disable",
"host" => "127.0.0.1",
"port" => 8081
)))
Please note that we use `rewrite-once` directive such
that once `"^(/media/.*)"` rule is executed it would
not be rewritten any more.
## nginx
### nginx
The rewrite rule is:
The rewrite rule that should be defined before
the FastCGI handler is following:
rewrite ^(/media/.*)$ $1 last;
rewrite ^/(.*)$ /mb.fcgi/$1 ;
## Sources
Note: the `last` parameter is used for `/media/.*` root
to prevent following substitution. This is not done
fo the root URL `^/(.*)$` as FastCGI uses rewriting
on its own.
- <http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite>
- <http://www.djangobook.com/en/beta/chapter21/#cn124>
## Useful Sources
- [Apache mod\_rewrite](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)
- [Lighttp mod\_rewrite](http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite)
- [Nginx rewrite module](http://wiki.nginx.org/HttpRewriteModule)
- [The Django Book - Deploying Django](http://www.djangobook.com/en/1.0/chapter20/)

Sidebar:

## Related
- [Web Server Configuration](/wikipp/en/page/cppcms_1x_tut_web_server_config)

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

Поддержать проект

CppCMS needs You


Navigation

Main Page


Valid CSS | Valid XHTML 1.0