在 Linux 上使用 Nginx 和 Gunicorn 托管 Django在 Linux 上使用 Nginx 和 Gunicorn 托管 Django在 Linux 上使用 Nginx 和 Gunicorn 托管 Django在 Linux 上使用 Nginx 和 Gunicorn 托管 Django
  • 业务
  • 目标
  • 支持
  • 关于
  • 联系我们
  • 登录
✕

在 Linux 上使用 Nginx 和 Gunicorn 托管 Django

发表 admin at 2025年2月28日
类别
未分类
标签

介绍

托管 Django Web 应用程序相当简单,尽管它可能比标准 PHP 应用程序更复杂。有几种方法可以处理 Django 与 Web 服务器的交互。 Gunicorn 无疑是最简单的之一。

Gunicorn(Green Unicorn 的缩写)充当 Web 服务器(本例中为 Nginx)和 Django 本身之间的中间服务器。它负责为应用程序本身提供服务,而 Nginx 则获取静态内容。

鳐鱼

安装

使用 Pip 安装 Gunicorn 非常简单。如果您已经使用 virtualenv 设置了 Django 项目,那么您已经拥有 Pip 并且应该熟悉它的工作方式。因此,在您的 virtualenv 中安装 Gunicorn。

$ pip install gunicorn

配置

Gunicorn 成为一个有吸引力的选择的原因之一是其配置的简单性。处理配置的最佳方法是在 Django 项目的根目录中创建一个 Gunicorn 文件夹。在该文件夹中,创建一个配置文件。

在本指南中,它将被称为 gunicorn-conf.py。在该文件中,创建类似于以下配置的内容。


import multiprocessing

bind = 'unix:///tmp/gunicorn1.sock'
workers = multiprocessing.cpu_count() * 2 + 1
reload = True
daemon = True

在上述配置的情况下,Gunicorn 将在 /tmp/gunicorn1.sock 创建一个 Unix 套接字。它还将启动多个工作进程,其数量相当于 CPU 核心数量的两倍加一。它还将自动重新加载并作为守护进程运行。

跑步

运行 Gunicorn 的命令有点长,但其中指定了其他配置选项。最重要的部分是将 Gunicorn 指向您项目的 .wsgi 文件。

gunicorn -c gunicorn/gunicorn-conf.py -D --error-logfile gunicorn/error.log yourproject.wsgi

上面的命令应该从项目的根目录运行。它告诉 Gunicorn 使用您通过 -c 标志创建的配置。 -D 再次指定它应该被守护。最后一部分指定了 Gunicorn 错误在您创建的 Gunicorn 文件夹中的位置。该命令最后告诉 Gunicorn 您的 .wsgi 文件的位置。

nginx

现在 Gunicorn 已配置并运行,您可以设置 Nginx 与其连接并提供静态文件。本指南假设您已经配置了 Nginx,并且您正在为通过它托管的站点使用单独的 server 块。它还将包含一些 SSL 信息。

如果您想了解如何为您的网站获取免费的 SSL 证书,请查看我们的 LetsEncrypt 指南。


# Set up the connection to Gunicorn
upstream yourproject-gunicorn {
    server unix:/tmp/gunicorn1.sock fail_timeout=0;
}

# Redirect unencrypted traffic to the encrypted site
server {
    listen       80;
    server_name  yourwebsite.com;
    return       301 https://yourwebsite.com$request_uri;
}

# The main server block
server {
	# Set the port to listen on and specify the domain to listen for
    listen 443 default ssl;
    client_max_body_size 4G;
    server_name yourwebsite.com;

	# Specify log locations
    access_log /var/log/nginx/yourwebsite.access_log main;
    error_log /var/log/nginx/yourwebsite.error_log info;

	# Point Nginx to your SSL certs
    ssl on;
    ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem;

	# Set your root directory
	root /var/www/yourvirtualenv/yourproject;

	# Point Nginx at your static files
    location /static/ {
		# Autoindex the files to make them browsable if you want
        autoindex on;
        # The location of your files
        alias /var/www/yourvirtualenv/yourproject/static/;
		# Set up caching for your static files
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        proxy_ignore_headers "Set-Cookie";
    }

	# Point Nginx at your uploaded files
    location /media/ {
		Autoindex if you want
        autoindex on;
        # The location of your uploaded files
        alias /var/www/yourvirtualenv/yourproject/media/;
		# Set up aching for your uploaded files
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        proxy_ignore_headers "Set-Cookie";
    }


	location / {
        # Try your static files first, then redirect to Gunicorn
        try_files $uri @proxy_to_app;
    }

	# Pass off requests to Gunicorn
    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass   http://njc-gunicorn;
    }

	# Caching for HTML, XML, and JSON
    location ~* \.(html?|xml|json)$ {
        expires 1h;
    }

	# Caching for all other static assets
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff2)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        proxy_ignore_headers "Set-Cookie";
    }
}

好吧,这有点多了,而且还可以更多。需要注意的重点是指向 Gunicorn 的 upstream 块和将流量传递到 Gunicorn 的 location 块。其余大部分都是可选的,但您应该以某种形式进行。配置中的注释应该可以帮助您了解具体细节。

保存该文件后,您可以重新启动 Nginx 以使更改生效。

# systemctl restart nginx

Nginx 重新上线后,您的网站应该可以通过您的域访问。

结束语

如果你想深入挖掘,Nginx 还可以做更多的事情。不过,所提供的配置是一个很好的起点,并且是您可以实际使用的。如果您习惯了 Apache 和臃肿的 PHP 应用程序,那么像这样的服务器配置的速度应该会让人感到惊喜。

©2015-2025 Norria Group support@norria.com