Nginx redirect http to custom https AND https to custom https - redirect

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!

Related

What is '_' in Nginx listen option?

What is _ in nginx listen option?
I read listen 80 _; in nginx cookbook.
I found server_name _; mean just invalid server_name.
Then what is in listen?
ref https://nginx.org/en/docs/http/ngx_http_core_module.html#server
listen 443 ssl : makes nginx listen on all ipv4 address on the server, on port 443 (0.0.0.0:443)
while
listen [::]:443 ssl : makes nginx listen on all ipv6 address on the server, on port 443 (:::443)
[::]:443 will not make nginx respond on ipv4 by default, unless you specify parameter ipv6only=off :
listen [::]:443 ipv6only=off;
ssl :
The ssl parameter (0.7.14) allows specifying that all connections accepted on this port should work in SSL mode.
http2 :
The http2 parameter (1.9.5) configures the port to accept HTTP/2 connections.
This doesn't mean it accepts only HTTP/2 connections.

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 - 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.

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;