Always Get Better

Posts Tagged ‘windows’

Reverse Proxy with IIS and Elastic Beanstalk

Monday, September 4th, 2017

Suppose your main website is a .NET/IIS stack running on AWS Elastic Beanstalk, and you decided to add a WordPress blog to the mix. Instead of having http://blog.yourdomain.com and http://www.yourdomain.com, you want to host the blog from a subdirectory at http://www.yourdomain.com/blog. There are a few ways you can go about this; this post will go through how to set up a reverse proxy to a dedicated blog server.

You have two options:

1. Host WordPress in IIS – Not fun. You need to configure IIS to run PHP, host a MySQL database, and manage your WordPress file directory and updates in an environment where user uploads and core files can get trashed at any minute when EB rolls out a new server. It’s possible to run an amazing HA WordPress install on Elastic Beanstalk (topic for another post) but in a subdirectory directly hosted in Windows/IIS? Not for the feint of heart)

2. Host WordPress on a Linux server somewhere else and reverse proxy – Let’s do this instead.

Basically you set up WordPress and get it to look how you want, configure it to respond to your domain, then configure IIS to fetch your blog URLs and return them whenever a request comes through to the blog subdirectory on your main site. Every other directory is served from your .NET app as normal.

Reverse Proxy a WordPress blog with IIS

Important: The final step to this is the most important bit if you’re running on Elastic Beanstalk. Make sure you follow it in order to actually enable IIS’ proxy module, which does not come pre-installed on Elastic Beanstalk’s AMI.

Configure WordPress to Respond to Your Domain

Add this section to the bottom of wp-config.php, just before the require_once for wp-settings.php. This tells WordPress to ignore its default server settings and use your website as its base path.

$hostname = 'www.yourdomain.com';
$_SERVER['HTTP_HOST'] = $hostname;
define('WP_HOME','https://www.yourdomain.com/blog');
define('WP_SITEURL','https://www.yourdomain.com/blog');
$current_url = 'https://' . $hostname . $_SERVER['REQUEST_URI'];

Configure IIS Paths

The reverse proxy makes use of the rewrite in IIS. To turn this on for your blog, add the following to the directive in your web.config file:

 <rewrite>
   <rules>
     <rule name="Reverse Proxy to blog" stopProcessing="true">
       <match url="^blog/(.*)" />
       <action type="Rewrite" url="https://blog.yourdomain.com/{R:1}" />
     </rule>
   </rules>
 </rewrite>

This tells IIS to redirect all requests beginning with “blog/” to your WordPress blog. IIS will reach out to your blog server as if it is the requestor, fetch the page, and return it as if the blog were hosted from within IIS itself. The {R:1} variable carries forward any path information to the blog — so your theme files, user uploads and static assets will all pass through.

If you deploy your site and try to access your blog page now, it won’t work. You’ll see a ‘404’ response code even though the rules are definitely set up properly. The final step to this is enabling the Application Request Routing module on IIS – this is not enabled by default in Elastic Beanstalk’s version of Windows.

Enabling the Reverse Proxy Module on Elastic Beanstalk

You could Remote Desktop into your web server machine and manually enable the ARR module, but it would stop working the next time your environment flips, the server gets reloaded for any reason (which can happen even if you are doing all at once deployments and not adding/removing machines), or nodes get added to your environment.

We need to make sure the module gets installed and checked every time you deploy your files, so it’s always present and available to use even when new machines come online.

To do that, we’ll use the .ebextensions scripting hooks to download, install and configure ARR every time a deploy runs.

1. Download the ARR Installer

Download the 65-bit ARR installer (from here) to S3 so it is available to your VM when it boots. We want to install to S3 instead of pulling directly off Microsoft’s servers because we can’t rely on outside links being available when we need to deploy, and if our VM happens to be inside a VM without a NAT then we can use Amazon’s S3 internal endpoints without needing to configure any more advanced network.

2. Add an ebextension hook

In your .ebextensions folder (at the root of your unzipped deploy package), add a new config file (install-arr.config) to instruct Elastic Beanstalk how to install the extension:

packages:
  msi:
    ApplicationRequestRouting: "pathtoyourinstallerons3"

commands:
  01_enablearr:
    command: "C:\\Windows\\system32\\inetsrv\\appcmd.exe set config  -section:system.webServer/proxy /enabled:True  /commit:apphost"

The packages/msi lines tell Elastic Beanstalk to download and run the installer. Since you won’t be physically present when that happens, the script will automatically accept all the license agreements and silently run.

The appcmd command instructs IIS to enable the reverse proxy module, which turns your rewrite instructions into actual reverse proxy commands. Now if you visit www.yourdomain.com/blog/, you will see your WordPress blog.

Bonus: Trailing Slashes

If you visit www.yourdomain.com/blog without a trailing slash, you won’t see the blog. You don’t want to start the rewrite rule at this level because your reverse proxy will try to access blog.yourdomain.com// (with a double slash) for all dependent resources, which can cause problems with WordPress’ routing.

For this case, I like to redirect to a trailing slash at the IIS level. Every time someone comes to yourdomain.com/blog, they will redirect with a clean 302 status command to yourdomain.com/blog/. Just add this rule in your section of web.config

<rule name="Add trailing slash to blog reverse proxy" stopProcessing="true">
  <matchurl="^blog$"/>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/"/>
</rule>

The regular expression in the match url tells the web server this should only apply to the “blog” path, that is, nothing before or after the word “blog”. This lets you have “blog” in other parts of your URL without accidentally redirecting valid pages.

Life in Linux

Friday, August 6th, 2010

So I wiped my hard drive and installed Ubuntu. After struggling with the decision to switch from Windows for some time, I finally resolved to move.

So far the results have been very good. My system boots up and is ready to use in less than a minute, there is no lag loading and switching programs, and everything I need for my day-to-day programming is available much more readily than it was with the other operating system.

The most striking difference to me is the amount of disk space I now have available to me. With all of my software, work projects, and operating system overhead, Windows left 80Gb free from my 285Gb drive. With all of my projects, code libraries, files and operating system installed, Ubuntu uses just 6.7Gb, leaving 97% of the drive available for my use. I am blown away by how much less clutter I have now.

I haven’t tried to do very much with Mono yet; we’ll see how it works when I try making improvements to my SiteAssistant project. I’ve been reading about Mono’s Winforms capabilities and so far am impressed by the possibilities. We’ll see how well it works with my fairly simple project; with any luck I may have found a cross-platform .NET solution with this one. Maybe the Winforms explorations will be a good topic for a future post.

Not missing Office yet, either. My Quicken financial software has been running perfectly under Wine, and all of my files appear to have made the move intact. I still own licenses to all my software, so on those rare instances if I really need it I can install Windows with VirtualBox and fill up some of that hard drive space I’ve earned.

Thinking About Switching to Ubuntu

Monday, August 2nd, 2010
Ubuntu-logo-unique-image
Creative Commons License photo credit: Jeffpro57

In the last number of weeks I have been seriously considering taking the plunge and wiping my Windows laptop clean in order to switch to Ubuntu as my primary machine. Although Windows 7 has gone a long way toward smoothing over the problems Vista brought, it isn’t perfect.

Windows isn’t a bad operating system, by any means. Like OS X and Ubuntu, it has its strengths and weaknesses. However, as a developer whose primary work involves web pages, I definitely see Windows as more of a barrier to efficient workflow. There are a few pieces of software that have kept me on Windows for awhile but which just don’t hold me back anymore:

1. Microsoft Office
I definitely qualify as a power user for this software. Yes, OpenOffice can do most of what MS Office can, but I will sorely miss the features that “most people” don’t use. However, the majority of my work doesn’t touch Office – in fact, it’s relatively rare that I will need Access, or Word, or even Excel. When I do use these programs, it only tends to be in support of a client who has used them inappropriately for some data storage.

I’ve long outgrown Outlook due to the amount of mail I keep; I don’t like to delete anything because true professionals are able to refer back to projects no matter how old. I don’t have a replacement mail program yet; but so far the Gmail interface has been more than sufficient.

2. Visual Studio
This software is giving me pause. If I switch over, I will be giving up my ability to truly work in the .NET world, which is where I have largely been for the past decade. Most of my workflow recently has been with the open source, PHP-driven web world and I’m not sure that I’m excited about going back to a pure Microsoft environment. That said, I want to be sure I’m not closing any doors.

Mono has made great strides in bringing the .NET platform, specifically C#, over to Mac and Unix, but the more Windows-centric database and GUI interfaces don’t translate over very well. I can always run a Windows Virtual Machine for the rare instances I will need to work on that platform, but it seems a bit counter-productive to keep around an environment that I don’t use for the sake of a few days each year.

3. Quicken
My other strong reason for staying with Windows has been my love for Quicken – the Mac version just doesn’t compare to the Windows version – and my inability to manage my finances on paper after years of dependence. Since so much of my data is tied into this software, any switch will involve either years of data entry or a major hit to my forecasting abilities.

Fortunately, Wine has come to the rescue – last night I was able to get a full install of Quicken on my test/Ubuntu machine with absolutely no problems! The fonts looked a little weird in the reports, but otherwise all of the functionality was there and working beautfully. It even looked like a Linux app – unbelievable!

Should I Stay or Should I Go?
So the big thing I’m weighing in my head right now is whether I can stand to give up the .NET programming I have been involved with for so long and switch to a full Linux environment. I still love my Windows environment and software, but it just doesn’t seem to make sense to keep it given my current open source focus.