Main  /  Edit version 15  /  Edit version 16  /   /  Users Area

Difference "How to run the application at the root of the web server" ver. 15 versus ver. 16

Content:

<!--toc-->
## Introduction
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.
It is simple and useful as all static files served
as is and the application's URL use its own paths:
For example:
Static files:
/media/style.css
Application:
/mb
/mb/tree/1
/mb/comment/10
Where "/tree/1" and "/comment/10" are application's
sub path.
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.
So to do this you need to use URL Rewriting rules
that exist in any modern web server.
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.
So we would show how to create correct rewrite rules
for the web server to serve the application in the
host root.
We would assume that:
- 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
### Apache
We should enable `mod_rewrite` and put in
our `<VirtualHost *>` location following rewrite rules
RewriteEngine On
RewriteRule ^(/media/.*)$ $1 [PT]
RewriteRule ^(/favicon\.ico)$ $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",
"^/favicon.ico$" => "$0",
"^/(.*)" => "/mb.fcgi/$1"
)
Please note that we use `rewrite-once` directive such
that once `"^(/media/.*)"` rule is executed it would
not be rewritten any more.
since version 1.4.23 it's possibly to directly use
fastcgi.server = (
"/" => ((
## your configuration ... and after
"fix-root-scriptname" => "enable",
"check-local" => "disable"
))
)
### Nginx
The rewrite rule that should be defined before
the FastCGI handler is following:
rewrite ^(/media/.*)$ $1 last;
rewrite ^(/favicon.ico)$ $1 last;
rewrite ^/(.*)$ /mb.fcgi/$1 ;
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.
### CppCMS-Embedded
The `http.rewrite` section defines the URL rewriting
rules. It is implemented in CppCMS sarting from CppCMS 0.99.11.
The `http` configuration section should look like:
"http" : {
"script" : "/mb.fcgi",
"rewrite" : [
{ "regex" : "/media(/.*)?", "pattern" : "$0" },
{ "regex" : "/favicon\\.ico", "pattern" : "$0" },
{ "regex" : ".*" , "pattern" : "/mb.fcgi$0" }
]
}
## Useful Sources
- [Apache mod\_rewrite](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)
- [Lighttpd 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/)

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