0

My django application hosted on Apache, and now I want to serve its static media through nginx, I don't have any prior experience in nginx...plus currently the static media is serve through Apache.. expecting some helping hand.

Apache 2.2 mod_wsgi nignx-0.7.65 Django 1.1.1

Thanks..

Switch
  • 159
  • 10

2 Answers2

4

Can you tell me what you have done till now. You have to install nginx and set up apache as a reverse proxy. You need to change the port apache listens on to 8080, and nginx will listen to port 80.

The request for static media will be delivered from disk directly, and the other files will be redirected to port 8080 to apache.

If you can ask some more specific questions I can help you with them.

Update

Check out Nginx solution for Apache ProxyPassReverse for an example on reverse proxy. For a more detailed example you can just ask and I will post it.

Better: Using Nginx as a Reverse Proxy to Get the Most Out of Your VPS

Update

Part of my nginx.conf for a subdomain looks like this:

server {
  listen      80;
  server_name domain.nl www.domain.nl ;
  error_log /var/www/vhosts/domain.nl/statistics/logs/error_log.nginx warn;

  location / {
    proxy_pass  http://www.domain.nl:8080$request_uri;
    include  /etc/nginx/proxy.conf;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
    root  /var/www/vhosts/domain.nl/httpdocs;
    expires 7d;
  }
 }

This listen to port 80 for domain.nl and www.domain.nl. When a request is recieved for non-static files the request is passed to port 8080 http://www.domain.nl:8080$request_ur.

When a request for static files is found jpg etc. This is given directly from disk /var/www/vhosts/domain.nl/httpdocs, the location where my website is stored.

Saif Bechan
  • 10,960
  • 10
  • 42
  • 63
  • I have setup both Apache 2.2 and nginx, Apache running on 80 and nginx is running on 8888. I'm kind of missed the points, I think I did the other way around but now I can directly access static media like http://localhost:8888/sites/media/ which is nginx. Now I can link Django's MEDIA_URL to this location, but I don't know weather it's correct or not. Please fill the gaps.. – Switch Apr 12 '10 at 03:23
  • Well this is usually done the other way around. If you link inside of django to your media than the request is still handled by apache. And this is not what you want. You want to let Nginx handle all the requests. I recomend you change this, it is not a hard setup. How is your server structure? Do you want to run subdomains, or is it a single domain setup? – Saif Bechan Apr 12 '10 at 08:11
  • Thanks, well it's a single domain setup.. and I want to know some basic information.. 1.Where do I put static media Apache's root or nginx root or somewhere else and how do I map the location of it to the server..2. Where do actually my Django app is resides nginx or Apache.3. I didn't get this one "The request for static media will be delivered from disk directly, and the other files will be redirected to port 8080 to apache." please give me more detailed explanation – Switch Apr 12 '10 at 09:01
  • Have you checked out the second link I sent you: Using Nginx as a Reverse Proxy to Get the Most Out of Your VPS. Basically what you do is just work with Apache when you start. Make everything so it works in Apache. That will become the backend. And then when everything works just how you want it in Apache, then you can configure Nginx in front of it. There are some minor steps to do so. – Saif Bechan Apr 12 '10 at 10:17
  • Now I get the idea.., Nginx is just pass the requests that come to 8080 (which is the actual back-end, so Apache will works normal) and any-other request that come to 80 (where static media is resides).. One more thing can you please post the configurations on Apache side (how Apache going to listen incoming request from nginx) – Switch Apr 12 '10 at 12:03
  • Ok listening part is done as per the article that you have provided, so now how cant I point my Django templates to static media. – Switch Apr 12 '10 at 12:56
  • Finally get it done.. but there's a doubt don't you think that is it ok to not to use MEDIA_URL, because nginx directly serves the static data.. – Switch Apr 12 '10 at 13:44
  • Oh sorry I do not know what MEDIA_URL is in Django. I use PHP. You can try and ask another question here on Serverfault for this. – Saif Bechan Apr 12 '10 at 14:44
1

Here it's explained a little better:

http://www.ventanazul.com/webzine/tutorials/django-deployment-guide-ubuntu

ducu
  • 111
  • 2