Skip to content
Myr

nginx.rst

This file

Path

docs/deploying/nginx.rst

69 lines · 2.3 KiB

Last changed by

Not known.

History of this file

Take it

1nginx2=====34`nginx`_ is a fast, production level HTTP server. When serving your5application with one of the WSGI servers listed in :doc:`index`, it is6often good or necessary to put a dedicated HTTP server in front of it.7This "reverse proxy" can handle incoming requests, TLS, and other8security and performance concerns better than the WSGI server.910Nginx can be installed using your system package manager, or a pre-built11executable for Windows. Installing and running Nginx itself is outside12the scope of this doc. This page outlines the basics of configuring13Nginx to proxy your application. Be sure to read its documentation to14understand what features are available.1516.. _nginx: https://nginx.org/171819Domain Name20-----------2122Acquiring and configuring a domain name is outside the scope of this23doc. In general, you will buy a domain name from a registrar, pay for24server space with a hosting provider, and then point your registrar25at the hosting provider's name servers.2627To simulate this, you can also edit your ``hosts`` file, located at28``/etc/hosts`` on Linux. Add a line that associates a name with the29local IP.3031Modern Linux systems may be configured to treat any domain name that32ends with ``.localhost`` like this without adding it to the ``hosts``33file.3435.. code-block:: python36    :caption: ``/etc/hosts``3738    127.0.0.1 hello.localhost394041Configuration42-------------4344The nginx configuration is located at ``/etc/nginx/nginx.conf`` on45Linux. It may be different depending on your operating system. Check the46docs and look for ``nginx.conf``.4748Remove or comment out any existing ``server`` section. Add a ``server``49section and use the ``proxy_pass`` directive to point to the address the50WSGI server is listening on. We'll assume the WSGI server is listening51locally at ``http://127.0.0.1:8000``.5253.. code-block:: nginx54    :caption: ``/etc/nginx.conf``5556    server {57        listen 80;58        server_name _;5960        location / {61            proxy_pass http://127.0.0.1:8000/;62            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;63            proxy_set_header X-Forwarded-Proto $scheme;64            proxy_set_header X-Forwarded-Host $host;65            proxy_set_header X-Forwarded-Prefix /;66        }67    }6869Then :doc:`proxy_fix` so that your application uses these headers.