简体   繁体   中英

Django How to redirect only example.com to https and *.example.com to http?

I need http://example.com to redirect to https://example.com . Whereas http://www.example.com , http://api.example.com must not redirect ie, subdomains need not redirect to https.

I can understand the configuration here by looking at it. But don't know to move further. Please help me. My configuration so far is:

sites-available/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;
}

}

Edit this:

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

Into:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM