Nginx

nginx (engine x) is a small robust HTTP and reverse proxy server, as well as a mail proxy server written by Igor Sysoev. It has been running for more than five years on many heavily loaded Russian sites including Rambler.

According to Netcraft nginx served or proxied 4.24% busiest sites in January 2010. Here are some of success stories: http://FastMail.FM, http://Wordpress.com.

Read the Nginx Docs, but perfectly Nginx Russian Docs and Examples give more info.

Download

Nginx is available at http://nginx.org/en/download.html

Use Nginx as Proxy

We all know our beloved and revered Web server Apache. Unfortunately the time it made so much “featured”, which sometimes struck by how much memory and resources in general, he can eat on a fairly simple tasks. And sometimes the server simply can not cope with the load (basically not enough memory).

It is necessary to note the fact - Apache completely wasteful spending in the memory of the impact of static content (images, HTML files, styles, etc. - any content that does not contain a server-side code).

Fortunately there is something to compare. It has long appeared an easy web server Nginx. It was written originally optimized for static and impact, in comparison with Apache, totally consumes memory. And wherever they can temporarily throw off a chunk of memory to disk, it does not leave extra system resources.

But Nginx does not contain any modules for the processing of dynamic content (except SSI). He can only “proxy” (pass) processing of content “backend” - gathered on the background dynamics processor Apache (or any other Web server). Just Nginx is able to communicate with Fast-CGI server on the backend.

This is where the separation begins at FrontEnd and BackEnd impact. The meaning is very simple - we give each do only what he's really good to do and nothing more. Nginx is on the front and accepts requests from clients. Inquiries as to the dynamic (or potentially dynamic content), Nginx proxy in backend, which presumably is Apache, and handles requests for dynamic content (eg, PHP-scripts). The result is given back to Nginx, which in turn returns the response to customers.

Thus, it is said, can “facilitate the Apache”, ie existing Apache put the backend, and before him on the front put Nginx. It is usually transferred to listen to what Apache is a port on 127.0.0.1 and there proxy send inquiries Nginx.

Unfortunately DirectAdmin checks for Apache on port 80 of the main IP, and change is impossible. According to this the only option, which will run this ligament is to install Nginx on other than port 80 (eg, 85) and forwarding all requests to port 80 (HTTP) on port 85. This can be done with FireWall.

Firewall

Linux
FreeBSD

Use Nginx as Proxy and Web for static content

In addition to proxying requests Nginx can act as a Web server, giving a static content. This reduces the load on the server at 15-40%, depending on how you have lots of static content on the sites.

To work properly need to make some changes in the work of DirectAdmin, which is recommended to do only experienced professionals.

Since the creation of Nginx configuration files necessary for each host, you must create a number of scripts, intercepting the following:

All of these scripts must be placed in the directory /usr/local/directadmin/scripts/custom and have the right to perform system user directadmin.

As seen from the content they interact with two scripts:

In turn, /usr/local/directadmin/scripts/nginx_task.sh must complete cron every 2 minutes what can be done by adding a line to crontab: * / 2 * * * * root /usr/local/directadmin/scripts/nginx_task.sh

During the execution of the script /usr/local/directadmin/scripts/nginx_task.sh reads all the tasks and executes them with main script /usr/local/directadmin/scripts/nginx_direct.sh

Such complexity is due to the fact that such tasks can be time consuming and lead to time-out Web Interface.

Main script

As seen from the contents, there are variables in the script of which we are interested in three:

  1. NGINXHOST - path connections hostnames Nginx
  2. NGINXTEMPATE - template domain Nginx
  3. NGINXSUBTEMPATE - template subdomain Nginx

Initial Nginx config

:!: This configuration file may not be sufficiently optimal for your case.

user apache apache;                                          
worker_processes  4;                                         
worker_rlimit_nofile 150000;                                 

error_log  /var/log/nginx/error_log;

pid        /var/run/nginx/nginx.pid;

events {
    worker_connections 25000;
}                             

http {
    include       /usr/local/nginx/etc/mime.types;
    default_type  application/octet-stream;       

    log_format  main  '$remote_addr - $remote_user [$time_local] $status '
                      '"$request" $body_bytes_sent "$http_referer" '      
                      '"$http_user_agent" "$http_x_forwarded_for"';       
 
    access_log  /var/log/nginx/access.log  main;

    limit_zone   one  $binary_remote_addr  10m;
    limit_conn   one  7;                       
    limit_rate         512K;                   
    limit_zone cglob $binary_remote_addr 16m;

    client_header_timeout       60;
    client_body_timeout         60;
    send_timeout                120;
    proxy_read_timeout          60; 
    proxy_connect_timeout       60; 
    proxy_send_timeout          180;

    msie_padding                on;

    proxy_buffer_size           32K;
    proxy_buffers               64 512K;
    proxy_busy_buffers_size     1024K;  
    proxy_temp_file_write_size  1024K;  

    gzip on;
    gzip_min_length  10240;
    gzip_buffers     32 64k;
    gzip_types       application/x-javascript text/css text/xml text/plain;

    client_header_buffer_size   4K;
    client_max_body_size        100000000;
    large_client_header_buffers 16 8K;    

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;

    output_buffers   32 256k;
    postpone_output  1460;   

    lingering_time     30;
    lingering_timeout  6; 
    reset_timedout_connection  on;

    keepalive_timeout  20;
    server_names_hash_bucket_size 10240;

server {
        listen       **IP**:85 default rcvbuf=8192 sndbuf=16384;
        server_name  localhost;
        access_log  /var/log/httpd/access_log  main;
        error_log   /var/log/nginx/error_log info;

        location / {
                    proxy_pass         http://**IP**/;
                    proxy_redirect     off;

                    proxy_set_header   Host             $host;
                    proxy_set_header   X-Real-IP        $remote_addr;
                    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                }
        # Main Static files location
        location ~* ^/(phpmyadmin|webmail|squirrelmail|uebimiau|roundcube)/.+\.(jpg|jpeg|gif|png|ico|css|zip|tar|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|wmv|avi|cur|swf|mp3|wma|htc|cur)$ {
                        root /var/www/html/;
                        expires      30d;
                        access_log        off;
         }
         # MRTG
            location ~* ^/(stats)/.+\.(jpg|jpeg|gif|png|html|htm)$ {
            root /var/www/html/;
            access_log        off;
        }

        #Static files location
        location ~* ^.+\.(jpg|jpeg|gif|mp3|png|avi|vob|mpg|mpeg|mp4|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js)$ {
            root /var/www/html;
        }
    }
include /usr/local/nginx/etc/virtual.conf;
}

Changes in the Apache config

Please, enable mod_rpaf for Apache. Connect it like this:

LoadModule rpaf_module / usr/lib/apache/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname On
RPAFproxy_ips **IP**
RPAFheader X-Real-IP

:!: Warning This statement implies that Nginx is installed in /usr/local/nginx. If not, then change the path for the correct scripts.

 
nginx.txt · Last modified: 2010/02/09 09:36 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Please visit Automatic Backlinks to start earning free backlinks Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Prom Ball Gowns