From charlesreid1

This page describes how to set up nginx as a reverse proxy server for an apache server.

Why

The reason you would want to use nginx as a reverse proxy for apache is, it allows you to continue using nginx as the front-facing server for the site, handling all public requests. Requests that don't match the conditions of the proxy are simply treated as usual. When a request does meet the proxy conditions, it is routed to a different local port, where Apache is listening.

Apache is set up to listen only for requests from the local host, meaning our Apache instance will not be publicly visible. Only a local process forwarding traffic to the correct port will be able to connect to the Apache process.

Note that much of this follows the digital ocean tutorial here.

Install

nginx

I'll assume you've already got nginx installed and set up for your site. This should be as simple as sudo apt-get install nginx.

apache

I'll assume that you have apache 2 installed. If you don't, try a sudo apt-get install apache2.


Configuring

Configuring nginx

Now install your proxy "site":

$ vim /etc/nginx/sites-available/example.conf

Here, we can add nginx directives that specify the conditions for the reverse proxy. For example, if we wanted nginx to forward requests for PHP files to Apache, we would add this block:

server {
        
        .............blah blah blah..................

        location ~ \.php$ {
        
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080/;

         }

         location ~ /\.ht {
                deny all;
        }
}

This sends any requests for php files to localhost (127.0.0.1) port 8080. This is the port we'll configure Apache to listen to. The second part is to protect any .htaccess files.

Now enable the new site:

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf

Configuring Apache

Edit the file /etc/apache2/ports.conf and change:

Listen 80

to configure Apache to listen only locally on port 8080:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Now edit the site configuration file,

vim /etc/apache2/sites-available/example

add a line that looks like:


<VirtualHost 127.0.0.1:8080>

    <Directory "/path/to/wiki">
        Options none
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>