By nginx build python-based web environment

 

Preface:

Before beginning to build, let's take a web service workflow under combing, look at the following figure:
Figure
1, the user (PC) launched http request to a web server
2, web server determines whether the user requests a file is a static file is read directly take static file and returned to the user, not the WSGI protocol by requesting threw web frameworks (django) code that handles
3, to see if web framework to start django middleware, if enabled, to request modifications based middleware, if not enabled , the process proceeds to the next step
4, the routing framework web application routes the request to the appropriate file according py request file name url
5, after receiving the request file corresponding py calculated according to the parameters of the user submitted (may be called during a database) and then return the result of calculation and custom header information and status code
after. 6, the returned web frame marked data generic identifier (header information) to the web server
. 7, marked with the web server's web server General identifier (header information) returns to the user
data 8, the user receives the returned

 

Through the above can be seen to interact with django framework based on WSGI protocol and web servers, WSGI protocol, what is it? Let's use the code to illustrate (write a simple pseudo code follows the WSGI protocol web server software and django program.):

WSGI server program:
class WSGI_WEB (Object):
DEF __init __ (Self):
# 1. Create a socket
self.tcp_server_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
self.tcp_server_socket.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 2. bind
self.tcp_server_socket.bind (( "", 7890))
# 3. turns listening socket
self.tcp_server_socket.listen (128)

def set_response_header(self, status, headers):
self.status = status
self.headers = [("server", "WSGI_simple_web v1.0")]
self.headers += headers

RUN DEF (Self):
Since the software of, client_addr = self.tcp_server_socket.accept ()
the env = new_socket.recv (1024)
body = file application (the env, set_response_header) # is the env browser to the web server receives the transmitted packet; set_response_header of one way to address web servers, django goal is to help generate web server http header (not to the web server in the form of return); in addition django application there is a core mission also calls here, is to get return data the body!
= + self.headers self.status header
Response = header + body
new_socket.send (response.encode ( "UTF-. 8"))


django的app程序:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]

 


Problem:
using a simple web server performance django provided poor in a production environment, generally used only for debugging. Powerful nginx does not support WSGI, then how to do it?
Map


Program:
between nginx and add a layer of support python WSGI application protocol enabled web server. After the static files by nginx processing, dynamic file threw WSGI server, then threw another web framework WSGI server processing. Optimal support WSGI protocol web server is uWSGI.
Map


UWSGI server and a method to build the linkage nginx detail below with:
1, the installation uWSGI (WSGI support WEB server):
Under centos python3.6 uWSGI mounting method:
yum the install PCRE * -Y-GCC-devel devel OpenSSL python36- devel.x86_64
pip3.6 install uwsgi
2, open uWSGI service
Method 1:
uwsgi --http 192.168.31.123:80 --file Teacher / wsgi.py---static the Map = / = static static
--http listening IP port
- -file project wsgi.py file path
--static-map static file path

Note: when executing this command: In this project must be in the directory ~

Second way (using a configuration file):
vi uwsgi.ini:
[uwsgi]
# monitor port (required when using nginx reverse proxy mode)
HTTP = 0.0.0.0:8888

# Project directory
chdir = / opt / test / test1 /

# Enable user name and user group uwsgi of
uid = root
gid = root

Application # specified item (I guess here "test1.wsgi" after stitching above the project directory, it will project wsgi.py file servers and associated uWSGI up)
Module = test1.wsgi: the Application

# Sock specified file path (required when using nginx local mode)
Socket = / opt / Test / Script / uwsgi.sock

# Enable master process
master = true

# Process the number of
Workers = 5
the PidFile = / opt / the Test / Script / uwsgi.pid

# Automatically remove unix Socket and pid file when the service stops when the
vacuum = true

# Serialization acceptable content, if possible
thunder-lock = true

# Enable thread
enable-threads = true

# Set break from time
harakiri = 30

A buffer #
post-buffering = 4096

# Set the log directory
daemonize = / opt / test / script / uwsgi.log

# Set how often loaded once project code
py-autoreload = 1

Execution profile (Note: This is an account of the implementation of what later infiltrated get to is what accounts so this step is not to avoid execution as root..):
Uwsgi --ini uwsgi.ini

Eggs:
Restart uWSGI process: after uwsgi --reload uwsgi.pid # ===> code does not load immediately change uWSGI process, then you can restart it uWSGI process to make it take effect. . . Is not feeling a little pit, all right, you can set in the configuration file py-autoreload in
close uWSGI process: uwsgi --stop uwsgi.pid

3, arranged nginx
embodiment a (reverse proxy mode):
upstream to uwsgi {
Server 10.10.10.29:8888;
}

server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

LOCATION / {
proxy_pass HTTP: // to uwsgi; # uWSGI through the reverse proxy server and associated
}
}


Second way (local mode):
Server {
the listen 8080;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

LOCATION / {
the include uwsgi_params; # nginx specified communication server and uWSGI
uwsgi_connect_timeout 30;
uwsgi_pass UNIX: /opt/test/script/uwsgi.sock; # sock by uWSGI server and file association! Because nginx will .sock to read the file, you need to turn off selinux job! ! !
}
}


4. At this time django's admin access management background, static resources retrieval will fail. Then you can unify all static resources to collect the item to the next folder, then go to the transfer of the nginx unified, truly static and dynamic separation (move to uWSGI, quiet retrieved directly from the nginx):
In the settings.py added:
TATIC_ROOT = os.path.join (base_dir, 'static_file')

execute the following command (collect all static files to the project 'static_file' directory):
python3.6 manage.py collectstatic --noinput
this time will be in the project directory generating a 'static_file' folder containing the file app admin all involved static

configuration nginx static file path (if not in the same uWSGI nginx and may use a reverse proxy server approach to the transfer of static files):
LOCATION / static / {
Alias / opt / Test / test1 / static_file /;
}

Guess you like

Origin www.cnblogs.com/baihualin/p/12133856.html