Nginx简介

来源:互联网 发布:凯文先生的淘宝店 编辑:程序博客网 时间:2024/06/06 18:02
http://tengine.taobao.org/book/
Nginx从入门到精通。

比如kill -HUP pid,则是告诉nginx,从容地重启nginx,我们一般用这个信号来重启nginx,或重新加载配置,因为是从容地重启,因此服务是不中断的。
nginx在0.8版本之后,引入了一系列命令行参数,来方便我们管理。比如,./nginx -s reload,就是来重启nginx,./nginx -s stop,就是来停止nginx的运行。

Nginx安装与使用:
http://www.cnblogs.com/skynet/p/4146083.html

Nginx教程:
https://my.oschina.net/u/2428225/blog/802549

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。

要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
  • WSGI看过前面小节的同学很清楚了,是一种通信协议。
  • uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
  • 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
在本文中,uwsgi 所扮演的的角色是后端 http 服务器,nginx 扮演的角色是前端 http 服务器,hello.py 是客户端应用程序。 用户从网页浏览器中发出请求,nginx 服务器收到请求后,会通过它的 uwsgi 模块将用户的请求转发给 uwsgi 服务器,uwsgi 服务器处理完毕后将结果返回给 nginx,浏览器将最终的结果展现给用户。


4、 配置nginx下的uwsgi站点 
例如新增以下一个站点uwsgi。 
vi /etc/nginx/conf.d/uwsgi.conf, 内容:

启动Nginx服务 
#service nginx start 
#chkconfig nginx on


5、编写Hello Word!

#vim index.py 

脚本名称和上面nginx虚机配置的uwsgi_param UWSGI_SCRIPT参数要一致

不使用web.py框架的写法:

01#!/usr/bin/python

02import os
03import sys

06def application(environ, start_response):
07status = '200 OK'
08output = 'Hello World!'
09response_headers = [('Content-type''text/plain'),
10('Content-Length', str(len(output)))]
11start_response(status, response_headers)
12return [output]

使用web.py框架的写法:

 
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-  
3
4
import web
5
6
urls = (
7
  '/t', 'test', #测试
8
  '/', 'home'
9
)
10
11
app = web.application(urls, globals())
12
#返回wsgi接口,application 是 wsgi app入口函数
13
application = app.wsgifunc()
14
15
class test:
16
  '测试'    
17
18
  def GET(self):
19
    # 开发测试用
20
    referer = web.ctx.env.get('HTTP_REFERER', 'http://google.com')
21
    client_ip = web.ctx.env.get('REMOTE_ADDR')
22
    host = web.ctx.env.get('host')
23
    fullpath = web.ctx.fullpath
24
    user_agent = web.ctx.env.get('HTTP_USER_AGENT')
25
26
    data = ""
27
    data += 'Client: %s<br/>\n' % client_ip
28
    data += 'User-agent: %s<br/>\n' % user_agent
29
    data += 'FullPath: %s<br/>\n' % fullpath
30
    data += 'Referer: %s<br/>\n' % referer
31
32
    return data
33
34
  def POST(self):
35
    pass
36
37
class home:
38
  '根目录请求的处理'        
39
  def GET(self):
40
    return "Hello Web.py"
41
42
  def POST(self):
43
    return self.GET()
44
45
#定义404错误显示内容  
46
def notfound():  
47
    return web.notfound("Sorry, the page you were looking for was not found.")  
48
49
app.notfound = notfound  
50
if __name__ == "__main__":
51
  app.run()


6、重新载入python脚本

#service uwsgi reload

或者

#python index.py 9092

表示使用index.py脚本在9092端口新开启一个web服务监听

这样你写的hello word就生效了,现在可以在浏览器输入你的ip地址+端口来访问python web内容了

原创粉丝点击