簡體   English   中英

Django如何僅將example.com重定向到https並將* .example.com重定向到http?

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

我需要http://example.com重定向到https://example.com http://www.example.com,http : //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;
}

}

編輯此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM