Nginx

Nginx, 发音”engine-X”(安俊-X), 是前10万大网站里第二常用的web服务器second most common web server among the top 100,000 websites。Nginx也常被用作反向代理以处理来自Python WSGI servers 甚至Apache服务器的请求。

Official Nginx logo.

Nginx在Python Web应用中如何部署?

Nginx常被用来作为发布静态文件,比如图像,CSS 和 JavaScript文件。

Nginx也常被配置成反向代理,即把传入的HTTP请求传输给WSGI server。WSGI服务器通过运行Python应用来产生动态内容。当WSGI服务器传输其响应,以HTML,JSON 或 XML 格式,反向代理再把这些内容传递回客户端。

反向代理服务器和WSGI服务器上的请求和相应周期,可以参见下图:

Python web application deployments rely on Nginx either as a web server or reverse proxy for WSGI servers.

一般来说,客户端不知道也不需要知道是一个Python web应用产生响应结果。响应结果可以是由后台系统的任何语言产生,不只是Python。

我该使用Nginx还是Apache?

首先需要说明:它们都是优秀的开源项目,都能很好的处理你的web应用。实际上,许多世界级大公司在它们的web服务器上两者都用。

Nginx的配置文件会相对更容易写。

Nginx安全加强

Nginx正常安装后做默认配置,是安全的基础。然而,第一次设置ciphers和redirects会比较麻烦。可以读读:

Nginx资源

Nginx can be used without Python so there are a massive number of fantastic resources available for installing, configuring and optimizing this web server implementation. The following resources are ones that I collected during my own struggle while learning how to use Nginx after I had used Apache HTTP Server for several years.

  • The Nginx chapter in the Architecture of Open Source Applications book has a great chapter devoted to why Nginx is built to scale a certain way and lessons learned along the development journey.
  • Inside Nginx: How we designed for performance and scale is a blog post from the developers behind Nginx on why they believe their architecture model is more performant and scalable than other approaches used to build web servers.
  • Test-driving web server configuration is a good story for how to iteratively apply configuration changes, such as routing traffic to Piwik for web analytics, reverse proxying to backend application servers and terminately TLS connections appropriately. It is impressive to read a well-written softare development article like this from a government agency, although UK’s Government Digital Service as well as USA’s 18F and US Digital Service foster a far more credible culture than most typical agencies.
  • Nginx for Developers: An Introduction provides the first steps to getting an initial Nginx configuration up and running.
  • A faster Web server: ripping out Apache for Nginx explains how Nginx can be used instead of Apache in some cases for better performance.
  • Nginx vs Apache: Our view is a first-party perspective written by the developers behind Nginx as to the differences between the web servers.
  • Rate Limiting with Nginx covers how to mitigate against brute force password guessing attempts using Nginx rate limits.
  • Nginx with dynamic upstreams is an important note for setting up your upstream WSGI server(s) if you’re using Nginx as a reverse proxy with hostnames that change.
  • Nginx Caching shows how to set up Nginx for caching HTTP requests, which is often done by Varnish but can also be handled by Nginx with the proxy_cache and related directives.
  • Nginx web server tutorials are oldies but goodies on setting up previous versions of Nginx.
  • Dynamic log formats in nginx explains how to use the HttpSetMiscModule module to transform variables in Nginx and map input to controlled output in the logs. The author uses this technique for pixel tracking but there are other purposes this method could be used for such as advanced debugging.
  • Detecting Bots in Apache & Nginx Logs is an awesome tutorial that shows how to filter web crawlers and bots from your traffic logs when using them for web traffic analytics.

Nginx发行版