There will be a time in your life where you want to set nginx up to serve all of your static content. Then you’ll get fancy with your programming and want to serve dynamic content. Heres the thing, you don’t want all of your requests to be served dynamically. You want the front facing web server to serve up your static content, while still letting connections through to your dynamic content engine. I’m going to show you how to do it. In our case, we’re using connect-vhoster in combination with node.js, connect and spark to serve dynamic content. Let’s get into the nitty gritty:

I want mine to work with wild card subdomains so i can set up anything on the fly. The nginx.conf I’m about to explain already has support for that, but you’ll need to make sure that you set up wild card DNS with your DNS host first.

In your nginx.conf we need a server block (this goes at the end of the http block) Here is mine:

server {
  listen 80;
  server_name ~^(?<domain>.*techosaur.us);
  root /var/webapps/vhosts/$host/public/;
  try_files $uri $uri/ @nodevhost;

  location @nodevhost {

    access_log      /var/log/nginx/vps-access-log;
    proxy_pass      http://localhost:3001;
    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;

  }
}

So there it is. It’s neat, it’s clean, and it’s not very descriptive.  I’ll explain it line by line:

  server 80;

This just tells nginx to listen on the standard http port of 80.

 

  server_name ~^(?<domain>.*techosaur.us);

This sets server_name to whatever the wild card subdomain of the moment is. It’s totally dynamic and very smooth.

 

  root /var/webapps/vhosts/$host/public/;

Here we set the root of the site of the moment to the correct directory based on the subdomain ($host). For example, if we’re being accessed on lollerskates.techosaur.us then $host is set to lollerskates.techosaur.us. If you’re using the connect middleware vhost module that I do (and I think you should), then your directory names in /var/webapps/vhosts match the subdomains that you want to use. This is a key part of letting nginx serve static files for your wild card subdomains. Without this, nginx won’t be able to find the files.

 

  try_files $uri $uri/ @nodevhost;

This is the most important part of nginx static file serving. This line tries to serve $uri (/something/whatever.html) from the root that we defined above, if that doesn’t work it’ll try it with a / on the end, and if that doesn’t work then it’s probably a dynamic page and will and it off to location @nodevhost, which is our reverse proxy.

I’m going to skim over the location @nodevhost block because this is covered elsewhere, but essentially it’s just a reverse proxy to localhost on port 3001 which is the port that my vhost module listens on. We forward some headers in the 2nd part which allow the vhosts to see the real remote IP instead of nginx’s IP.

And that’s about it :D

I want to ride my bicycle!

Comments Off

So I have a bike. It’s a mountain bike. I enjoy riding it quite a bit in the mountains, but around town it’s kind of a pain. The bike weighs like 50 pounds so it adds a lot to what my legs have to move. On top of that, it’s full suspension. Although full suspension is cool and makes bumps less bumpy, it takes about half the power out of your stroke. So when I’m trying the 100% up hill course home, I end up looking like I’ve run a marathon at my destination.

To solve this problem I’ve decided to buy a new bike. A road bike. One that doesn’t include shock absorbers, has small tires instead of 2″ wide tires, and will be light enough to pick up with one finger. My budget is $1000 and under (preferably around $700). Right now I have no clue what bike to get. I was looking at the Trek 7.1 FX, but i really want drop bars and more road geometry. This bike is never going to leave the road. I took a look at Trek’s “Town” section on their website but all of the bikes there are fixies. I just don’t want a fixie. 50+% of my ride is up hill and it just won’t work out on a fixie. I need gears! So now I’m at square one, I have no clue what bike to get.

Maybe I’ll go into the bike store and just see what they recommend.

My Body is Trolling Me.

Comments Off

I think my body is trolling me. I feel like shit all day long… all I want to do is crawl in bed and sleep. So then I get in bed to go to sleep at 22:00 and I’m just wide awake now. I’m thinking about the bike I want to purchase. I’m thinking about my new job. I’m thinking about my old job. I’m thinking about the mountain of homework that should have been done today but isn’t even started. Ugh. Body… quit trolling.

In other news I’ve had some major allergies lately and I think it may be related to the pillows that I’m using. I bought some pillows from Ikea about two weeks ago and I’ve been a little congested ever since then. Now I’m super congested and waking up with my eyes glued shut every morning. Really the only thing that I’ve changed in regards to my sleep situation is my pillows. I’m trying a different pillow tonight. If that seems to help then I’ll switch back to the Ikea pillow and see if it gets worse. It may just be seasonal allergies or a cold, but it’s possibly the pillows.

If the sun was massive.

Comments Off

Wouldn’t it be really cool if the sun looked freaking huge? Not this tiny albeit bright light in the sky, but like a giant huge flaming ball of light that you could look straight at without burning your retinas off. I think that would be awesome. Here is a professional artist rendering with a house for size reference:

FREAKING HUGE SUN.

 

 

 

 

 

 

 

 

Do you see how awesome that is?