nginx重定向www和非www

时间:2017-04-16 17:02:15

标签: django redirect nginx

您好我的第一个Django网站有问题而且不确定我做错了什么。

我基本上想要重定向http:// www。 example.com和http:// example.com和https:// www。 example.com all to> https://example.com

来自http:// example.com的重定向似乎有效。

这就是我的conf文件的样子。

server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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/django_project/media;
}

# your Django project's static files - amend as required
location /static {
    alias /home/django/django_project/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 Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
}


}

server {
listen 80;
server_name www.example.com;
return 301 $scheme://$host$request_uri;
}


server {
listen 80;
server_name example.com;
return 301 $scheme://$host$request_uri;
}



server {
listen [::]:443 ssl;
listen 443 ssl;
server_name www.example.com;
return 301 $scheme://$host$request_uri;
}

我做错了什么?

理查德的回答解决了我的问题。

0 个答案:

没有答案