How can I test https connections with Django as easily as I can non-https connections using 'runserver'?

后端 未结 9 924
生来不讨喜
生来不讨喜 2020-11-28 17:55

I have an application that uses \"secure\" cookies and want to test it\'s functionality without needing to set up a complicated SSL enabled development server. Is there any

9条回答
  •  北海茫月
    2020-11-28 18:15

    Handle SSL/TLS with a proxy such as Nginx rather than Django. Nginx can be set up to listen on port 443 and then forward requests to your Django dev server (typically http://127.0.0.1:8000). An Nginx configuration for this might look like the following:

    server {
        listen 443 ssl;
        server_name django-dev.localhost;
    
        ssl_certificate /etc/ssl/certs/nginx_chain.pem;
        ssl_certificate_key /etc/ssl/private/nginx.pem;    
    
        location / {
            proxy_pass http://127.0.0.1:8000/;
            proxy_set_header Host $host;
        }
    }
    

    You'll also need to map django-dev.localhost to 127.0.0.1 and add django-dev.localhost to ALLOWED_HOSTS in settings.py. On Linux, you'll need to add the following line to /etc/hosts:

    127.0.0.1   django-dev.localhost
    

    You'll then be able to reach your dev site by going to https://django-dev.localhost in your browser (you will need to bypass your browser's security warning).

提交回复
热议问题