nginx redirect non www to www while maintainng protocol - redirect

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;

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: How to redirect all http / https requests to the main domain

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?

Nginx redirrect http to https

I need to redirect all the http requests to https in nginx and also to use some self-generated SSL certificate (using opensssl), but something is wrong with my configuration:
server {
listen 80;
server_name x.x.x.x;
# 301 = permanent redirect, 302 = temporary redirect
#return 301 https://x.x.x.x;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name x.x.x.x;
root /usr/share/nginx/html;
index index.html index.htm;
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
}

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.