Skip to content

Instantly share code, notes, and snippets.

@maliubiao
Last active December 29, 2015 09:58
Show Gist options
  • Select an option

  • Save maliubiao/7653464 to your computer and use it in GitHub Desktop.

Select an option

Save maliubiao/7653464 to your computer and use it in GitHub Desktop.
uwsgi-problem
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 20240;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
uwsgi_pass 127.0.0.1:3031;
include uwsgi_params;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
<uwsgi>
<socket>127.0.0.1:3031</socket>
<module>wsgi_hello</module>
<callable>myapp</callable>
<master>1</master>
<daemonize>uwsgi.log</daemonize>
<workers>4</workers>
<touch-reload>reload</touch-reload>
</uwsgi>
import traceback
from mmap import mmap
import re
import cStringIO
_match = re.match
_content_buffer = cStringIO.StringIO()
pages = {
404: "404.html"
}
pages_mmap = {}
pages_path = "pages/"
default_pages = {
404: "404.html"
}
default_pages_mmap = {}
statics = {
}
statics_mmap = {}
static_path = "static/"
status = ""
headers = {}
default_headers = {"Content-Type": "text/html"}
status_mesage = {
100: "100 Continue",
101: "101 Switching Protocols",
102: "102 Processing",
200: "200 OK",
201: "201 Created",
202: "202 Accepted",
203: "203 Non-Authoritative Information",
204: "204 No Content",
205: "205 Reset Content",
206: "206 Partial Content",
300: "300 Multiple Choices",
301: "301 Moved Permanently",
302: "302 Found",
303: "303 See Other",
304: "304 Not Modified",
305: "305 Use Proxy",
306: "306 Switch Proxy",
307: "307 Temporary Redirect",
400: "400 Bad Request",
401: "401 Unauthorized",
402: "402 Payment Required",
403: "403 Forbidden",
404: "404 Not Found",
405: "405 Method Not Allowed",
406: "406 Not Acceptable",
407: "407 Proxy Authentication Required",
408: "408 Request Timeout",
409: "409 Conflict",
410: "410 Gone",
411: "411 Length Required",
412: "412 Precondition Failed",
413: "413 Request Entity Too Large",
414: "414 Request-URI Too Long",
415: "415 Unsupportd Media Type",
416: "416 Requested Range Not Satisfiable",
417: "417 Expectation Failed",
418: "418 I'm a teapot",
421: "421 There are too many connections from your Internet Address",
422: "422 Unprocessable Entity",
423: "423 Locked",
424: "424 Failed Dependency",
425: "425 Unordered Collection",
426: "426 Upgrade Required",
449: "449 Retry With",
500: "500 Internal Server Error",
501: "501 Not implemented",
502: "502 Bad Gateway",
503: "503 Service Unavailable",
504: "504 Gateway Timeout",
505: "505 HTTP Version Not Supported",
506: "506 Variant Also Negotiates",
507: "507 Insufficient Storage",
509: "509 Bandwidth Limit Exceeded",
510: "Not Extended"
}
#support static files
def register_dir(dir):
pass
def set_content_type():
pass
def get_content_type():
pass
def set_cookie():
pass
def get_cookie():
pass
def setup_headers(headers):
return headers.items()
def root_handler(request):
global status
_content_buffer.write(str(request))
status = status_mesage[200]
#be careful, please use very strict rules.
urls = {
r"/$": root_handler
}
#share pages with workers
def init_mmap(ent, mm, path):
for page in ent:
m = 0
try:
f = open(path + ent[page], "r+b")
m = mmap(f.fileno(), 0)
f.close()
except Exception, e:
print(e)
mm[page] = m
def init_pages():
init_mmap(pages, pages_mmap, pages_path)
init_mmap(default_pages, default_pages_mmap, pages_path)
init_mmap(statics, statics_mmap, static_path)
init_pages()
def myapp(environ, start_response):
global status
global headers
global _content_buffer
match = False
status = ""
_content_buffer.truncate(0)
target_url = environ["PATH_INFO"]
for url_pattern in urls:
if _match(url_pattern, target_url):
match = True
urls[url_pattern](environ)
break
if not match:
#_content_buffer.truncate(0)
page_404 = ""
if 404 in pages_mmap:
page_404 = pages_mmap[404]
else:
page_404 = default_pages_mmap[404]
_content_buffer.write(page_404.read(51))
page_404.seek(0)
status = status_mesage[404]
if not status:
status = status_mesage[404]
if not headers:
headers = default_headers
final_header = setup_headers(headers)
start_response(status, final_header)
return _content_buffer.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment