什么是web服务器
连接用户浏览器与python服务器端程序的中间节点,目前主流的Web服务器包括Nginx、Apache、IIS、lighthttpd等,python服务器端程序在linux平台使用最广泛的就是Nginx。
WSGI
WSGI是将python服务端程序连接到web服务器的通用协议,独立的WSGI程序例如uWSGI和apache的mod_wdgi。
Nginx
Linux+Nginx+uwsgi配置
linux系统下安装Nginx
sudo apt-get install Nginx
安装文件路径
- 程序文件: 放在/usr/sbin/nginx 目录下
 - 全局配置文件: /etc/nginx/nginx.conf
 - 访问日志文件: /var/log/nginx/access.log
 - 错误日志文件: /var/log/nginx/error.log
 - 站点配置文件: /etc/nginx/sites-enabled/default
 
启动nginx服务器
sudo service nginx start
停止nginx服务器
sudo service nginx stop
重启nginx服务器
sudo server nginx restart
查看nginx服务状态
sudo service nginx status
Nginx配置文件
nginx安装后以默认方式启动,如果需要调试运行参数可用过全局配置文件(Nginx.conf)和站点配置文件(sites-enabled/)进行设置。
*全局配置文件(/etc/nginx/nginx.conf)参数解析:
user www-data; # 定义nginx运行的用户worker_processes auto; # nginx进程数应设置与系统CPU数量相等的数值pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events {worker_connections 768; # 每个Nginx进程可打开的最大文件数# multi_accept on; # 在nginx接到一个新连接通知后调用accept()来接受尽量多的连接}http {### Basic Settings##sendfile on; # 是否允许文件上传tcp_nopush on; # 防止网络阻塞tcp_nodelay on; # 防止网络阻塞keepalive_timeout 65; # 允许客户端最长连接最大秒数types_hash_max_size 2048; # nginx散列表大小, 本值越大,占用的内容空间越大,路由速度越快# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;default_type application/octet-stream;### SSL Settings##ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers on;### Logging Settings##access_log /var/log/nginx/access.log; # 访问日志文件路径名error_log /var/log/nginx/error.log; # 错误日志文件路径名### Gzip Settings##gzip on;# gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;### Virtual Host Configs### include 命名加载站点配置文件include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}
使用nginx的优势
- 安全问题:使程序不直接暴露给浏览器访问,uwsgi是内网接口,通过nginx开放某个端口
 - 负载均衡:nginx可代理多台uwsgi完成负载均衡
 - 静态文件处理效率高
 
默认站点配置文件解析(/etc/nginx/sites-enabled/default)
server {listen 80 default_server;listen [::]:80 default_server;# SSL configuration## listen 443 ssl default_server;# listen [::]:443 ssl default_server;## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332## Read up on ssl_ciphers to ensure a secure configuration.# See: https://bugs.debian.org/765782## Self signed certs generated by the ssl-cert package# Don't use them in a production server!## include snippets/snakeoil.conf;root /var/www/html;# Add index.php to the list if you are using PHPindex index.html index.htm index.nginx-debian.html;server_name _;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri/ =404;}# pass PHP scripts to FastCGI server##location ~ \.php$ {# include snippets/fastcgi-php.conf;## # With php-fpm (or other unix sockets):# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;# # With php-cgi (or other tcp sockets):# fastcgi_pass 127.0.0.1:9000;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# Virtual Host configuration for example.com## You can move that to a different file under sites-available/ and symlink that# to sites-enabled/ to enable it.##server {# 配置站点监听的端口# listen 80;# listen [::]:80;## server_name example.com; # 站点监听的IP地址,loacalhost只能用本机访问,一般需要改为真实IP## root /var/www/example.com; # 配置HTTP根页面目录# index index.html; # 配置HTTP根目录中的默认页面## location / {# try_files $uri $uri/ =404;# }#}
安装uWSGI及配置
安装
pip install uwsgi
通过命名运行uWSGI
启动时用—http参数执行了监听端口,用—wsgi-file指定了服务器端的程序名
uwsgi --http :9090 --wsgi-file test.py
uWSGI可通过配置文件设置这些参数,即保存在文件 uwsgi.ini中
[uwsgi]http = :9090wsgi-site = test.py
启动时指定配置文件
uwsgi uwsgi.ini
访问网站的一个链接,背后发生了什么?????
首先访问一个url,会通过url访问到nginx,nginx接收到浏览器发送的http请求,将请求进行解析,分析url,如果是静态文件请求则直接返回nginx配置的静态文件目录的具体文件。
如果是非静态文件,nginx会将请求转发给uwsgi,uwsgi接收到请求后则处理为wsgi可接收的格式,wsgi根据请求调用应用程序的文件的某个函数或者方法,处理完后再提交给wsgi,打包成uwsgi能接受的格式,转发给nginx,最终nginx将结果返回给浏览器渲染展示。
