nginx: proxy all traffic to apache

Sometimes you need to add nginx frontend beside apache webserver. In my case I have tons of websites where visitors are very rare. Further I’m too lazy to write configuration files for each virtual host in nginx. On other side I want to proxy some traffic to another webservers such as nodejs applications or just serve static content.

\"nginx-apache\"

In this case I assume that your apache2 listening for connections on 127.0.0.1:8080, ie in config file of apache:

/etc/apache2$ cat ports.conf
Listen 8080

Make sure all your VitrualHost binded to port 8080


The idea is configure nginx policy: to proxy all requests to apache except of specific websites. It can be done by creating virtual host which will catch all requests for non-existing domain name such as \"_\"

/etc/nginx/sites-enabled# cat apache 
upstream apachephp {
  server 127.0.0.1:8080;
}

server {
  listen       0.0.0.0:80 default_server;
  server_name  _;

  #access_log  /var/log/nginx/log/apache-proxy.access.log;
  #error_log  /var/log/nginx/log/apache-proxy.error.log;



  ## send request back to apache1 ##
  location / {
    proxy_pass  http://apachephp;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_redirect off;
    proxy_buffering 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;
  }
}

Don\'t forget to install apache module rpaf for ip address tracking


$ sudo apt-get install libapache2-mod-rpaf