Redirect https:// to https://www - nginx

I have a ubuntu server with DigitalOcean which is managed using forge.laravel.com
I have tried numerous different methods in the nginx.conf file to redirect all the possible scenarios to my domain https://www.domain.com
https://www.domain.com = https://www.domain.com -- GREAT
http://domain.com = https://www.domain.com -- GREAT
http://www.domain.com = https://www.domain.com -- GREAT
https://domain.com = https://domain.com -- This should redirect to the www.
heres part of my nginx.conf
server {
listen 80;
server_name domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 80;
server_name www.domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
root /home/forge/default/public;
.... other nginx.conf configuration
Can anyone tell me what I'm doing wrong? I've tried numerous combinations.

Replace
server {
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
root /home/forge/default/public;
By
server {
listen 443 ssl;
server_name domain.com;
return 301 https://www.domain.com$request_uri;
#INSERT HERE CERTIFICATES DIRECTIVES
}
server {
listen 443 ssl;
server_name www.domain.com;
root /home/forge/default/public;

Related

Nginx - redir only non-www to www, leave other subdomains intact (ignore subdomains)

what I try to achieve (redirecting only non-www to www - ignoring all subdomains)
example.com > https://www.example.com
example.com/query-string > https://www.example.com/query-string
http://example.com > https://www.example.com
https://example.com > https://www.example.com
es.example.com > https://es.example.com
http://es.example.com > https://es.example.com
http://es.example.com/query-string > https://es.example.com/query-string
...
My version is 1.22.1.
What I got so far:
server {
listen 80;
listen [::]:80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/key.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/ca.pem;
ssl_stapling_verify on;
server_name example.com;
#return 301 https://www.example.com$request_uri;
return 301 https://www.$host$request_uri;
}
server {
server_name www.example.com;
access_log /var/log/nginx/example.com.access.log rt_cache;
error_log /var/log/nginx/example.com.error.log;
root /var/www/example.com/htdocs;
index index.php index.html index.htm;
include ...
}
Second server block don't work. This
return 301 https://www.example.com$request_uri;
does not work because it redirs all subdomains to www (https://de.example.com > https://www.example.com)
This
return 301 https://www.$host$request_uri;
does not work even it redirs non-www do www version but in the same time it redirs all subdomains to www.subdomain (https://de.example.com > https://www.de.example.com).
I think the problem is in the server_name of the second server block. Any ideas how to solve this?
Try to use this config:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
server_name "~(?<!www)\.example\.com$";
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
return 301 $scheme://www.$host$request_uri;
}
server {
listen 443 ssl;
server_name *.example.com;
...
}

Nginx How to prevent processing requests with undefined server names

Nginx is 1.14.1 version
have several virtual hosts and default in the /etc/nging/sites-enabled:
I've tried to configure using this doc: http://nginx.org/en/docs/http/request_processing.html
default
server {
listen 80;
server_name "";
return 444;
}
server {
listen 443 ssl http2 default_server;
server_name _;
ssl_certificate ....
ssl_certificate_key .....
add_header Strict-Transport-Security 'max-age=31536000';
return 444;
}
domain1
server{
listen 80;
server_name domain1;
return 301 https://$server_name;
}
server {
server_name domain1;
listen 443 ssl;
..................
}
but when tried to get access using server IP nginx redirect to domain1. please help what's wrong here. I'd like to deny access by IP to sites and leave only requests with domain name

Nginx redirect url to secure domain

My webstie is hosted on aws EC2 instance and I have nginx 1.12.2 and Ec2 Operating system is centos, how do I redirect http://example.com and https://example.com to https://www.example.com .
Thanks
You will need 3 server sections (and I'm almost sure you forgot to handle http://www.example.com case)
server {
listen 80;
server_name example.com;
location / {
return 301 https://www.example.com$request_uri;
}
...
server {
listen 443 ssl;
server_name example.com;
ssl_certificate ...
location / {
return 301 https://www.example.com$request_uri;
}
...
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate ...
location / {
#your configuration here
}
...

Nginx will rewirte image domain http to https, But I don't want the static server as https

I have a wordpress website with https protocol by configuring the nginx 301 redirect:
server {
listen 80;
server_name xxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}
And my article has some image links with static server like:
http://yyy.com/1.png
But when i access this article: it will be https://yyy.com/1.png, How do I configure the nginx that can still use http for the image static server?
You would do that using below config
server {
listen 80;
server_name xxx.com;
location ~* \.(png|ico|jpeg)$ {
root <your root folder>;
try_files $uri =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}

Ngnix setup for dedicated subdomains

I want Ngnix to handle only a few subdomains and if it is not matching it should return an 404.
The following subdomains should work: domain.com, www.domain.com, api.domain.com and ftp.domain.com.
I use the following config:
server {
listen 80;
listen [::]:80;
server_name *.domain.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
listen [::]:443 ipv6only=on;
server_name domain.com www.domain.com api.domain.com ftp.domain.com;
.....
}
server {
listen 443 default_server;
server_name _;
return 444;
}
The problem is that the website keeps working on every subdomain like test.domain.com. Of casurse the DNS is setup with an wildcard and I don't want to change that.
With adding the default_server I'm getting ssl errors?
Any suggestions?

Resources