How to run django with nginx on a windows machine?

2019-02-03 19:26发布

问题:

I have a django project. I have installed nginx server. I want to run nginx along with django on windows machine. I have tried a few blogs Nginx Django Uwsgi. But all of them required uwsgi. But the uwsgi is not installing on windows and gives "uname" error. Is there any way to install and run nginx and django on windows ? Any pointers will be very helpfull thanks.

回答1:

Best way (IMHO) is using apache+mod_wsgi. Both uWSGI and Gunicorn are not windows-friendly (albeit uWSGI has cygwin support, so you can eventually try it [if you are brave enough]).

Another approach would be installing a linux vm (virtualbox, vmware...) on the windows machine to host your application and proxy requests from local IIS to the virtual system.



回答2:

This question is from a year ago but I'll answer this for the others looking for a solution to their Windows Django/nginx problems.

As uWSGI is clearly not an option for Windows (forget about cygwin/virtual environments). I didn't want to go for mod_wsgi + Apache because I had to merge an existing AngularJS/nginx project together with my newly created Django project.

I ended up using FastCGI, even though Django support for it will soon be deprecated, it still works. This (somewhat badly written but it does help) tutorial helped me with this.

The key actions were:

  • Make sure flup is installed ( pip install flup)
  • Edit your nginx.conf as shown in the tutorial:

    location / {    
    # host and port to fastcgi server
    fastcgi_pass 127.0.0.1:<port nr>;
    fastcgi_pass_header Authorization; 
    fastcgi_hide_header X-Accel-Redirect;
    fastcgi_hide_header X-Sendfile;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors    off;
    fastcgi_param       CONTENT_LENGTH  $content_length;
    fastcgi_param       CONTENT_TYPE    $content_type;
    fastcgi_param       PATH_INFO       $fastcgi_script_name;
    fastcgi_param       QUERY_STRING    $query_string;
    fastcgi_param       REMOTE_ADDR     $remote_addr;
    fastcgi_param       REQUEST_METHOD  $request_method;
    fastcgi_param       REQUEST_URI     $request_uri;
    fastcgi_param       SERVER_NAME     $server_name;
    fastcgi_param       SERVER_PORT     $server_port;
    fastcgi_param       SERVER_PROTOCOL $server_protocol;
    }
    
  • Then run your Django project with fcgi, with the port being the same one as seen above in the .conf python manage.py runfcgi method=threaded host=127.0.0.1 port=<port nr>

Worked for me.
If you don't have any reason like I had for which you really want to use nginx, I'd suggest going for the mod_wsgi + Apache webserver approach, it will probably make your life a lot easier.



回答3:

You can try to use Helicon Zoo to run Django applications on Windows machine with IIS 7+. Helicon Zoo is stable and production-ready.

Tutorial here: Running Django on Windows



回答4:

After the Django1.9+, the FastCGI support via the runfcgi management command is removed. So you have to use old version django, or use the mod_wsgi + apache.

If you only want to develop the django code, then you can just edit nginx.conf add proxy_pass http://127.0.0.1:8000 into the location option. For example:

    location /api/ {
        proxy_pass http://127.0.0.1:8000; 
    }

    location /static/ {
        proxy_pass http://127.0.0.1:8000; 
    }


标签: django nginx