首页 > 解决方案 > Django 如何仅将 example.com 重定向到 https 并将 *.example.com 重定向到 http?

问题描述

我需要http://example.com重定向到https://example.com。而http://www.example.comhttp://api.example.com不能重定向,即子域不需要重定向到 https。

我可以通过查看它来理解这里的配置。但不知道进一步。请帮我。到目前为止我的配置是:

网站可用/default.py

upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
server_name example.com www.example.com cloud.example.com api.example.com; 
listen 80;
return 301 https://example.com$request_uri;
}

server {
server_name example.com www.example.com cloud.example.com api.example.com;

listen 443;  # <-

ssl on;  # <-
ssl_certificate /etc/ssl/example_cert_chain.crt;  # <-
ssl_certificate_key /etc/ssl/example.key;  # <-
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

# Your Django project's media files - amend as required
location /media  {
    alias /home/django/django_project/media;
}

# your Django project's static files - amend as required
location /static {
    alias /home/django/django_project/static/;
}

# Proxy the static assests for the Django Admin panel
location /static/admin {
   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;  # <-
proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_buffering off;

        proxy_pass http://app_server;
}

}

标签: djangoubuntu

解决方案


编辑这个:

server { 
    server_name example.com www.example.com cloud.example.com api.example.com;
    listen 80; 
    return 301 https://example.com$request_uri; 
}

进入:

server { 
    server_name example.com www.example.com cloud.example.com api.example.com;
    listen 80; 
    if ($http_host = "example.com") { 
        rewrite ^ https://example.com$request_uri permanent;
    }
}

推荐阅读