Nginx Reverse Proxy to Virtualmin Managed Server

Written by Peter Davies on .

Change virtualmin to operate from port 8080 for any new virtual hosts through: System Settings -> Server Templates -> Default Settings -> [Dropdown] Apache Website and edit the "port number" for standard port 80. Also change any existing vhosts by going to the "change ip" tab which allows you to stipulate the correct new port of 8080. You may also need to modify the apache module settings so that it knows not to listen on port 80, if left unmodified it would prevent Nginx from starting up.

As the normal HTTP requests are being proxied, it is advisable to get apache to use the "X-Forwarded-For" IP address for logging purposes. All you need to do is edit the /etc/apache2/apache2.conf to include:

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

Now install Nginx:

apt-get install nginx

Create the proxy config nano /etc/nginx/conf.d/proxy.conf adding the following:

server {

        listen 80;

        access_log off;
        error_log off;

        location /RequestDenied {
                return 418;
    }

        location / {
                proxy_pass      http://127.0.0.1:8080;
                proxy_redirect  off;

                #Proxy Settings
                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;

                proxy_max_temp_file_size 0;
                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;
        }
}

A quick restart of Nginx /etc/init.d/nginx restart and a check of the operational ports:

root@debian:~# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3953/nginx
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      2606/apache2

You should be good to go the main server: http://192.168.142.128 and see that Nginx is operational like so:

Its working!

References: