How to run django with nginx on a windows machine?

前端 未结 4 1742
一生所求
一生所求 2020-12-29 13:37

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 the

4条回答
  •  半阙折子戏
    2020-12-29 14:01

    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:;
      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=

    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.

提交回复
热议问题