Comments on How to install Nginx as Reverse Proxy for Apache on Ubuntu 15.10

Nginx or - engine-x - is a high-performance web server with low memory usage, created by Igor Sysoev in 2002. Nginx is not just a web server, it can be used as a reverse proxy for many protocols like HTTP, HTTPS, POP3, SMTP, and IMAP and as a load balancer and HTTP cache as well. In this tutorial, I will install and configure Nginx as caching reverse proxy for an Apache web server on Ubuntu 15.10.

9 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: MjRad

Why would I want to do this?

By: till

Nginx delivers static and cached content much faster than apache. The above setup combines the speed from Nginx with the flexibility and .htaccess file compatibility from apache. So this setup is faster than a pure apache setup.

By: Matthew Lenz

You'd have to have a pretty good reason to disable .html caching.  Keep in mind that people just copy these tutorials outright. You are losing a big benefit by disabling static html cache.

By: Dattatray Gadade

Nice toturial, Thank you very much.

By: peri0603

I have Ubuntu 16.04 and ISPConfig 3.1 installed on my Server and want to do the same on my server. Can somebody help me...

By: elsonpulikkan

    ********** Set nginx as reverse proxy to apache server **********        sudo apt-get update    sudo apt-get install apache2 php libapache2-mod-php php-mcrypt        sudo php -v    sudo service apache2 restart            vim /etc/apache2/ports.conf ( *** Change the default port of apache2 80 to 8080 *** ) Listen 8080;    service apache2 reload        service apache2 restart        netstat -ntlp        mkdir /var/www/myapache.com        touch /var/www/myapache.com/index.php        vim /var/www/myapache.com/index.php ( **** add the apache2 site code here must be phpinfo(); page **** )    This is my apache server  <?php  phpinfo();  ?>    vim /etc/apache2/apache2.conf ( **** add the apache2 server virtualhost block here  ****)     namevirtualhost <your I.P address here>:8080  <VirtualHost <your I.P address here>:8080>    ServerName myapache.com    DocumentRoot /var/www/myapache.com    directoryindex index.php  </VirtualHost>     service apache2 reload    service apache2 restart        sudo apt-get install nginx        sudo mkdir -p /var/www/example.com/html    touch /var/www/example.com/html/index.html    vim /var/www/example.com/html/index.html ( **** add the nginx site html code here **** )    <html>    <head>        <title>Welcome to Example.com!</title>    </head>    <body>        <h1>Success!  The example.com server block is working!</h1>    </body></html>        cd /etc/nginx/sites-available/        rm -rf /etc/nginx/sites-available/default        touch example.com        sudo vim /etc/nginx/sites-available/example.com         ( *** add the nginx server virtualhost block here  *****)             server {    listen 80 default_server;    root /var/www/example.com/html;    index index.html index.htm;    server_name example.com www.example.com;    location / {        proxy_pass http://<your I.P address here>:8080;        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_set_header X-Forwarded-Proto $scheme;        try_files $uri $uri/ =404;    }}    cat /etc/nginx/sites-available/example.com        cd /etc/nginx/sites-enabled/        rm -rf /etc/nginx/sites-enabled/default        sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/        sudo service nginx configtest        sudo service nginx restart        sudo service nginx reload 

By: Richard

Can you post also on how to configure nginx as a reverse proxy with SSL for multiple servers (I'm using VMs for these servers, the physical server contains the nginx) and port forward nginx to the Internet.

Thanks!

By: Problem with clean wordpress install redirect 301

Hi,

Thanks for the tutorial! I have configured all the things like you say.. but, have a problem with a clean wordpress installation.. I can login to the wp-admin area, but, when I try to navigate the front.. the navigator continuosly loop in a 301 redirection and finish then showing ERR_TO_MANY_REDIRECTS

You can see here: http://destinopunilla.com.ar/wordpress/wordpress/

But, I can load ok a simple php file and you can see here: http://destinopunilla.com.ar/sitioweb/verphpdestino2.php

 

Mi nginx server for the domain contain this:

-------------------------------------------------

server {

        listen 80 default_server;

        listen [::]:80 default_server;

        root /home/destinopunilla/public_html;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name destinopunilla.com.ar www.destinopunilla.com.ar;

        ##        location / {

                try_files $uri $uri/ /index.php;

        }

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

        }

 

        ##        location ~* \.(bmp|gif|ico|jpeg|jpg|pict|png|svg|swf|tif)$ {

                try_files $uri =404;

        }

 

        ##        location ~* \.(class|css|csv|doc|docx|ejs|eot|eps|jar|js|mid|midi|otf|pdf|pls|ppt|pptx|ps|svgz|tiff|ttf|txt|webp|woff|woff2|xls|xlsx)$ {

                try_files $uri =404;

        }

}

