什么是web服务器

连接用户浏览器与python服务器端程序的中间节点,目前主流的Web服务器包括Nginx、Apache、IIS、lighthttpd等,python服务器端程序在linux平台使用最广泛的就是Nginx。

WSGI

WSGI是将python服务端程序连接到web服务器的通用协议,独立的WSGI程序例如uWSGI和apache的mod_wdgi。

Nginx

Nginx是一个高性能HTTP和反向代理服务器

Linux+Nginx+uwsgi配置

linux系统下安装Nginx

  1. 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服务器

  1. sudo service nginx start

停止nginx服务器

  1. sudo service nginx stop

重启nginx服务器

  1. sudo server nginx restart

查看nginx服务状态

  1. sudo service nginx status

Nginx配置文件

nginx安装后以默认方式启动,如果需要调试运行参数可用过全局配置文件(Nginx.conf)和站点配置文件(sites-enabled/)进行设置。
*全局配置文件(/etc/nginx/nginx.conf)参数解析

  1. user www-data; # 定义nginx运行的用户
  2. worker_processes auto; # nginx进程数应设置与系统CPU数量相等的数值
  3. pid /run/nginx.pid;
  4. include /etc/nginx/modules-enabled/*.conf;
  5. events {
  6. worker_connections 768; # 每个Nginx进程可打开的最大文件数
  7. # multi_accept on; # 在nginx接到一个新连接通知后调用accept()来接受尽量多的连接
  8. }
  9. http {
  10. ##
  11. # Basic Settings
  12. ##
  13. sendfile on; # 是否允许文件上传
  14. tcp_nopush on; # 防止网络阻塞
  15. tcp_nodelay on; # 防止网络阻塞
  16. keepalive_timeout 65; # 允许客户端最长连接最大秒数
  17. types_hash_max_size 2048; # nginx散列表大小, 本值越大,占用的内容空间越大,路由速度越快
  18. # server_tokens off;
  19. # server_names_hash_bucket_size 64;
  20. # server_name_in_redirect off;
  21. include /etc/nginx/mime.types;
  22. default_type application/octet-stream;
  23. ##
  24. # SSL Settings
  25. ##
  26. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  27. ssl_prefer_server_ciphers on;
  28. ##
  29. # Logging Settings
  30. ##
  31. access_log /var/log/nginx/access.log; # 访问日志文件路径名
  32. error_log /var/log/nginx/error.log; # 错误日志文件路径名
  33. ##
  34. # Gzip Settings
  35. ##
  36. gzip on;
  37. # gzip_vary on;
  38. # gzip_proxied any;
  39. # gzip_comp_level 6;
  40. # gzip_buffers 16 8k;
  41. # gzip_http_version 1.1;
  42. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  43. ##
  44. # Virtual Host Configs
  45. ##
  46. # include 命名加载站点配置文件
  47. include /etc/nginx/conf.d/*.conf;
  48. include /etc/nginx/sites-enabled/*;
  49. }

使用nginx的优势

  • 安全问题:使程序不直接暴露给浏览器访问,uwsgi是内网接口,通过nginx开放某个端口
  • 负载均衡:nginx可代理多台uwsgi完成负载均衡
  • 静态文件处理效率高

默认站点配置文件解析(/etc/nginx/sites-enabled/default)

  1. server {
  2. listen 80 default_server;
  3. listen [::]:80 default_server;
  4. # SSL configuration
  5. #
  6. # listen 443 ssl default_server;
  7. # listen [::]:443 ssl default_server;
  8. #
  9. # Note: You should disable gzip for SSL traffic.
  10. # See: https://bugs.debian.org/773332
  11. #
  12. # Read up on ssl_ciphers to ensure a secure configuration.
  13. # See: https://bugs.debian.org/765782
  14. #
  15. # Self signed certs generated by the ssl-cert package
  16. # Don't use them in a production server!
  17. #
  18. # include snippets/snakeoil.conf;
  19. root /var/www/html;
  20. # Add index.php to the list if you are using PHP
  21. index index.html index.htm index.nginx-debian.html;
  22. server_name _;
  23. location / {
  24. # First attempt to serve request as file, then
  25. # as directory, then fall back to displaying a 404.
  26. try_files $uri $uri/ =404;
  27. }
  28. # pass PHP scripts to FastCGI server
  29. #
  30. #location ~ \.php$ {
  31. # include snippets/fastcgi-php.conf;
  32. #
  33. # # With php-fpm (or other unix sockets):
  34. # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  35. # # With php-cgi (or other tcp sockets):
  36. # fastcgi_pass 127.0.0.1:9000;
  37. #}
  38. # deny access to .htaccess files, if Apache's document root
  39. # concurs with nginx's one
  40. #
  41. #location ~ /\.ht {
  42. # deny all;
  43. #}
  44. }
  45. # Virtual Host configuration for example.com
  46. #
  47. # You can move that to a different file under sites-available/ and symlink that
  48. # to sites-enabled/ to enable it.
  49. #
  50. #server {
  51. # 配置站点监听的端口
  52. # listen 80;
  53. # listen [::]:80;
  54. #
  55. # server_name example.com; # 站点监听的IP地址,loacalhost只能用本机访问,一般需要改为真实IP
  56. #
  57. # root /var/www/example.com; # 配置HTTP根页面目录
  58. # index index.html; # 配置HTTP根目录中的默认页面
  59. #
  60. # location / {
  61. # try_files $uri $uri/ =404;
  62. # }
  63. #}

安装uWSGI及配置

安装

  1. pip install uwsgi

通过命名运行uWSGI
启动时用—http参数执行了监听端口,用—wsgi-file指定了服务器端的程序名

  1. uwsgi --http :9090 --wsgi-file test.py

uWSGI可通过配置文件设置这些参数,即保存在文件 uwsgi.ini中

  1. [uwsgi]
  2. http = :9090
  3. wsgi-site = test.py

启动时指定配置文件

  1. uwsgi uwsgi.ini

访问网站的一个链接,背后发生了什么?????
首先访问一个url,会通过url访问到nginx,nginx接收到浏览器发送的http请求,将请求进行解析,分析url,如果是静态文件请求则直接返回nginx配置的静态文件目录的具体文件。
如果是非静态文件,nginx会将请求转发给uwsgi,uwsgi接收到请求后则处理为wsgi可接收的格式,wsgi根据请求调用应用程序的文件的某个函数或者方法,处理完后再提交给wsgi,打包成uwsgi能接受的格式,转发给nginx,最终nginx将结果返回给浏览器渲染展示。