当使用NGINX的HTTPS

时间:2018-10-11 00:28:51

标签: django amazon-web-services nginx https lets-encrypt

我正在aws上部署django应用程序,并且在使用http时运行良好,因此我使用了加密功能来启用https。它工作正常,我可以看到索引页面,但是当我尝试登录(发布请求)时,它返回404错误。我对此一无所知。

这是我的nginx配置文件:

upstream sample_project_server {
  server unix:/home/ubuntu/django_env/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 443 ssl;
    server_name mydomain.es www.mydomain.es;
    ssl_certificate /etc/letsencrypt/live/mydomain.es/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.es/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    root /var/www/html;
    index index.html index.htm;
    server_name localhost;

    client_max_body_size 4G;
    access_log /home/ubuntu/logs/nginx-access.log;
    error_log /home/ubuntu/logs/nginx-error.log;

    location ~* \.(eot|otf|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin *;
    }

    location /static/ {
        alias   /home/ubuntu/static/;
    }

    location /media/ {
        alias   /home/ubuntu/media/;
    }

    location / {
        proxy_pass http://localhost:3000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;;
        proxy_set_header X-Forward-Proto http;
        proxy_set_header X-Nginx-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://sample_project_server;
            break;
        }

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
   }
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/ubuntu/static/;
    }
}
server {
    listen 80;
    server_name mydomain.es www.mydomain.es;
    return 301 https://$host$request_uri;
}

这是我在Django中的设置文件的一部分:

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
    'localhost:8080','localhost:8000',
)
CORS_ORIGIN_REGEX_WHITELIST = (
    'localhost:8080','localhost:8000',
)
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

非常感谢!

0 个答案:

没有答案