You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次使用Nginx对接Django:uWSGI配置后切换Nginx的相关问题

Setting Up Django with Nginx & uWSGI (Emperor Mode)

Hey there! Since you've already got the basics down—knowing you need uWSGI to bridge Django and Nginx, and even started with Emperor mode—let's get you sorted with the right configs and next steps.

1. Finalize Your uWSGI Site Config

You mentioned creating a file in /etc/uwsgi/sites/—let's assume it's named mysite.ini (adjust to your actual project name if needed). Here's a production-ready config to use:

[uwsgi]
# Core project paths
project = mysite
base = /root

# Socket for secure communication with Nginx (replaces direct HTTP port)
socket = %(base)/%(project)/%(project).sock
# Set permissions so Nginx can access the socket
chmod-socket = 664
# Clean up the socket when uWSGI stops running
vacuum = true

# Django-specific settings
chdir = %(base)/%(project)
home = %(base)/Env/firstsite  # Path to your virtual environment
module = %(project).wsgi:application

# Process management (tweak based on your server's CPU/RAM)
master = true
processes = 4
threads = 2

# Logging for debugging
logto = /var/log/uwsgi/%(project).log

Key notes here:

  • The socket replaces the direct HTTP port you used earlier—this is the secure, recommended way for Nginx to talk to uWSGI.
  • chmod-socket ensures Nginx has permission to interact with the socket (a common pitfall!).
  • home points to your virtual environment so uWSGI uses the correct Python packages for your Django app.

2. Configure Nginx to Proxy Requests to uWSGI

Next, set up an Nginx server block to handle HTTP traffic and pass app requests to uWSGI. First, create a config file at /etc/nginx/sites-available/mysite:

server {
    listen 80;
    server_name your-domain-or-ip;  # Replace with your actual domain/server IP

    # Serve static files directly with Nginx (far more efficient than Django)
    location /static/ {
        root /root/mysite;  # Path to your Django project root
        expires 30d;  # Cache static files for better performance
    }

    # Proxy all other requests to uWSGI
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/root/mysite/mysite.sock;  # Match the socket path from your uWSGI config
    }
}

Enable this config by creating a symbolic link to the sites-enabled directory:

ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/

3. Post-Configuration Steps

Let's get everything up and running smoothly:

  • Create uWSGI log directory: If it doesn't exist, run mkdir -p /var/log/uwsgi and set proper ownership (e.g., chown www-data:www-data /var/log/uwsgi if uWSGI runs as the www-data user).
  • Restart uWSGI Emperor: Use your system's service manager—for systemd, that's systemctl restart uwsgi.
  • Test Nginx config: Always check for syntax errors first with nginx -t. If it says "test is successful", restart Nginx with systemctl restart nginx.
  • Collect static files: Don't forget to run python manage.py collectstatic in your Django project root—this gathers all static files into the /static/ directory Nginx will serve.

Why This Setup Is Better Than Direct HTTP

Your initial uwsgi --http command works for testing, but using Nginx as the front-end adds critical benefits:

  • Nginx handles SSL/TLS (you can add Let's Encrypt certificates later for HTTPS).
  • It filters malicious requests before they reach your Django app.
  • It efficiently serves static files, reducing load on uWSGI and your Django application.

If you run into issues (like 502 Bad Gateway errors), check the uWSGI and Nginx logs—they'll usually point you to permission problems or misconfigured paths.

内容的提问来源于stack exchange,提问作者ShellRox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:12:20