Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 配置HTTPS重定向失败_Django_Ssl_Nginx_Hosting - Fatal编程技术网

Django 配置HTTPS重定向失败

Django 配置HTTPS重定向失败,django,ssl,nginx,hosting,Django,Ssl,Nginx,Hosting,我正在digitalocean上托管一个Django应用程序。我跟随完成它的SSL认证。根据该教程,我不知道在何处添加这行代码: return 301 https://$server_name$request_uri; 我尝试在/etc/nginx/sites enabled/leptitox\u pro server { listen 80; server_name 68.183.203.33 yahkut.com www.yahkut.com; return 301

我正在digitalocean上托管一个Django应用程序。我跟随完成它的SSL认证。根据该教程,我不知道在何处添加这行代码:

return 301 https://$server_name$request_uri;

我尝试在
/etc/nginx/sites enabled/leptitox\u pro

server {
    listen 80;
    server_name 68.183.203.33 yahkut.com www.yahkut.com;
    return 301 https://$server_name$request_uri;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/leptitoxadmin/pyapps/Leptitox;
    }

    location /media/ {
        root /home/leptitoxadmin/pyapps/Leptitox;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
当它不起作用时,我将它添加到
/etc/nginx/sites available/leptitox\u pro

server {
    listen 80;
    server_name 68.183.203.33 yahkut.com www.yahkut.com;
    return 301 https://$server_name$request_uri;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/leptitoxadmin/pyapps/Leptitox;
    }

    location /media/ {
        root /home/leptitoxadmin/pyapps/Leptitox;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
它在那里也不起作用,所以我在
/etc/nginx/nginx.conf
中的服务器代码块下面添加了:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        server {                                           # new
          listen 80;                                       # new
          server_name yahkut.com;                          # new
          return 301 https://$server_name$request_uri;     # new
        }

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
}
然后我重新启动了ngnix并运行了
nginx-t
,得到了一条成功消息,当我运行该网站时,我得到了
404找不到
或该网站的不安全版本。
请帮我做这个。谢谢

您必须将运行端口80的服务器块与运行端口443(SSL)的服务器块分开。就这样,

server {
    listen 80;
    server_name 68.183.203.33 yahkut.com www.yahkut.com;
    return 301 https://$server_name$request_uri;
    # Stop here, it's will be redirect to HTTPS. There's no left to execute
}

server {
    listen 443 ssl;
    server_name yahkut.com www.yahkut.com;
    ssl_certificate /path/to/certificate/your_domain_chain.crt;
    ssl_certificate_key /path/to/your_private.key;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/leptitoxadmin/pyapps/Leptitox;
    }

    location /media/ {
        root /home/leptitoxadmin/pyapps/Leptitox;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

添加这些服务器块

这是为了将http重定向到https

server {
   listen 80;
   server_name example.com; 
   location / {
          return 301 https://$host$request_uri;
   }
}
使用ssl创建主块

server {
    listen 443 ssl ;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;  
    index index.html index.htm index.nginx-debian.html;
    server_name example.com; 
    location / {
    proxy_pass http://localhost:5003; // Your port goes here
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
}

这在/nginx/sites enabled/default下,或者您可以在此文件夹中为其创建不同的文件

我必须在哪里添加这些代码块?nginx.conf
/etc/nginx/sites enabled/leptitox_pro
很好
nginx:[警告]0.0.0:443上的服务器名称“yahkut.com”冲突,忽略了
我在重新启动nginx时收到此错误首先删除其他文件上yahkut.com的所有其他代码。