django - Django 如何仅将 example.com 重定向到 https 并将 *.example.com 重定向到 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;
}
}
推荐阅读
- android - Firebase Firestore 未连接
- python - XOAUTH2 Django 中的无效 SASL 参数
- ruby-on-rails - 如何使用动态绑定编写 Rails ActiveRecord 更新查询?
- asp.net-mvc - 如何使用来自 ASP.NET MVC 视图的@Input 将 JSON 数组传递给 Angular2 模板?
- angularjs - 来自服务的Angularjs绑定值
- python - Keras LSTM 输入 - ValueError:检查输入时出错:预期 input_1 具有 3 个维度,但得到的数组具有形状 (1745, 1)
- python - Python pandas 设置 NaN 值失败
- tomcat7 - Ubuntu VM CPU 过载
- arrays - 是否可以在 PowerShell 中使 IndexOf 不区分大小写?
- filter - Ansible: Use filter as conditional in playbook for role-inclusion