Debian – Configure nginx + apache bundle

If you are the administrator of the web server, then sooner or later the question of costs optimization of system resources consumed by apache. Specificallywe have a desire to give the files without processing them using apache. The solution is to install bundlesapache + nginx, nginx is where as the front-end forapache.


Both will work in conjunction with apache, nginx


Apache will listen to only one ip ( port (for example 81)listen to the internal ip of tohave no desire to connect directly to apache, apachewill listen on port 81 for the reason that the 80 portwill now listen to nginx
If there is a request for a file with a resolution: (bmpjpg jpeg | | | pjpeg | ico | gif | png | css | doc | txt | js |docx | .RTF | ppt | pdf | swf)you can change or add to the list, then a request to apache and nginx willnot immediately be given to the file that will saverequest processing time and system resources.
All other requests will be for redirecting the apache ( 81), but first set the package libapache2-modrpaf, which will transmit to the apache is notthe $ _SERVER [REMOTE_ADDR] = andreal client ip created the request.


Where to get a Debian repository for nginx nginx
The good guys (System administrators) are an Assembly of packages nxing and put all his effortsinto the repository:
  1. deb squeeze main
  2. deb-src squeeze main
The key for the repository setup nginx

  1. gpg –keyserver –recv-keys 0A3D4789
  2. gpg –armor –export 0A3D4789 | apt-key add –
After updating the list of packages in the repositoriesyou can install nginxplus, which unlike the usualpackage nginx going with additional modulesngx_loadavg_variables and ngx_addon’s_if_statment.patch. patch.
So get down to installing nginx + apache bundlesettings
The first thing I hope experience in configuringapache server is.
You will need to make apache listen to 81:

  1. nano /etc/apache2/ports.conf



  1. NameVirtualHost
  2. Listen 80

change on


  1. NameVirtualHost
  2. Listen 81

So if you already have apache virtualhost, you mustlike images get to listen on 81, replacing:


  1. <VirtualHost *:80>



  1. <VirtualHost>

Install libapache2-modrpaf apache to get the realclient ip, not


  1. aptitude install libapache2-mod-rpaf

You can now restart apache:


  1. /etc/init.d/apache2 restart

Install nginx-plus


  1. aptitude install nginx-plus


Move on to configuring nginx hosts, which are stored in the directory:/etc/nginx/site-enabled/


Create a description for the host, pravivalready existing for the configs in apache and nginx.



  1. server {
  2. listen 80;
  3. server_name;
  4. access_log /var/log/nginx/localhost.access.log;
  5. location ~* ^.+\.(bmp|jpg|jpeg|pjpeg|gif|ico|png|css|doc|txt|js|docx|rtf|ppt|pdf|swf|zip|rar|gz)$ {
  6. expires 10d;
  7. log_not_found on;
  8. try_files $uri
  9. /home/user/domains/$uri;
  10. }
  11. error_page 500 502 503 504 /50x.html;
  12. location = /50x.html {
  13. root /var/www/nginx-default;
  14. }
  15. location / {
  16. proxy_pass; # делаем переадресацию запросов на apache
  17. proxy_redirect off;
  18. proxy_set_header Host $host;
  19. # Эти настройки необходимы, что бы из скриптов было видно реальные IP пользователя, а не фронт-части
  20. proxy_set_header X-Real-IP $remote_addr;
  21. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  22. client_max_body_size 10m;
  23. client_body_buffer_size 128k;
  24. proxy_connect_timeout 90;
  25. proxy_send_timeout 90;
  26. proxy_read_timeout 90;
  27. proxy_buffer_size 4k;
  28. proxy_buffers 4 32k;
  29. proxy_busy_buffers_size 64k;
  30. proxy_temp_file_write_size 64k;
  31. }
  32. }


I think it is clear that our site is in the directory:/home/user/domains/


Now edit the apache virtualhostconfiguration/etc/apache2/sites-enabled/000-default



  1. <VirtualHost>
  2. DocumentRoot /home/user/domains/
  3. ServerAlias localhost
  4. ServerName www.localhost
  5. ScriptAlias /cgi-bin/ /home/user/domains/
  6. <Directory “/home/user/domains/”>
  7. AllowOverride All
  8. Order Deny,Allow
  9. Allow from all
  10. Options All
  11. </Directory>
  12. <Directory “/home/user/domains/”>
  13. AllowOverride None
  14. Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  15. Order allow,deny
  16. Allow from all
  17. </Directory>
  18. <IfModule dir_module>
  19. DirectoryIndex index.php index.html index.cgi
  20. </IfModule>
  21. #SuexecUserGroup valualit valualit
  22. ErrorLog “/home/user/domains/”
  23. CustomLog “/home/user/domains/” combined
  24. LogLevel warn
  25. </VirtualHost>

As you have noticed there is no big deal, just restart apache and nginx:


  1. /etc/init.d/apache2 restart
  2. /etc/init.d/nginx-plus restart

If you are not forgotten, the virtualhost change thesettings through the Control Panel, complexity ariseswhen you add or edit a virtualhost in bundlesbecause the default settings are designed to workonly with apache, but this topic I will discuss later in the example directadmin + nginx + apache.