directadmin + nginx and how to make them friends

    As you know, apache is a good thing, but its heaviness, greatly limits the possibilities.
    To solve the problem, a bunch of frontend + backend is used. Apache acts as the backend server, and any other is lightweight as the frontend server. In this article, we will consider a bunch in which nginx acts as a front-end server.
    The Internet is full of articles on how to configure nginx, but I have not come across any how to make nginx work together with the directadmin control panel.


    How it was.


    The task was, but the solution did not immediately come to mind. A little googling, several solution options appeared:
    1) to wrap listening on port 80 to some other, and there already listen to nginx. I did not like this solution much, due to the fact that it imposes certain gameplay.
    2) in any way make the Apache listen to another port (not 80), and there everything is already simple.

    If in a normal situation, when we do not have a hosting control panel, everything is simple - we go and correct the handles of the appache config, then with the control panel, it will not work. Each time after small changes to the site’s settings through the direct admin, the entire section of the appache config related to the changed site is overwritten.

    Enlightenment


    Having climbed a little in the directory in which I have directadmin installed, and this: / usr / local / directadmin, it turned out that there are templates for creating virtual hosts. Voila ... happiness knew no bounds. It turns out everything is easier than it could be!

    Implementation


    You need to copy the files from the $ DIRECTADMIN_HOME / data / templates / directory related to the virtual hosts to the $ DIRECTADMIN_HOME / data / templates / custom directory.
    This is done to ensure that when updating directmin versions, the templates you changed were not overwritten with standard ones.

    $ DIRECTADMIN_HOME #cd / data / the templates /
    #cp virtual_host.conf custom / virtual_host.conf
    #cp virtual_host2.conf custom / virtual_host2.conf
    #cp virtual_host_sub.conf custom / virtual_host_sub.conf
    #cp virtual_host2_sub.conf custom / virtual_host2_sub.conf

    Now in the files we copied, you need to fix the port. In each file, a line:

    replaced by:
    8181>
    Now we have apache listening on virtual hosts on port 8181. And so that traffic was delivered to it, we will take care. More precisely, nginx will take care of this.

    Nginx installation


    This is done elementarily.
    You can install it from the repositories (but I would not recommend it):
    # apt-get install nginx
    Why wouldn’t I recommend installing from the repositories? Because the release of new stable versions occurs very often, and it is better to download the new version from the developer's site.
    At the time of writing, the latest stable version was 0.6.35, and the latest current version 0.7.42, and version 0.4.13 is available for comparison in the repositories.

    So, let's start the installation. I describe the process in short, or rather even say only in commands, because You can find a more detailed description at the following link .

    #mkdir / root / nginxsrcs && cd / root / nginxsrcs
    #wget http://sysoev.ru/nginx/nginx-0.7.42.tar.gz
    #tar zxf nginx-0.7.42.tar.gz
    #cd nginx-0.7.42
    #. / configure
    #make
    #make install
    #cd / usr / local / nginx / conf
    #vim nginx.conf

    We need to configure nginx as a cache proxy server.
    You ask, why not give away the statics (pictures, JS files, html) without apache? It's simple - nginx does not know how to work with ".htaccess" (to be honest, I'm only glad about that). But you can configure as you prefer.

    Next, the config, marked with key places: Save the config and try to run nginx. If you already have apache running, then most likely nginx will not start, since port 80 is occupied by apache. You need to fix the file /etc/httpd/conf/httpd.conf.

    worker_processes 3;
    pid logs/nginx.pid;
    events {
    worker_connections 1024;
    }

    http {
    access_log off;
    error_log off;
    include mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent"';
    server {
    access_log off;
    error_log off;
    listen 1.2.3.4:80; # IP:port которые будут слушаться
    server_name *.*; #нам нет никакого различия на какой вирт-хост пришел запрос. Всё обрабатывает apache

    charset windows-1251;

    location /
    {
    proxy_pass http://1.2.3.4:8181/; # адрес который слушает apache.
    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;

    client_max_body_size 10m;
    client_body_buffer_size 8k; #128k

    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;

    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    }
    }






    Replace the string “Listen 80” with “Listen 8181”.
    And "NameVirtualHost 1.2.3.4:80" on "NameVirtualHost 1.2.3.4:8181".
    Restart apache. We start nginx.
    Voila! Everything works. We can turn to any virtual host.

    Real IP


    We can notice that the client IP will be 1.2.3.4.
    This happens due to the fact that the real IP of the client is received by nginx, and when the traffic is forwarded by apache, it already goes from our internal IP.
    But it's not a problem. There is a great mod_rpaf module for apache.
    It is installed elementarily.

    #cd / root / nginxsrcs
    #wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
    #tar xzf mod_rpaf-0.6.tar.gz
    #cd mod_rpaf-0.6
    #apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

    Create the mod_rpaf.conf configuration file: Restart apache. That's it, now we have a fully working bunch of nginx + apache + directadmin.
    LoadModule rpaf_module /usr/lib/apache/mod_rpaf-2.0.so
    RPAFenable On
    RPAFsethostname Off
    RPAFproxy_ips 127.0.0.1 1.2.3.4
    RPAFheader X-Real-IP





    Also popular now: