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

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

Related

Nginx HTTPS (443) config ignored and HTTP (80) used instead

I was trying to redirect non secure (domain.com and www.domain.com) to secure version and I was getting a "too many redirects" error.
So, I decided to simplify the config to test and try to find out the error.
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
return 302 https://www.google.com;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/cert.key;
server_name example.com www.example.com;
return 302 https://www.amazon.com;
}
If I am not wrong, when I visit http://example.com/ or http://www.example.com/, I should be redirected to https://www.google.com
And I if I visit https://example.com/ or https://www.example.com/, I should be redirected to https://www.amazon.com
But, any case, I am always redirected to https://www.google.com. What is wrong?
Your browser might be caching the redirects.
Try using Incognito windows for both the test cases. Your config file seems to be fine.
For your domain.com, you can have following configuration:
server {
server_name _;
listen 443;
root /var/www/html;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}

Nginx re-directions from "www" to "non www" and from "http" to "https"?

I have my app hosted in the base URL: https://myapp.com/
Now I want to add re-directions from "www" to "non www" / "http" to "https", where:
https://myapp.com/
https://www.myapp.com/
http://myapp.com/
http://www.myapp.com/
Last 3 URLs should 301 redirect to the first one.
Right now second URL is not redirected and the last 2 are redirected using a 307 redirection instead of 301.
Here is my nginx configuration:
server {
listen 80;
server_name myapp.com www.myapp.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name myapp.com www.myapp.com;
server_tokens off;
ssl_certificate /etc/nginx/conf.d/self-signed-fullchain.pem;
ssl_certificate_key /etc/nginx/conf.d/self-signed-privkey.pem;
include /etc/nginx/conf.d/options-ssl-nginx.conf;
ssl_dhparam /etc/nginx/conf.d/ssl-dhparams.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ ^/(api)/ {
proxy_pass http://myapp:3000;
}
}
So how can I actually do this?
Just add one more server block with server_name www.myapp.com;, and add redirect:
return 301 https://myapp.com$request_uri;
Edit main server block to server_name myapp.com;
Should be something like that:
server {
listen 80;
server_name myapp.com www.myapp.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name www.myapp.com;
server_tokens off;
ssl_certificate /etc/nginx/conf.d/self-signed-fullchain.pem;
ssl_certificate_key /etc/nginx/conf.d/self-signed-privkey.pem;
include /etc/nginx/conf.d/options-ssl-nginx.conf;
ssl_dhparam /etc/nginx/conf.d/ssl-dhparams.pem;
return 301 https://myapp.com$request_uri;
}
server {
listen 443 ssl;
server_name myapp.com;
server_tokens off;
ssl_certificate /etc/nginx/conf.d/self-signed-fullchain.pem;
ssl_certificate_key /etc/nginx/conf.d/self-signed-privkey.pem;
include /etc/nginx/conf.d/options-ssl-nginx.conf;
ssl_dhparam /etc/nginx/conf.d/ssl-dhparams.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ ^/(api)/ {
proxy_pass http://myapp:3000;
}
}

In Nginx, I want to redirect the sub-domain request with request-uri to sub-domain but if there is no request-uri in it should redirect to main domain

My problem statement :
my domain : example.com
sub-domain : main.example.com
when we will access:
1. http://main.example.com/xyz or https://main.example.com/xyz :
It must be redirect to https://main.example.com/xyz
http://main.example.com or https://main.example.com :
It must be redirect to https://www.example.com
I am using nginx. What will be configuration file for Nginx server?
My current setting is :
server{
listen 443;
ssl on;
ssl_certificate /var/www/html/demo.crt;
ssl_certificate_key /var/www/html/demo.key;
server_name main.example.com$request_uri;
location / {
root /var/www/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name main.example.com$request_uri;
return 301 https://main.example.com$request_uri;
}
server {
listen 80;
server_name main.example.com;
return 301 https://www.example.com;
}
server {
listen 443;
server_name main.example.com;
return 301 https://www.example.com;
}
Try
server{
listen 80;
listen 443 ssl;
server_name main.example.com;
ssl_certificate /var/www/html/demo.crt;
ssl_certificate_key /var/www/html/demo.key;
location / {
proxy_pass https://www.example.com;
}
location ~ ^(/.+) {
return 301 https://main.example.com$1;
}
}
http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

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

Redirect https:// to https://www

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;

Resources