HTTP Server: NGINX

Reverse proxy for Apache

by ross at 08:48:16 on August 2, 2014

Prepare Apache

Edit apache24/httpd.conf and change Listen directive:

Listen 127.0.0.1:8080

Edit all the virtual hosts you have and change port 80 to port 8080, like this:

<VirtualHost *:8080>

Because Apache will only see connections from the proxy not the remote user all the logs will only contain 127.0.0.1 as the source address. mod_rpaf2 fixes that.

# cd /usr/ports/www/mod_rpaf2
# make install clean

Your httpd.conf should contain this:

LoadModule rpaf_module        libexec/apache24/mod_rpaf2.so

Create apache24/modules.d/200_mod_rpaf.conf:

<IfModule rpaf_module>
    RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips 127.0.0.1
    RPAFheader X-Forwarded-For
</IfModule>

Create NGINX proxy server

Create nginx/sites/example.com.conf:

server {
    listen 80;
    server_name example.com www.example.com;

    access_log   /var/log/nginx-example.com-access.log  main;
    error_log    /var/log/nginx-example.com-error.log;

    location / {
        proxy_pass http://127.0.0.1:8080;
        include /usr/local/etc/nginx/proxy_params;
    }

    ## If you have this in Apache VirtualHost:
    ##     Alias /mysql "/usr/local/www/phpMyAdmin/"
    ## Then you should use NGINX alias for /mysql static files location:
    #location ~* ^/mysql/(.+\.(ico|pdf|flv|jpg|jpeg|png|gif|svg|eot|ttf|woff|css|js))$ {
    #    alias /usr/local/www/phpMyAdmin/$1;
    #}

    location ~* ^.+\.(ico|pdf|flv|jpg|jpeg|png|gif|svg|eot|ttf|woff|css|js)$ {
        # The same as Apache's DocumentRoot:
        root /home/example.com/public;
    }
}

In this setup NGINX will serve static files with known extensions on its own. The rest will be proxied to Apache running on 127.0.0.1:8080.

Run

# service apache24 restart
# service nginx restart

 

Comments
Thanks for this very helpful steps. I am just starting learning freebsd and building my own web server.

In this step, is the Apache assumed to be already installed and configured like your other tutorial?
-- mcac
Tuesday, April 21, 2015, 21:06:22
Yes, apache is assumed to be up and running. This page shows how to modify an existing standalone Apache installation to be a backend for Nginx.
-- ross
Wednesday, April 22, 2015, 1:44:11
Thanks Ross.

Please bare with me. I'm pretty new to this side of the world.

If I understood this correctly, NGINX is the reverse proxy server, acting as the mediator between the outside world and the internal domain. Apache is the actual back end web sever that serves the data back to NGINX and from NGINX back to the outside world. If that's the case, why did you choose nginx over apache as the front end and can you have nginx as a back end too?
-- mcac
Friday, April 24, 2015, 1:08:40
I guess this step is not really necessary if you have standalone server like the previous step.

is this something possible, server1 handles proxy, routing, gateway & firewall and server2 is purely web server?

____________________________________ _______________
|proxy/router/gateway/firewall| > | Web Server|
-- mcac
Friday, April 24, 2015, 1:18:17
As you can see in the config above NGINX serves static files (like .png, .jpeg) on its own and only queries Apache for other files like PHP scripts.

One reason people do this is that NGINX is considered to be way faster on static files than Apache. This also saves CPU/IO resources of the backend so it could handle a bigger load.
-- ross
Friday, May 1, 2015, 11:29:16