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
}
}
OK, so what you really need to be doing is:
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:
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.