简体   繁体   中英

django apps on nginx running separately from apache

I am installing production server for my django apps and I can not make it work. My configuration files can be found here .
Basically I have apache2 installed and running on port 80 for my php applications. I want to run my django apps on nginx with uwsgi, apart from apache2. So I am running nginx on port 8000.
When I open http://IP:8000/ I can see my site properly.
1. But how do I set it up with domain name?
I've set A tag in dns to IP. Now it hits apache2 "it works" page because it hits port 80 on default? So I need to proxy pass all requests to my domain.com? Something like this?
/etc/apache2/sites-enabled/domain.com :

<VirtualHost *:80>
  ServerName domain.com

  ProxyPreserveHost On
  ProxyPass / http://IP:8000
</VirtualHost>

It does not work, so how do I pass all domain requests from apache to nginx?
2. How do I add another domain name? (new app)
Do I just create new socket file for new app, keep it on port 8000 and nginx will decide depending on domain name what conf file to use?

I have not found any similar tutorials, nginx usually handles static files and sends requests to apache2. However I want it other way.

Thanks for your answer. To make it work I had to set apache proxy like this:

<VirtualHost *:80>
  ServerName www.domain.com

  ProxyPreserveHost On

  ProxyPass /static http://XX.XX.XX.XX:8000/static
  ProxyPassReverse /static http://XX.XX.XX.XX:8000/static


  ProxyPass / http://XX.XX.XX.XX:8000
  ProxyPassReverse / http://XX.XX.XX.XX:8000

  RewriteEngine On

  RewriteCond %{REQUEST_URI} ^(.(?!\.css|js|gif|png|jpg|ico))*$
  RewriteRule /(.*) http://XX.XX.XX.XX:8000/$1 [P,L]
</VirtualHost>

and enable proxy_http:

sudo a2enmod proxy  
sudo a2enmod proxy_http  
sudo service apache2 restart

1. How do you set it up with domain name? In the server block of your nginx conf file set the server_name:

    server {
        listen 8000;
        server_name www.my-django-domain-one.foobar;
        #rest of your config regarding forwarding to django...
    }

Your site will be available at http://www.my-django-domain-one.foobar:8000 .

2. How do you add another domain name? (new app) Nginx will not decide anything based on the conf filename. Create a new conf file or use the existing one (only matters in the sense of how you want to organize your configs)

    server {
        listen 8000;
        server_name www.my-django-domain-two.foobar;
        #rest of your config regarding forwarding to django...
    }

However, I recommend an approach involving only one web server. Opinions on which to use vary of course but both of them can do what you want to achieve on their own. You add unnecessary complexity to your setup (eg two servers to keep patched) and -depending on your traffic- it may even have a significant impact on your performance.

Check out this tutorial to see how you could make your php apps work with nginx and php-fpm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM