Home > Article > Backend Development > Solve all problems of deploying Django with nginx+uwsgi (summary)_nginx
This article mainly introduces all the problems of deploying Django with nginx+uwsgi (summary). I think it is quite good. Now I will share it with you and give you a reference. Let’s take a look together
Recently, the small project I wrote during the summer vacation has been completed. I thought about putting it on my own cloud server. I originally thought that I just need to open the port and run python3 manager runserver 0.0.0.0:80 and it will be done. , and finally I learned that this only applies to Django's development mode and only supports single-user access. In this case, a web server is required for deployment. I used nginx
nginx?
Why nginx?
First of all, I think it is small, very lightweight, easy to use, not as complicated as apache, and online It is recommended to use nginx to deploy Django.
Installation
Skip it directly here, let’s talk about it. Linux users recommend you to install from source code, because the command installation may pretend to be a Taobao II. For the newly developed nginx, I personally recommend using the original version.
uwsgi
Why do you need this?
Simply put, nginx is a reverse proxy server, it can do What's going on? To listen to a port, such as 80, you can configure a reverse proxy port, such as 8000. In this way, all external users' access to port 80 is actually requesting data from port 8000, but the user is not actually communicating with port 8000. , but passed 80 this bridge. At present, I only think that this can hide my real port. If you have any suggestions, please leave a message.
In this case, it can actually only be accessed by a single user, so we need a tool that can be accessed by multiple users concurrently, then it is uwsgi.
how to install?
pip install uwsgi
Configuration file
Let me take a look at it first The file status of my project:
FlyCold ├── FlyCold │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py ├── SchoolBuy │ ├── admin.py │ ├── forms.py │ ├── __init__.py │ ├── models.py │ ├── urls.py │ └── views.py └── templates
Explanation below, this is a simplified directory tree, the created project is named FlyCold, the generated FlyCold subdirectory and SchoolBuy subdirectory. My main code is in SchoolBuy, setting.py is in the Flycold subdirectory, and manager.py is in the Flycold root directory.
After installation, create a configuration file with the following content
# myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8080 #真实服务的端口 # Django项目根目录 (绝对路径) chdir = /home/lyt/FlyCold # wsgi.py文件在项目中的位置 module = FlyCold.wsgi # process-related settings # master master = true # 运行的进程数 processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
This .ini file can be placed anywhere, when starting uwsgi --ini ***.ini
Configure nginx
Find nginx.conf and write the following content
server { #这里是访问时用到的端口 listen 80; server_name localhost; charset UTF-8; #这块存让日志文件 access_log /var/log/nginx/SchoolBuy_access.log; error_log /var/log/nginx/SchoolBuy_error.log; client_max_body_size 75M; location / { include uwsgi_params; #同uwsgi内容 uwsgi_pass 127.0.0.1:8001; #链接超时时间 uwsgi_read_timeout 30; } }
In this way, restart your nginx, access port 80, and you will see the effect.
Still have questions?
You may have discovered that the static resources on your web page cannot be accessed! ! For example, the admin page will be very simple. This is because when nginx+uwsgi+Django, nginx cannot be used as a proxy for Django's processing of static resources (maybe). In short, Django should not be allowed to do this kind of thing, because nginx is more capable of handling static resources. For static resources, let nginx handle it.
Generally speaking, you will have two types of static resources: links starting with /media/ and links starting with /static/. Static is used to process some website original images, videos, js, and css files. Django itself supports this kind of link. So how to turn off Django to process files starting with /static/? It is very simple. Change the DEBUG value to False in setting.py. At this time, Django will not process the /static/ file.
What about /media/? Generally speaking, we will save the pictures uploaded by users, use /media/ when displaying them on the web page, and set
MEDIA_URL = '/media/' #访问的前缀链接 MEDIA_ROOT = os.path.join(BASE_DIR, '../media') #存放文件的具体位置
in setting.py Then add
from django.conf import settings from django.conf.urls.static import static if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in url.py. This means that when DEBUG=True, the /media/ file will be parsed, and the file storage location is the second parameters.
In this way, when deploying to a production environment, you only need to change DEBUG to False, and Django will not process static and media.
Collect static files
Django has a tool that can collect all static files used in the application to facilitate nginx parsing. Specific:
Set STATIC_ROOT = os.path.join(BASE_DIR, '../collectedstatic')
In this way, all static files collected will be into the directory above. How to run this tool? python3 manager.py collectstatic
Configure nginx to parse static files
Similarly, nginx.conf
First, Add the user root
statement at the top of the file to let the root user run nginx, otherwise access to static files may prompt that there is no permission
Secondly, before the configuration file location / mentioned above Add the following content
location /static/ { autoindex on; alias /root/SchoolBuyWeb/collectedstatic/; } location /media/ { autoindex on; alias /root/SchoolBuyWeb/media/; }
Pay attention to the alias and then correspond to the directory you set!
Restart nginx, now it’s ok~~
The above is the detailed content of Solve all problems of deploying Django with nginx+uwsgi (summary)_nginx. For more information, please follow other related articles on the PHP Chinese website!