why site is not redirecting with nginx - nginx

I am trying to redirect the domain example.com to another website example.blogspot.com (which is not hosted in my server).
I had been using htaccess and cpanel, so it was easy to redirect. But why redirect is not working in nginx.
No effects seen with following way in the version nginx/1.15.10 and latest vesta control panel,
Still showing the default page (index.html) generated by vesta control panel. Should I have to change in public_html ? Or restart server ?
I have edited nginx.conf with following code.
method 1
http{
server{
listen 80;
server_name example.com www.example.com;
return 301 https://example.blogspot.com$request_uri;
}
.....
}
method 2
http{
server{
listen [::]:80;
listen 80;
server_name example.com www.example.com;
return 301 https://example.blogspot.com$request_uri;
}
.....
}
method 3
http{
server {
server_name .example.com;
return 301 $scheme://example.blogspot.com;
}
....
}
restarting ngix after save
sudo service nginx restart
sudo systemctl restart nginx

Finally, adding Server Ip address with listen port redirect domain
http{
server{
listen my.server.ip.address:80;
server_name example.com www.example.com;
return 301 https://example.blogspot.com$request_uri;
}
.....
}

Related

Redirecting in nginx results in errors

Environment: Ubuntu 18.04, nginx, WordPress
WordPress is running fine at https://www.example.org. In addition, the following rule is defined to direct http to https:
server {
if ($host = www.example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name www.example.org;
return 404; # managed by Certbot
}
With this, a user's request to http://www.example.org automatically gets redirected to https://www.example.org.
Now, I am trying to define a rule such that https://example.org can also be redirected to https://www.example.org.
Here is my new nginx rule:
server {
listen 443 ssl;
server_name example.org;
return 301 https://www.example.org$request_uri;
}
However, this seems results in breaking the whole site. Even https://www.example.org, that was working fine earlier, gives an error that "this site can't be reached."
This is the last error from nginx error.log:
no "ssl_certificate" is defined in server listening on SSL port while SSL handshaking, client: blah, server: 0.0.0.0:443
To recap,
https://www.example.org works fine
http://www.example.org properly gets redirected to https://www.example.com
https://example.org does not work. It should also have been redirected to https://www.example.org
Can someone please tell me what is it that I am doing wrong? Regards.
You haven't specified if you need all URIs to redirect, but here are several options. Make sure you are restarting the nginx service after these changes.
Specific URIs
server {
listen 80;
server_name example.org www.example.org;
return 301 https://example.org$request_uri;
}
All URIs
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}

Nginx subdomain redirect rule

I have 2 different server, Already added A record entry
I wanted redirect subdomain to different server.
I made changes in nginx configuration, but its still not working.
server {
server_name example.com subdomain.example.com;
return 301 https://www.example.com$request_uri;
}
How about:
server {
listen 80;
# listen [::]:80;
server_name example.com subdomain.example.com;
return 301 https://www.example.com$request_uri;
}
I'm not a nginx expert but usually you have to define the ports you want the server to listen to. Maybe you forgot that.

Redirect non-www to www on nginx server

I want to redirect from https://example.com to https://www.example.com. I read multiple questions here and articles about how to do it but it doesn't seem to work. Here is what I tried:
server{
listen 80;
listen 443 ssl;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
I set an A record with Name www that points to my IP.
I also use Cloudflare if that is important to know ...
Any ideas what I'm doing wrong?

Nginx server_name & listen matches specified patterns

In this example the domain has been replaced with domain.com
Our main issue:
When i type https://domain.com i don't get redirected to https://www.domain.com, we currently don't have a rule for this what would be the best way to solve this?
According to our nginx configuration we have not specified 443 for https://domain.com but still its accessible, why is that?
We have valid ssl certificates for both domain.com and www.domain.com.
We do not have a wildcard certificate *.domain.com.
Our Configuration:
#All non-matching patterns
server
{
listen 80;
#enabling this will cause things to break.
#2015/12/18 09:21:54 [error] 32165#0: *1661 no "ssl_certificate" is defined in server listening on SSL port while SSL handshaking, client: *censored*, server: 0.0.0.0:443
#listen 443 ssl;
#Horrible looking match all pattern.
server_name _ "" domain.com *.domain.com;
return 301 https://www.domain.com$request_uri;
}
#Main site ssl enforced
server
{
listen 443 ssl;
server_name www.domain.com ios.domain.com android.domain.com;
...
}
#Staging / Test site
server
{
listen 443 ssl;
listen 80;
server_name stage.domain.com;
...
}
#Rental cars site ssl enforced
server
{
listen 443 ssl;
server_name hyrbil.domain.com;
...
}
#ios redirect to enforce https
server
{
listen 80;
server_name ios.domain.com;
return 301 https://ios.domain.com$request_uri;
}
#android redirect to enforce https
server
{
listen 80;
server_name android.domain.com;
return 301 https://android.domain.com$request_uri;
}
Bonus question:
Is it possible to match all ssl traffic and do a redirect unless it matches a specific domain, for example make https://xxx.domain.com pass a 301 to https://www.domain.com even tho i don't have a certificate for xxx.domain.com without showing "This page is unsecure, are you sure that you want to proceeed"?
If you have one virtualhost listening on 443, all traffic reaches your IP address will be served by that virtualhost.
Create an SSL virtualhost for domain.com and put a simple redirect in it.
Or create a "catch all/default" SSL virtualhost, and check the HOST header and redirect regarding that, like:
if ($host !~* ^www\.doman\.com$) {
rewrite ^(.*)$ http://www.domain.com$1 permanent;
}
But it will show SSL certificate error on all FQDNs not included in your certificate!

Nginx config redirect www

I'm having trouble figuring out my nginx config.
My config is supposed to force the use of https and no www. Some subdomains api.domain.com, api.staging.domain.com is allowed. The last mentioned is currently configured in another conf file, not enclosed here.
Currently my problem is that this seems to allow www. I need www to be 301 redirected to no www. The subdomain www is setup as a cname in the dns config.
Can someone help me out?
server {
listen 54.00.00.00;
server_name 54.00.00.00;
rewrite .* https://domain.com$request_uri permanent;
}
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443 default ssl;
server_name domain.com api.domain.com;
root /var/www/domain.com/current/web;
##
# Certificate Settings
##
ssl_certificate /etc/nginx/ssl/domain.com.pem;
ssl_certificate_key /etc/nginx/ssl/domain.com.key;
You problem is that this server
server {
listen 80;
return 301 https://$host$request_uri;
}
Matches all servers, even the ones with www mind that if you are requesting http://www.example.com, $host will be www.example.com, so you end up redirecting to the https version with www.
If you want to do a catch all and still strip www from those who have it I suggest it would be better to use regex in the server name
server {
server_name ~^(www\.)?(?<domain>.+)$;
return 301 https://$domain$request_uri;
}

Resources