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

I am running a server with nginx on port 80 and Apache on 8080. I want the home page of my site to be served with nginx, and every other request passed through to Apache. I found this great article and understand the nginx proxy_pass directive, but I can't figure out the right regex to tell nginx to only serve the home page of my site. Since users will come to the site by just visiting http://mysite.com (without /index.htm), I don't know what "location" value I should use.

Here's an example config file that demonstrates how to have all pages sent to Apache (like I want) except the /css folder, and image files. As you can see nginx uses a simple regex to specify what should be served by nginx. What regex would I use to specify only the home page should be served by nginx? Or should I be using try_files somehow?

server {
      root /usr/local/www/mydomain.com;
      server_name mydomain.com www.mydomain.com;

      # next line says anything that matches this regex should be sent to Apache     
      location / {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            # Apache is listening on port 8080
            proxy_pass http://127.0.0.1:8080;
      }

      # Example 1 - Specify a folder and its contents
      # To serve everything under /css from nginx only
      location /css { }

      # Example 2 - Specify a RegEx pattern such as file extensions
      # to serve only image files directly from Nginx
      location ~* ^.+\.(jpg|jpeg|gif|png)$
      {
            # this will match any file of the above extensions
      }
}
share|improve this question
    
Is this a contrived example or something? It doesn't seem likely that you only want to serve the home page from nginx. The usual configuration is to serve any static files, and pass everything else upstream. What are you really trying to do? –  Michael Hampton Mar 8 '13 at 5:15
    
Honest, this is a real thing. The URL is going to be blasted on TV screens across America and gets a huge traffic spike, but everyone is looking for something that will be right on the home page. The rest of the site needs to be available and running, but while the TV program is on, I have a special lean home page with the thing that 99% of folks are looking for.... so they'll see what they want (on the one single nginx-served home page) and leave, and the other 1% off folks can click off from there and use the rest of the site (served by Apache through nginx reverse proxy). –  Eric Mar 8 '13 at 5:20
    
Hmm, so you make the home page a static file for the duration. Takes five seconds, and doesn't require any bizarre configurations. –  Michael Hampton Mar 8 '13 at 5:21
    
But the problem is a jillion people come to the site and even with the special lean (static) home page, Apache gets buried quickly. That's why I want nginx to serve it. –  Eric Mar 8 '13 at 5:22

1 Answer 1

up vote 0 down vote accepted

OK, so what you really need to be doing is:

  1. Serve all static files from nginx, and only pass upstream to Apache any other requests.
  2. When necessary for performance reasons, make the homepage a static file such as index.html in the document root. Thus, nginx will serve it directly. Delete it to return to the previous behavior.

Your configuration should look something like this:

server {
      root /usr/local/www/mydomain.com;
      server_name mydomain.com www.mydomain.com;

      index index.html;

      location @upstream {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            # Apache is listening on port 8080
            proxy_pass http://127.0.0.1:8080;
      }

      location / {
            try_files $uri $uri/ @upstream;
      }
}

Later you should look at doing caching within your web app; if it writes generated HTML files to the disk, you could then have nginx serve those files directly out of its cache.

share|improve this answer
    
This seems like it should work, but my site is powered by Wordpress and it's just failing left and right-- nginx won't let me access any subdirectories, and PHP code from a WP plugin is being shown mixed in the HTML code (which obviously screws up the page). I'm guessing all this is because WP routes pages strangely, or something along those lines. –  Eric Mar 8 '13 at 18:18
    
In that case, why are you using Apache at all? –  Michael Hampton Mar 8 '13 at 20:04
    
Because Wordpress likes Apache and its .htaccess file... and the caching plugin I'm using does a lot to the .htaccess file as well. And I'm inheriting the whole project and not looking to rework it all :-) Good news is, I found a solution: I moved Apache up to port 8080 and kept nginx on port 80. Nginx never really serves any Apache pages; once someone clicks on the home page, I add :8080 to the URL and they're handled by Apache. Works perfectly for my needs. –  Eric Mar 11 '13 at 3:37
1  
    
Thanks for the link-- I hadn't seen that before. In browsing that page, I think it definitely shows how much Apache wants to work with .htaccess, and what a pain in the ass it is to get Wordpress (and whatever plug-ins you're using) going with nginx. That's a lot of by-hand customization. I kinda like my dual-port trick ;-) ...in all seriousness, it does the job for the two hours that the URL is broadcast on the air. The rest of the time I just run the site as a normal Apache setup since the traffic isn't spiking. Thanks for all your help and good information. –  Eric Mar 13 '13 at 16:46

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.