Nginx 和 Gunicorn 通信的两种方式

通常在启动Gunicorn之后,我们希望Nginx将网络请求转发到Gunicorn。通过在Nginx的配置文件中定义upstream让Nginx知道应该把请求转发到哪里,格式如下:

upstream backend {
  server address [params]
}

The address can be specified as a domain name or IP address, with an optional port, or as a UNIX-domain socket path specified after the “unix:” prefix. If a port is not specified, the port 80 is used. A domain name that resolves to several IP addresses defines multiple servers at once.

有两种方式搭建起两者之间的通信

  1. 地址:端口
    例如:
server backend1.example.com //  Case 1: remote service
server 127.0.0.1:8080  //  Case 2: local gunicorn

使用这种配置要求你的web application是跑在其他域名下(Case 1),或者Gunicorn是跑在本地8080(Case 2)。
这样两者就是通过http进行通信的。

  1. Unix socket路径
server unix:///tmp/nginx.socket

使用这种配置要求你的Gunicorn是跑在本地,并且Gunicorn 的设置为 --bind unix:///tmp/nginx.socket
这样两者使用过socket进行通信。

参考文章

Nginx doc about upstream definition
Deploy Nginx + Gunicorn + Flask on Heroku
Deploy Nginx + Gunicorn + Flask on Digital Ocean

你可能感兴趣的:(Nginx 和 Gunicorn 通信的两种方式)