Welcome! Log In Create A New Profile

Advanced

One single 301 redirect to HTTPS + with WWW subdomain

Posted by saby 
One single 301 redirect to HTTPS + with WWW subdomain
November 26, 2020 06:36PM
Hello, please I don't know what I am doing wrong. I have removed a default file entry from */etc/nginx/sites-enabled* and there is only one enabled site:


server {
listen 80;
listen 443 ssl;
server_name example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

return 301 https://www.$server_name$request_uri;
}

server {
listen 443 ssl;
server_name www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

location = /favicon.ico { access_log off; log_not_found off; }

location /static/ {
root /django/example;
}

location /media/ {
root /django/example;
}

location / {
include proxy_params;
proxy_pass http://unix:/run/mydomain.sock;
}
}



When I try a CURL test "curl -IL http://soldvehicle.com/", it is still doing two 301 redirects, instead of one. It first redirects from http to https and then to www:


HTTP/1.1 301 Moved Permanently
Date: Tue, 24 Nov 2020 13:29:24 GMT
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Tue, 24 Nov 2020 14:29:24 GMT
Location: https://example.com/
...
...
Server: cloudflare

HTTP/2 301
date: Tue, 24 Nov 2020 13:29:24 GMT
content-type: text/html
set-cookie: __cfduid=d750a6e7c1d415accb428ff6431220db01606224564; expires=Thu, 24-Dec-20 13:29:24 GMT; path=/; domain=.example.com; HttpOnly; SameSite=Lax; Secure
location: https://www.example.com/
cf-cache-status: DYNAMIC
...
...
server: cloudflare

HTTP/2 200
date: Tue, 24 Nov 2020 13:29:25 GMT
content-type: text/html; charset=utf-8
set-cookie: __cfduid=d075d1d072fd35d4ec365037a68bb30cdb608224565; expires=Thu, 24-Dec-20 13:29:25 GMT; path=/; domain=.example.com; HttpOnly; SameSite=Lax; Secure
...
...
server: cloudflare



/var/log/nginx/access.log:
141.101.aa.xxx - - [24/Nov/2020:15:03:08 +0000] "HEAD / HTTP/1.1" 301 0 "-" "curl/7.68.0"
141.101.aa.yy - - [24/Nov/2020:15:03:09 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.68.0"

(these IPs looks like cloudflare IPs )



Is there something wrong, with my nginx configuration? Is possible to do one single 301 redirect from "http and non-www" to "https + www"?

Thank you for any advice!



Edited 1 time(s). Last edit at 11/26/2020 06:37PM by saby.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 85
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready