Theme NexT works best with JavaScript enabled
Nginx的location配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 使用Nginx location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个location的优先级该如何区分。 location 的匹配顺序其实是“先匹配普通,再匹配正则”。 nginx 是“先匹配正则 location 再匹配普通 location ”,其实这是一个误区, nginx 其实 是“先匹配普通 location ,再匹配正则 location ”。 但是普通 location 的匹配结果又分两种: 一种是“严格精确匹配”,官方英文说法是“ 准确匹配 ”; 一种是“最大前缀匹配”,官方英文说法是“ 字面值字符串匹配开始部分 查询的-将使用最具体的匹配 ”。 location 匹配的优先级(与location在配置文件中的顺序无关) = 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。 普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有 没有正则表达式匹配和更长的匹配。 ^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。最后匹配 带有"~" 和"~*" 的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有 正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用
location语法 1 2 3 4 Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: — Context: server, location
location匹配符号
匹配符
匹配规则
优先级
=
精确匹配
1
^~
以某个字符串开头,一般用来匹配目录
2
~
区分大小写的正则匹配
3
~*
不区分大小写的正则匹配
4
/
通用匹配,任何请求都会匹配到
5
优先级认证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 示例: [root@web01 ~] server { listen 80 ; server_name 192.168.15.8 ; location ~* /python { default_type text/html; return 200 "Location ~*" ; } location ~ /Python { default_type text/html; return 200 "Location ~" ; } location ^~ /python { default_type text/html; return 200 "Location ^~" ; } location = /python { default_type text/html; return 200 "Location =" ; } } 案例: [root@web01 ~] server { listen 80 ; server_name 192.168.15.8 ; location / { root /usr/share/nginx/html; index index.html; } location ~* \.(jpg|gif|png|jpeg) { deny all; } location =/status { auth_basic "closed site" ; auth_basic_user_file /etc/nginx/auth_basic; stub_status; } }
location应用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 location / { ... } location ~ \.py$ { ... } location ~ \.html$ { ... } location ~* .*\.(jpg|gif|png|js|css)$ { ... } location ~* "\.(sql|bak|tgz|tar.gz|.git)$" { ... }
uWSGI
1 2 3 4 5 6 7 8 WebApp采用 Python 的web框架 Django 开发 因为Nginx不支持WSGI协议,无法直接调用 Python 开发的WebApp。所以需要借助uWSGI, 在 Nginx + uWSGI + Django 的框架里,nginx代理+WebServer,uWSGI是WSGI server, Django是webApp。Nginx接收用户请求,并判定哪些转发到uWSGI,uWSGI再去调用pyWebApp。 由于apache、nginx等,它们自己都没有解析动态语言如php的功能,而是分派给其他模块来做,比如apache就 可以说是内置模块,让人感觉apache就支持php一样
这里一定要注意 WSGI , uwsgi , uWSGI 这三个概念的区分:
WSGI : 是一种接口标准协议/规范,实现了python web程序与web服务器之间交互的通用性。利用它django等python web开发框架就可以部署不同的web server上了,目的是制定标准,以保证不同Web服务器可以和不同的Python程序之间相互通信
uwsgi : 是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
uWSGI :是基于自有uwsgi协议、WSGI协议和http服务协议的web网关或服务器。负责响应 python 的web请求。
参考文档
uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型,每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。uwsgi是一种线路协议而不是通信协议,在此常用于在uwsgi服务器与其他网络服务器的数据通信。uwsgi协议是一个uwsgi服务器自有的协议,它用于定义传输信息的类型。
uwsgi实现了WSGI的所有接口,是一个快速、自我修复、开发人员和系统管理员友好的服务器。uwsgi代码完全 用C编写,效率高、性能稳定。
LNMP架构
LNMP工作流程
首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
1.静态请求:请求的内容是静态文件就是静态请求
1)静态文件:文件上传到服务器,永远不会改变的文件就是静态文件
2)html 就是一个标准的静态文件
2.动态请求:请求的内容是动态的就是动态请求
1)不是真实存在服务器上的内容,是通过数据库或者其他服务拼凑成的数据
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过uwsgi协议转交给后端的Python程序处理
LNMP访问流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1、客户端(PC)向web服务器发起http请求服务资源 2、nginx作为直接对外的服务接口,接收到客户端发送过来的http请求,会解包、分析,如果是静态文件 请求则根据nginx配置的静态文件目录,并返回请求的资源给客户端,不是则通过nginx就通过配置文件, 将请求传递给uWSGI;uWSGI 将接收到的包进行处理,并转发给WSGI,WSGI协议将请求丢给web框架 (django)代码处理 3、看web框架是否启动django中间件,如果启用,则依据中间件对请求进行修改,如果不启用,则进入下一步 4、web框架中的路由程序将根据请求中的url文件名将请求路由至相应py文件 5、相应py文件收到请求后根据用户提交的参数进行计算(期间可能会调用数据库),然后返回计算后的结果和自 定义头部信息以及状态码返回 6、web框架将返回的数据打上通用标识符(头部信息)后返回给WSGI 7、WSGI将返回数据进行打包,转发给uWSGI,uWSGI接收后转发给nginx,nginx最终将返回值返回给客户端(PC)。 8、客户端收到返回的数据
LNMP搭建
服务器
IP
lb01
192.168.15.5
db01
192.168.15.7
web01
192.168.15.8
uwsgi服务部署 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 1、创建用户 [root@web01 ~] [root@web01 opt] [root@web01 opt] 2、安装python依赖软件 [root@web01 opt] 3、安装Django和uwsgi [root@web01 opt] [root@web01 opt] 4、创建项目:linux [root@web01 opt] [root@web01 opt] [root@web01 linux] [root@web01 linux] ALLOWED_HOSTS = ['*' ] DATABASES = {} [root@web01 linux] 5、编辑项目配置文件 [root@web01 linux] [uwsgi] socket = :8000 chdir = /opt/linuxwsgi-file = linux/wsgi.py module = linux.wsgi master = true processes = 4 vacuum = true 6、后台启动uwsgi(不建议用root运行,所以指定给其他用户) [root@web01 linux] -d : 以守护进程方式运行 --ini : 指定配置文件路径 --uid : 指定uid [root@web01 linux] 7、编辑Nginx配置文件 [root@web01 linux] server { listen 80; server_name py.test.com; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 2; uwsgi_param UWSGI_SCRIPT linux.wsgi; uwsgi_param UWSGI_CHDIR /opt/linux; index index.html index.htm; client_max_body_size 35m; } } 8、重启Nginx配置 [root@web01 linux]
BBS项目部署 部署数据库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1、安装数据库 [root@db01 ~] 2、启动数据库 [root@db01 ~] 3、远程连接MySQL数据 MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root' @'%' IDENTIFIED BY '123456' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> CREATE DATABASE `bbs` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; Query OK, 1 row affected (0.00 sec)
部署BBS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1、上传代码 [root@db01 ~] [root@db01 ~] 2、数据库迁移 [root@db01 ~] [root@web01 migrations] /opt/bbs/app01/migrations [root@web01 migrations] [root@web01 migrations] [root@web01 migrations] [root@web01 bbs] /opt/bbs [root@web01 bbs] [root@web01 bbs] [root@web01 bbs] [root@web01 bbs] ALLOWED_HOSTS = ['*' ] DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.mysql' , 'NAME' : 'bbs' , 'USER' : 'root' , 'PASSWORD' : '123456' , 'HOST' : '192.168.15.7' , 'PORT' : 3306, 'CHARSET' : 'utf8' } } [root@web01 bbs] [root@web01 bbs] 3、配置并启动uwsgi [root@web01 bbs] [root@web01 bbs] [uwsgi] socket = :8002 chdir = /opt/bbswsgi-file = bbs/wsgi.py module = bbs.wsgi master = true processes = 4 vacuum = true [root@web01 bbs] 4、配置Nginx [root@web01 bbs] server { listen 80; server_name bbs.test.com; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8002; uwsgi_read_timeout 2; uwsgi_param UWSGI_SCRIPT bbs.wsgi; uwsgi_param UWSGI_CHDIR /opt/bbs; index index.html index.htm; client_max_body_size 35m; } location /s { alias /opt/bbs/static; } } [root@web01 bbs] 5、测试访问BBS 浏览器输入 bbs.test.com