Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

For reasons to hideous to go into... I'm using Nginx as a webserver and would like it to serve one page for internal users (say on 10.0.0.0/16) and another page to external users on any other IP address.

For example:

  • "Internal" PC with an IP of 10.0.0.34 goes to company.com/page.html gets page internal.html

  • "External" PC with an IP of 8.8.8.8 goes to company.com/page.html gets page external.html

share|improve this question
1  
To clarify; Using Nginx isn't hideous, what I need it to do is! Nginx is great! –  Jon Rhoades May 9 '11 at 12:41

1 Answer 1

up vote 9 down vote accepted

Make use of Nginx geo module. It lets you set variable's value based on a client IP address. geo directive must be in http section:

http {
  geo $client {
    default extra;
    10.0.0.0/8 intra;
  }

You can use it later in locations to lookup files

location / {
  try_files $uri.$client $uri = 404;
}

Which means, Nginx will set $client to either extra or intra based on a client's IP. Let's assume it's a Intranet client. If a client asks for page.html, Nginx will search for file /your/root/page.html.intra. If there is no such file, it will search for /your/root/page.html. If it cannot find neither of these, Nginx returns 404 "Not Found" response. More on "try_files" in the documentation

By the way, index directive supports variables as well. E.g.

index index.$client.html index.html;
share|improve this answer
1  
Worked nicely - I ended up doing location = /filename & rewrite ^ /filename.$client last; –  Jon Rhoades May 12 '11 at 6:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.