Nginx + uwsgi deployment Django project


Project environment

Server: alicloud ECS; System version: CentOS 8 0 Nginx version: 0.14.1; Uwsgi version: 2.0.18; Python version: 3.6.0.

Project dependencies and versions are as follows:


Log in to the alicloud console and open the port

  1. Configure ports for http80 and Django services
  2. Configure uwsgi to start Django service
  • Create in the project root directoryuwsgi.iniDocuments,vi uwsgi.iniEnter the following:
#Use when using nginx connection
#Directly as a web server, use Python manage py runserver ip:port
#Project directory
#WSGI The directory of the PY file, relative to the project directory
#Specifies the number of worker processes to start
#Specifies the number of threads in the worker process
#Specifies that there is a main process in these processes
#Save the PID of the main process after startup
#Set uwsgi background operation, uwsgi Log save log information
#Set the maximum number of bytes of the log file
log-maxsize = 100000
#Set the maximum number of requests per process
max-requests = 1000
#Set the path of the virtual environment

Also refer toDjango official documentsNginx Chinese documentCustom configuration.

  • Uwsgi command
Start service: uwsgi -- ini uwsgi ini
Stop service: uwsgi -- stop uwsgi pid

Configure nginx to start the static file service

  • get into/etc/nginx/conf.dFolder, create your own project profilemyproject.conf, enter the configuration below.
server {
    listen 80;
        server_name ip;
        charset utf-8;
        client_max_body_size 75M;
        location /static {
        alias /root/project/myblog/static;

    location /media {
        alias /root/project/myblog/media;

    location / {
        include /etc/nginx/uwsgi_params;

  • Restart the nginx serviceservice nginx restart

Problems encountered and Solutions

Problem 1: nginx static resource file cannot be accessed, 403 Forbidden error
Solution: innginx.confConfiguration file header plususer root

user root;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/;
events {
    worker\_connections 1024;
... ...

If the background site style cannot be displayed, it is because the static file collection operation is not carried out. You can enter it at the terminalpython collectstaticsolve. The premise is Xu Zaisettings.pyFile configuration is goodSTATIC_ROOT = os.path.join(BASE_DIR, "static")

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]