nginx
Nginx (pronounced "engine X") is a highly-performant and widely-used open source web server. When we serve Python applications, we typically do so with nginx serving as a reverse proxy. You can also use nginx to serve multiple applications from a single machine.
The following is a canonical example nginx configuration that is aligned with the way we typically deploy apps. A few highlights:
- By default, this config presumes your application is serving on port 5000. This is the default port for AWS's Elastic Beanstalk, so it should also work as a replacement for its default nginx config.
- It automatically redirects all HTTP requests to HTTPS, or, if the client supports it, HTTP/2. Support for HTTP/2 was added in Nginx 1.9.5 or later.
- The SSL ciphers are configured according to recommendations of security researchers that discovered the "Logjam" SSL cipher vulnerability.
- The configuration requires, in addition to an SSL certificate and private key, a 2048-bit Diffie-Hellman group generated with OpenSSL:
openssl dhparam -out dhparams.pem 2048
- Note that you should combine the issued cert and any supplied intermediate CA certs into a single
.pem
file for deploying on a server. Leave out the root certificate, as browsers already include trusted root certs.
upstream app_server {
server localhost:5000 fail_timeout=0;
}
server {
listen 80;
server_name localhost;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
ssl_certificate /etc/nginx/application.pem;
ssl_certificate_key /etc/nginx/application.key;
ssl_dhparam /etc/nginx/dhparams.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_prefer_server_ciphers on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_read_timeout 604800;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}