nginx 运行 上的 django 应用程序与 apache 分开

django apps on nginx running separately from apache

我正在为我的 Django 应用程序安装生产服务器,但我无法让它工作。我的配置文件可以找到 .
基本上,我为我的 php 应用程序安装了 apache2 并在端口 80 上 运行ning。我想 运行 我的 django 应用程序在 nginx 上使用 uwsgi,除了 apache2。所以我在端口 8000 上 运行ning nginx。
当我打开 http://IP:8000/ 时,我可以正常看到我的网站。
1。但是如何设置域名呢?
我已经在 dns 中将 A 标记设置为 IP。现在它命中 apache2 "it works" 页面是因为它默认命中端口 80?所以我需要代理将所有请求传递给我的 domain.com?是这样的吗?
/etc/apache2/sites-enabled/domain.com:

<VirtualHost *:80>
  ServerName domain.com

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

它不起作用,那么如何将所有域请求从 apache 传递到 nginx?
2。如何添加另一个域名? (新应用)
我是否只为新应用程序创建新套接字文件,将其保留在端口 8000 上,nginx 将根据域名决定使用哪个 conf 文件?

没找到类似的教程,nginx一般处理静态文件,向apache2发送请求。但是我想要其他方式。

1.你如何设置域名? 在 nginx conf 文件的服务器块中设置 server_name:

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

您的站点将在 http://www.my-django-domain-one.foobar:8000 可用。

2。如何添加另一个域名? (新应用) Nginx 不会根据 conf 文件名来决定任何事情。创建一个新的 conf 文件或使用现有的(仅在您希望如何组织配置的意义上很重要)

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

但是,我推荐一种只涉及一个网络服务器的方法。关于使用哪个的意见当然会有所不同,但他们都可以自己做你想实现的事情。您为您的设置增加了不必要的复杂性(例如,两台服务器保持打补丁)并且 - 根据您的流量 - 它甚至可能对您的性能产​​生重大影响。

查看此 tutorial 了解如何让您的 php 应用程序与 nginx 和 php-fpm 一起工作。

感谢您的回答。为了让它工作,我必须像这样设置 apache 代理:

<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/ [P,L]
</VirtualHost>

并启用 proxy_http:

sudo a2enmod proxy  
sudo a2enmod proxy_http  
sudo service apache2 restart