--------------------------------------------

The ports of apache were changed to listen 8080, and my virtualhost of apache domain contain this:

<VirtualHost 127.0.0.1:8080>

SuexecUserGroup "#1002" "#1002"

ServerName destinopunilla.com.ar

        ServerAlias www.destinopunilla.com.ar

        ServerAlias mail.destinopunilla.com.ar

        ServerAlias webmail.destinopunilla.com.ar

        ServerAlias admin.destinopunilla.com.ar

        DocumentRoot /home/destinopunilla/public_html

        ErrorLog /var/log/virtualmin/destinopunilla.com.ar_error_log

        CustomLog /var/log/virtualmin/destinopunilla.com.ar_access_log combined

        DirectoryIndex index.html index.htm index.php index.php4 index.php5

        <Directory /home/destinopunilla/public_html>

                Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch +ExecCGI

                allow from all

                AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch

                Require all granted

                AddType application/x-httpd-php .php

                AddHandler fcgid-script .php

                AddHandler fcgid-script .php7.4

                FCGIWrapper /home/destinopunilla/fcgi-bin/php7.4.fcgi .php

                FCGIWrapper /home/destinopunilla/fcgi-bin/php7.4.fcgi .php7.4

        </Directory>

</VirtualHost>

 

Can you tell where could be the missconfiguration to work well with a wordpress front side?

 

By: ezequiel cattaneo

Hi,

 

Thanks for the tutorial! I have configured all the things like you say.. but, I have a problem with a clean wordpress installation.. I can login to the wp-admin area without problems, but, when I try to navigate the frontside.. 

the browser enters a continuous loop (301 document / redirection) and finish then showing ERR_TO_MANY_REDIRECTS

 

You can see the problem here: 

http://destinopunilla.com.ar/wordpress/wordpress/

 

But, I can load ok a simple php file, you can see here: 

http://destinopunilla.com.ar/sitioweb/verphpdestino2.php

 

 

Mi nginx server for the domain contain this:

-------------------------------------------------

 

server {

 

        listen 80 default_server;

        listen [::]:80 default_server;

        root /home/destinopunilla/public_html;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name destinopunilla.com.ar www.destinopunilla.com.ar;

 

        ##

        location / {

                try_files $uri $uri/ /index.php;

        }

 

        ##

        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;

        }

 

        ##

        location ~* \.(bmp|gif|ico|jpeg|jpg|pict|png|svg|swf|tif)$ {

                try_files $uri =404;

        }

 

        ##

        location ~* \.(class|css|csv|doc|docx|ejs|eot|eps|jar|js|mid|midi|otf|pdf|pls|ppt|pptx|ps|svgz|tiff|ttf|txt|webp|woff|woff2|xls|xlsx)$ {

                try_files $uri =404;

        }

 

}

 

--------------------------------------------

 

 

The port of apache was changed to listen 8080, and my virtualhost of apache for the domain actually contain this:

--------------------------------------------

<VirtualHost 127.0.0.1:8080>

SuexecUserGroup "#1002" "#1002"

ServerName destinopunilla.com.ar

        ServerAlias www.destinopunilla.com.ar

        ServerAlias mail.destinopunilla.com.ar

        ServerAlias webmail.destinopunilla.com.ar

        ServerAlias admin.destinopunilla.com.ar

        DocumentRoot /home/destinopunilla/public_html

ErrorLog /var/log/virtualmin/destinopunilla.com.ar_error_log

        CustomLog /var/log/virtualmin/destinopunilla.com.ar_access_log combined

        DirectoryIndex index.html index.htm index.php index.php4 index.php5

        <Directory /home/destinopunilla/public_html>

                Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch +ExecCGI

               allow from all

               AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch

                Require all granted

                AddType application/x-httpd-php .php

                AddHandler fcgid-script .php

                AddHandler fcgid-script .php7.4

                FCGIWrapper /home/destinopunilla/fcgi-bin/php7.4.fcgi .php

                FCGIWrapper /home/destinopunilla/fcgi-bin/php7.4.fcgi .php7.4

        </Directory>

</VirtualHost>

--------------------------------------------

 

 

Can you tell me where could be the missconfiguration to work ok with a wordpress frontside?