XE with Nginx
Introduction
Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Nginx now hosts nearly 12.18% (22.2M) of active sites across all domains. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
Basically Nginx is an alternative to the classic Apache Web Server (httpd). And it’s a very good one!
Xpress Engine works with Nginx just as with any other web server out there which has support for PHP. The only thing that is missing is the set of the rewrite rules (not always used, but in most of the cases). These will help any XE user to have SEF (Search Engine Friendly) URLs.
As you may know, the default distribution of XE comes with a file called .htaccess that enables rewrite rules for Apache Web Server.
So if you are using Apache as Web Server (which is probably the most common case) you won’t have to do anything in order to benefit of the XE rewrite rules. However, no corresponding file is provided by default (in the default distribution of XE) for Nginx.
In this page you can find the necessary XE rewrite rules for Nginx. Applying these rules into your Nginx virtual host will make XE perform exactly the same it does in Apache Web Server.
Requirements
The main advantages of Nginx are: very fast and low resource consumption. The main disadvantage is that is not that easy (as in the case of Apache) to make the configuration of your Nginx web server.
There are many differences between Apache and Nginx. You should carefully read about all these before trying to use XE with Nginx (for this you can refer to Nginx official site documentation).
One major difference is that Nginx does not run PHP embedded in its current process (like in the case of Apache). You will have to configure PHP to run as a Fast CGI and then to configure Nginx to use that Fast CGI.
You should also take care about the spawning of PHP process necessary to serve the requests via Fast CGI. Or you could use another application that will do that for you (this case is recommended). Such an application is FPM (FastCGI Process Manager).
Basically the requirements for setting up XE to use Nginx as a web server are:
- Nginx
- PHP (configured to run as a FastCGI)
- FPM
- The set of XE rewriting rules for Nginx (presented below)
This tutorial will not focus on describing all the necessary steps for installing the above apps on each platform. For this there are dedicated tutorials out there (example for installing on CentOS using yum: http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/).
XE rewrite rules
rewrite ^/(layouts|m.layouts)/(.+)/(.+).html$ /./index.php last;
rewrite ^/(modules|addons|widgets)/(.+)/(conf|queries|schemas)/(.+).xml$ /./index.php last;
location ~ /(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*)
{
try_files $uri /./files/$2/$3;
}
location ~ /(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*)
{
try_files $uri /./$2/$3;
}
rewrite ^/(rss|atom)$ /./index.php?module=rss&act=$1 last;
rewrite ^/([a-zA-Z0-9_]+)/(rss|atom|api)$ /./index.php?mid=$1&act=$2 last;
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$ /./index.php?vid=$1&mid=$2&act=$3 last;
rewrite ^/([0-9]+)/(.+)/trackback$ /./index.php?document_srl=$1&key=$2&act=trackback last;
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ /./index.php?vid=$1&document_srl=$2&key=$3&act=trackback last;
rewrite ^/admin/?$ /./index.php?module=admin last;
rewrite ^/([0-9]+)$ /./index.php?document_srl=$1 last;
location ~ /([a-zA-Z0-9_]+)/?$
{
try_files $uri/ /./index.php?mid=$1$is_args$args;
}
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)$ /./index.php?mid=$1&document_srl=$2 last;
location ~ /([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$
{
try_files $uri/ /./index.php?vid=$1&mid=$2$is_args$args;
}
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$ /./index.php?vid=$1&mid=$2&document_srl=$3 last;
rewrite ^/([a-zA-Z0-9_]+)/entry/(.+)$ /./index.php?mid=$1&entry=$2 last;
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$ /./index.php?vid=$1&mid=$2&entry=$3 last; The lines above should be placed inside your Nginx virtual host config file - server section.
Sample Nginx configuration file
server {
server_name YOUR_DOMAIN;
root /usr/share/nginx/html/xe/;
access_log /var/log/nginx/xe/YOUR_DOMAIN-access.log;
error_log /var/log/nginx/xe/YOUR_DOMAIN-error.log;
rewrite ^/(layouts|m.layouts)/(.+)/(.+).html$ /./index.php last;
rewrite ^/(modules|addons|widgets)/(.+)/(conf|queries|schemas)/(.+).xml$ /./index.php last;
location ~ /(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*)
{
try_files $uri /./files/$2/$3;
}
location ~ /(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*)
{
try_files $uri /./$2/$3;
}
rewrite ^/(rss|atom)$ /./index.php?module=rss&act=$1 last;
rewrite ^/([a-zA-Z0-9_]+)/(rss|atom|api)$ /./index.php?mid=$1&act=$2 last;
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$ /./index.php?vid=$1&mid=$2&act=$3 last;
rewrite ^/([0-9]+)/(.+)/trackback$ /./index.php?document_srl=$1&key=$2&act=trackback last;
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ /./index.php?vid=$1&document_srl=$2&key=$3&act=trackback last;
rewrite ^/admin/?$ /./index.php?module=admin last;
rewrite ^/([0-9]+)$ /./index.php?document_srl=$1 last;
location ~ /([a-zA-Z0-9_]+)/?$
{
try_files $uri/ /./index.php?mid=$1$is_args$args;
}
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)$ /./index.php?mid=$1&document_srl=$2 last;
location ~ /([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$
{
try_files $uri/ /./index.php?vid=$1&mid=$2$is_args$args;
}
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$ /./index.php?vid=$1&mid=$2&document_srl=$3 last;
rewrite ^/([a-zA-Z0-9_]+)/entry/(.+)$ /./index.php?mid=$1&entry=$2 last;
rewrite ^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$ /./index.php?vid=$1&mid=$2&entry=$3 last;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}Don't forget to replace YOUR_DOMAIN in the sample above with your corresponding domain. This file should be saved in nginx conf folder (for example on CentOS: /etc/nginx/conf.d/YOUR_DOMAIN.conf)
Conclusion
Hope this will work just fine for you. If you have any questions related to Nginx and XE please don’t hesitate to post your questions on the forum. We will be more than glad to help you.
There is already a section started by delphixe2 user (who provided the first set of rewrite rules – many thanks!) for this topic. You can use this section or create another one as well.
