Nginx (for static files) and Apache (for dynamic content)?

6,629

Solution 1

They can be on the same port, if they are on different IPs. Or they can be on different ports with the same IP, just not both (Could also be different IPs and Different Ports). The multiple IP scenario is required if they will be different servers, but you could use multiple IPs on the same server.

I swear I am not trying to confuse you :-)

The location directive in the link you provided would go within the server directive (Notice that in the documentation for the location directive there is "context: server "). If you are using a recent Ubuntu version with a default apt install, you probably want to edit default in the sites-enabled directory. For example (kind of silly since it passes everything to Apache):

server {
    listen   80 default;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:2500;
    }
}

In this case I am using the same server for both. Nginx listens on 80, and Apache Listens on 2500. You would point DNS to the ip of the Nginx server since that is the one that handles all requests by proxying for the other servers. Basically, from the client perspective, they are only talking to the Nginx server.

Solution 2

I can't help with the nginx config, because I haven't used it yet myself, but if you have a look at the config snippets in that question you linked to you'll see that Apache is running on port 8080. That's the key to having both running at once - they need to be on different ports. Because nginx is proxying for Apache the user never sees what port the latter is running on, nor does he/she need to.

Share:
6,629

Related videos on Youtube

Matthew
Author by

Matthew

Updated on September 17, 2022

Comments

  • Matthew
    Matthew over 1 year

    So, my entire application runs on apache just fine. However, I want to test how much the requests per second increases if I put all static files through nginx instead.

    I found this thread: https://stackoverflow.com/questions/869001/how-to-serve-all-existing-static-files-directly-with-nginx-but-proxy-to-apache-t

    But I have a couple problems. I'm completely new to nginx, so I'm not sure where to put the configuration. (The file is at /etc/nginx/nginx.conf, but I don't know if I just add the code to the bottom or what?)

    Also, how can I have both servers running at the same time? Is it because they both listen on port 80? Right now I have to stop one to start the other, and that's as far as I've gotten.

    Thanks for any help.

  • Matthew
    Matthew about 14 years
    To be "running" on port 8080 does that just mean I change where apache "listens" to?
  • John Gardeniers
    John Gardeniers about 14 years
    Correct. Change it in the config, restart Apache and the two will happily run side by side.
  • Matthew
    Matthew about 14 years
    Alright, so I have both running (apache and nginx). Somethings weird though. The static files are fine. (I did that whitelist thing, so css and js files should be served from nginx) However, if it's not a static file, it should proxy to apache, right? For some reason, the apache default page appears, as if there's no content in the root folder. Should my application work exactly normal, or are there things where nginx is affecting how it works?