NGINX: How to redirect all http / https requests to the main domain - redirect

I'm trying to configure redirect on my Nginx server.
I only want to use the domain : example.com (no subdomain or www) with https.
Everything is fine with http but with https it only redirects www, so I have :
http://example.com [redirect to] https://example.com
http://www.example.com [redirect to] https://example.com
http://foo.example.com [redirect to] https://example.com
https://www.example.com [redirect to] https://example.com
https://foo.example.com [no redirect] NET::ERR_CERT_COMMON_NAME_INVALID
In my sites-available/default file, I have two server blocks:
One to redirect request:
server {
listen 80;
listen 443 ssl;
server_name _;
return 301 https://example.com$request_uri;
}
Server configuration:
server {
listen 443 ssl;
server_name example.com;
...
}
I think my redirect server block is not configured properly for https, how can I correct the error?

Related

how to redirect http://www to https://www - nginx

I'm going to redirect http://www.example.com to https://www.example.com. in nginx server-block I have below snippet:
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.$host$request_uri;
}
In case example.com it works properly and redirects http://example.com to https://www.example.com, but in http://www it doesn't redirect properly and redirect the browser to https://www.www.example.com. How can I solve this problem?
Use $server_name instead of $host, example:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}

Nginx redirect http to custom https AND https to custom https

I have a server on a restricted vps. I only have access to port 80, and some higher ports - no 443.
I was able to redirect all http to https on port 11111 [with return 301], OR redirect https request made on port 443 to 11111 [with error_page 497].
I would like to redirect http to https on custom port, and 'redirect' https to https custom port. I cannot listen on 443.
This is what I have so far:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.org;
return 301 https://$server_name:11111$request_uri;
#error_page 497 https://$server_name:11111$request_uri;
}
server{
# SSL configuration
#
listen 11111 ssl default_server;
listen [::]:11111 ssl default_server;
}
Thanks in advance!

Nginx - redirect correctly from http:// to https:// without www

Following my configuration file:
server {
listen [::]:443 ipv6only=off ssl;
server_name www.example.com;
// ssl stuff
return 301 https://example.com$request_uri;
}
server {
listen [::]:80 ipv6only=off;
return 301 https://example.com$request_uri;
}
server {
listen [::]:443 ssl;
server_name example.com;
// php and ssl stuff
}
I don't understand why http://www.example.com redirects to https://www.example.com and then to https://example.com. How to redirect from http://www.example.com directly to https://example.com?
NGINX config for redirect from HTTP to HTTPS without WWW:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.com www.example.com;
##here-ssl-settings##
return 301 https://example.com$request_uri;
}
With HSTS enabled, the first redirect is done directly by your browser without any network interaction.

how to redirect a url like https://example.com to https://www.example.com in haproxy

I know how to redirect from:
http example.com to https www.example.com
and
http www.example.com to https www.example.com
but don't know how to redirect from:
https example.com to https www.example.com in Haproxy
redirect prefix https://www.example.com code 301 if { hdr(host) -i example.com } in both frontend
frontend weblb
bind *:80
acl is_www hdr_beg(host) ilanni.com
redirect prefix https://www.ilanni.com code 301 if is_www
acl is_host hdr_beg(host) wwww.ilanni.com
redirect scheme https if !{ ssl_fc }
bind *:443 ssl crt /etc/haproxy/ilanni.com.pem
use_backend sellerserver if is_host
backend sellerserver
balance source
server web1 127.0.0.1:8111 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

nginx redirect non www to www while maintainng protocol

I am trying to redirect non www to www but making sure to keep the requested protocol so 80 would go to http://www. and 443 would go to https://www.
I have gotten this far..
server {
listen 80;
listen 443;
server_name domain.com;
# add ssl settings
return 301 $scheme://www.domain.com$request_uri;
}
separate http and https server config.
change return 301 ... to rewrite ^(.*) http://www.domain.com/$1 permanent; (http or https)
change listen 443; to listen 443 ssl;