{"id":280,"date":"2013-06-14T19:09:17","date_gmt":"2013-06-14T19:09:17","guid":{"rendered":"http:\/\/blog.shineservers.com\/?p=280"},"modified":"2013-06-14T19:09:17","modified_gmt":"2013-06-14T19:09:17","slug":"how-to-configure-nginx-as-a-front-end-proxy-for-apache-on-ubuntu","status":"publish","type":"post","link":"https:\/\/www.shineservers.com\/2013\/06\/14\/how-to-configure-nginx-as-a-front-end-proxy-for-apache-on-ubuntu\/","title":{"rendered":"How to Configure Nginx as a Front End Proxy for Apache On Ubuntu"},"content":{"rendered":"<p>Both nginx and apache are powerful and effective servers. Apache currently reigns as the #1 server for websites and since its public release in 2006, nginx has taken the world by storm and is now the #2 server for active sites. The reasons for each respective server\u2019s popularity are clear: apache\u2019s power and nginx\u2019s speed are well known. However, both servers do have drawbacks\u2014apache is hard on server memory, while nginx (great at static files) needs the help of php-fpm or similar modules for dynamic content.<\/p>\n<p>However, one can combine the two web servers to great effect, with nginx as static web server front and apache processing the back end.<\/p>\n<p><span style=\"font-size: medium;\">Install Nginx<\/span><\/p>\n<p>To start off, we need to install and configure nginx which will serve the front end of our site.<\/p>\n<p>Let\u2019s download it from apt-get:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo apt-get install nginx<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_1\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 1079&#215;71.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img decoding=\"async\" id=\"ncode_imageresizer_container_1\" alt=\"\" src=\"http:\/\/i.imgur.com\/1NfKIPS.png\" width=\"700\" height=\"46\" border=\"0\" \/><\/p>\n<table id=\"ncode_imageresizer_warning_2\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 743&#215;210.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img fetchpriority=\"high\" decoding=\"async\" id=\"ncode_imageresizer_container_2\" alt=\"\" src=\"http:\/\/i.imgur.com\/p6o5dFz.png\" width=\"700\" height=\"197\" border=\"0\" \/><\/p>\n<p>Once it has downloaded, you can go ahead and configure the virtual host to run on the front end.<\/p>\n<p>There are a few changes we need to make in the configuration.<\/p>\n<p><span style=\"font-size: medium;\">Configure nginx<\/span><\/p>\n<p>Open up the nginx configuration.<\/p>\n<p><b>Install Nano :-<\/b><\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo apt-get install nano<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_3\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 1015&#215;342.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_3\" alt=\"\" src=\"http:\/\/i.imgur.com\/9peOzHx.png\" width=\"700\" height=\"235\" border=\"0\" \/><\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo nano \/etc\/nginx\/sites-available\/example<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_4\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 984&#215;22.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_4\" alt=\"\" src=\"http:\/\/i.imgur.com\/3ZvWEvH.png\" width=\"700\" height=\"15\" border=\"0\" \/><\/p>\n<table id=\"ncode_imageresizer_warning_5\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 893&#215;679.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_5\" alt=\"\" src=\"http:\/\/i.imgur.com\/Ad0t9BY.png\" width=\"700\" height=\"532\" border=\"0\" \/><\/p>\n<p>The following configuration will set you up to use nginx as the front end server. It is very similar to the default set up, and the details are under the configuration.<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">server {\n        listen   80; \n\n        root \/var\/www\/; \n        index index.php index.html index.htm;\n\n        server_name example.com; \n\n        location \/ {\n        try_files $uri $uri\/ \/index.php;\n        }\n\n        location ~ \\.php$ {\n\n        proxy_set_header X-Real-IP  $remote_addr;\n        proxy_set_header X-Forwarded-For $remote_addr;\n        proxy_set_header Host $host;\n        proxy_pass http:\/\/127.0.0.1:8080;\n\n         }\n\n         location ~ \/\\.ht {\n                deny all;\n        }\n}<\/pre>\n<\/div>\n<p>The following changes were implemented in the configuration:<\/p>\n<ul>\n<li>The root was set to the correct web directory<\/li>\n<li>index.php was added on the index line<\/li>\n<li>try_files attempts to serve whatever page the visitor requests. If nginx is unable, then the file is passed to the proxy<\/li>\n<li>proxy_pass lets nginx the address of the proxied server<\/li>\n<li>Finally the &#8220;location ~ \/\\.ht {&#8221; location block denies access to .htaccess files, if Apache&#8217;s document root concurs with nginx&#8217;s one<\/li>\n<\/ul>\n<p>This configuration sets up a system where all extensions with a php ending are rerouted to the apache backend which will run on port 8080.<\/p>\n<p>Activate the virtual host.<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo ln -s \/etc\/nginx\/sites-available\/example \/etc\/nginx\/sites-enabled\/example<\/pre>\n<\/div>\n<p>Additionally, delete the default nginx server block.<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo rm \/etc\/nginx\/sites-enabled\/default<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_6\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 837&#215;102.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_6\" alt=\"\" src=\"http:\/\/i.imgur.com\/nqVxWyP.png\" width=\"700\" height=\"85\" border=\"0\" \/><\/p>\n<p>The next step is to install and configure apache.<\/p>\n<p><span style=\"font-size: medium;\">Install Apache<\/span><br \/>\nWith nginx taken care of, it\u2019s time to install our backend, apache.<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo apt-get install apache2<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_7\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 785&#215;54.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_7\" alt=\"\" src=\"http:\/\/i.imgur.com\/q4D94Ez.png\" width=\"700\" height=\"48\" border=\"0\" \/><\/p>\n<p>Since nginx is still not turned on, Apache will start running on port 80.<\/p>\n<p><span style=\"font-size: medium;\">Configure Apache<\/span><br \/>\nWe need to configure apache to take over the backend, which as we told nginx, will be running on port 8080. Open up the apache ports file to start setting apache on the correct port:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo nano \/etc\/apache2\/ports.conf<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_8\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 852&#215;504.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_8\" alt=\"\" src=\"http:\/\/i.imgur.com\/2I3rIVJ.png\" width=\"700\" height=\"414\" border=\"0\" \/><\/p>\n<p>Find and change the following lines to have apache running on port 8080, accessible only from the localhost:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">NameVirtualHost 127.0.0.1:8080\nListen 127.0.0.1:8080<\/pre>\n<\/div>\n<p>Save and Exit.<\/p>\n<p>Subsequently, open up a new virtual host file, copying the layout from the default apache file:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo cp \/etc\/apache2\/sites-available\/default \/etc\/apache2\/sites-available\/example<\/pre>\n<\/div>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo nano \/etc\/apache2\/sites-available\/example<\/pre>\n<\/div>\n<p>The main issue that needs to be addressed here is that the virtual host needs to be, once again, running on port 8080 (instead of the default 80 given to nginx).<\/p>\n<p>The line should look like this:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">&lt;VirtualHost 127.0.0.1:8080&gt;<\/pre>\n<\/div>\n<p>Make sure your Document Root is correct. Save and exit the file and activate that virtual host:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo a2ensite example<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_9\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 922&#215;188.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_9\" alt=\"\" src=\"http:\/\/i.imgur.com\/zL5clnr.png\" width=\"700\" height=\"142\" border=\"0\" \/><\/p>\n<p>Before we start testing anything out, we need to equip apache with php. Go ahead and install it now:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\"> sudo apt-get install php5<\/pre>\n<\/div>\n<p>Restart both servers to make the changes effective:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo service apache2 restart<\/pre>\n<\/div>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo service nginx restart<\/pre>\n<\/div>\n<table id=\"ncode_imageresizer_warning_10\" width=\"700\">\n<tbody>\n<tr>\n<td width=\"20\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.wjunction.com\/images\/statusicon\/wol_error.gif\" width=\"16\" height=\"16\" border=\"0\" \/><\/td>\n<td>This image has been resized. Click this bar to view the full image. The original image is sized 705&#215;100.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ncode_imageresizer_container_10\" alt=\"\" src=\"http:\/\/i.imgur.com\/aHrbJRB.png\" width=\"700\" height=\"99\" border=\"0\" \/><\/p>\n<p><span style=\"font-size: medium;\">Finish Up<\/span><br \/>\nWe have set up the VPS with nginx running on the front end of our site and apache processing php on the back end. Loading our domain will take us to our site\u2019s default page.<\/p>\n<p>We can check that information is being routed to apache is working by running a common php script.<\/p>\n<p>Go ahead and create the php.info file:<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo nano \/var\/www\/info.php<\/pre>\n<\/div>\n<p>Paste the following lines into that file:<\/p>\n<div>\n<div>PHP Code:<\/div>\n<div dir=\"ltr\"><code><\/code><code>&lt;?<br \/>\nphpinfo(\u00a0);<br \/>\n?&gt;<\/code><\/div>\n<\/div>\n<p>Save and exit.<\/p>\n<p>Visiting your domain\/info.php should show you php info screen, and you\u2019ll be able to see that this was handled by apache.<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"http:\/\/i.imgur.com\/8FOXZzJ.png\" border=\"0\" \/><\/p>\n<p>Finally, you can see which ports are open and which application is on each one by typing in this command.<\/p>\n<div>\n<div>Code:<\/div>\n<pre dir=\"ltr\">sudo netstat -plunt<\/pre>\n<\/div>\n<p>That&#8217;s all ! Let me know if anyone is facing any issues while installing the above codes .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Both nginx and apache are powerful and effective servers. Apache currently reigns as the #1 server for websites and since its public release in 2006, nginx has taken the world by storm and is now the #2 server for active sites. The reasons for each respective server\u2019s popularity are clear: apache\u2019s power and nginx\u2019s speed [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[60],"tags":[127],"class_list":["post-280","post","type-post","status-publish","format-standard","hentry","category-linux","tag-configure-nginx-as-reverse-proxy-ubuntu"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts\/280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":0,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}