Nginx redirect not applied - nginx

I am configuring an nginx server at the moment and I have two domain types:
www.example.com
example.com
On the server I added a new server block with the following code to redirect every incoming requests to the domain without www.
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
The problem is that I only got HTTP 200 instead of 301.
The changed file is: /etc/nginx/sites-enabled/default
What am I missing? I did exactly how the documentation says in Digital Ocean.

Related

Redirection to newsite in nginx

We have an nginx 1.20.1 webserver on Ubuntu 16.04 hosting many sites. One site is oldsite.example.com and while we're building a newsite.example.com we wish for users to start using the new URL (newsite.example.com). We already made a DNS change but wanted clarity on the nginx part. New URL has to work with both HTTP/HTTPS and we have separate SSL cert it as well.
Thank you for reading.
oldsite nginx conf:
server {
server_name oldsite.example.com
;
listen 80;
if ($host = oldsite.example.com) {
return 301 https://$host$request_uri;
}
return 404;
}
server {
listen 443 ssl;
add_header Content-Security-Policy upgrade-insecure-requests;
For your question about redirection, a simple 302 will do the trick. It is similar to the 301 redirection you are already using:
return 301 https://$host$request_uri;
And you can return sth. like this, but in your ssl server section:
server {
listen 443 ssl;
server_name newsite.example.com;
return 302 https://oldsite.example.com$request_uri;
...
But I would recommend a proxy_pass, or just copy & paste your old configs into the new one. It will save you a redirection.
And for all of your misunderstandings about the protocol,
There is a server name indicator, or SNI, in plain text, in a TLS request (at least for now). If you know about the Host: header, that is it. You can have multiple HTTPS sites on the same host distinguished by server_name, which is exactly what you do for HTTP sites.
Even for HTTP sites, the Host: header is required (for HTTP/1.1 or higher, but no higher), and the server will look at it. You cannot simply CNAME (or sth. similar) a domain into another without modifying server configuration.
wouldn't the user's browser, when they type newsite.example.com and end up at oldsite.example.com
As I said, there is a server name indicator and the server will look at it, as it will look at Host: header. You can distinguish different sites on the same host by different server names.

NGINX HTTPs redirect not working for subdomains

I am using the following NGINX configuration to redirect all wildcard subdomains to https:
server {
listen 80;
listen [::]:80;
server_name "~^(?<sub>.+)\.example\.com$";
return 302 https://$server_name$request_uri;
}
It works on certain browsers Chrome, but not through some VPN platforms.
The website error returned is:
The requested URL was not recognised as a valid URL. Attempted to load: https://~^(?<sub>.+)\.example\.com$/
I have also tried using the following as the server_name:
server_name ~^.*$
But it returns the error:
The website error returned is:
The requested URL was not recognised as a valid URL. Attempted to load: ~^.*$
How could I get NGINX to redirect HTTP to HTTPS?
I have another site in Apache which redirects just fine across all platforms, so cannot be an issue with the online VPN services themselves.

Nginx as proxy : how to exclude specific folder under https but not under http

I can rewrite some specific http folders to https folders, but i can't rewrite all https except these specific folders from https to http; i'm stuck in a loop
Using NGinx 1.12 as a proxy, handling both http and https
i have one server section to handle http 80 and one server section to handle https (i know they can be together in the same section).
both of them are beginning that way
location / {
proxy_pass
server {
listen 80;
i have
location ~ ^/(xxx|yyy|zzz)/.*$ {
rewrite ^ https://www.example.com$uri permanent;
}
and anytime i'm in http, it is redirecting to https fine for the xxx,yyy and zzz folders. so far, so good.
but under server {
listen 443 ssl;
, i would like to redirect everything except the xxx,yyy,zzz folder to go back to http.
I did try to do the reverse in the https section meaning :
location / {
rewrite ^ http://www.example.com$uri permanent;
}
and
location ~ ^/(xxx|yyy|zzz)/.*$ {
#do nothing
}
but it is not working, either i get a 404 error or a loop
The only solution i found as Nginx is a proxy is making Apache handling the redirect on its side.
so,
a. nginx 80 is redirecting to nginx 443 specific folders.
b. all https is redirect by nginx 443 to apache 443, and then in apache 443 conf i do a test, if it the specific folders, i stop, and otherwise i redirect to nginx 80.
It's working, but i'm sure it is possible to make nginx handle it and avoid this 1 loop. if someone as a beautiful answer :-)

NGINX : redirecting non-www to www

Could be the stupid one and could be most duplicate question ever asked.
But yes, I can't get this done.
I've one domain which is : example.co.in
I want to redirect to www.example.co.in
Server is : NGINX
My vHost config is:
server {
listen 80;
server_name example.co.in www.example.co.in;
return 301 http://www.example.co.in$request_uri;
}
server {
listen 80;
server_name example.co.in www.example.co.in;
access_log /var/www/example.co.in/public_html/logs/access.log;
error_log /var/www/example.co.in/public_html/logs/error.log;
....
}
I've a A record entry for www in my dns server.
Edit:
I'm getting ERR_TOO_MANY_REDIRECTS error
Any help would be help full.
You have to server blocks with the same port and server_names. It hits the first one and tells the requester to redirect to www which the requester makes another request to the same server block which starts the process over again.
If you want it to redirect to www then take www out of the first server block then reload or restart nginx.

Redirect root to another folder in nginx server

I am very new to nginx. I was using Apache previously and was using htaccess to redirect root to another folder. Now migrated to nginx. Here is four things I want to achieve
Redirect http to https e.g. http://www.example.com -> https://www.example.com
Then redirect root to another folder but URL must be rewritten as example.com not example.com/blog
All files in php should show as html in url e.g. example.com/contact.php -> example.com/contact.html
example.com/page.php?content=file -> example.com/file
I found this code to redirect but don't know where to insert this code nginx.conf or any other file?
server{
location = / {
return 301 https://www.example.com/blog;
}
}
Also please suggest me if these changes are made in nginx.conf file or /etc/nginx/sites-available/example.com file.
To redirect HTTP to HTTPS traffic you can create another server block to match the incoming HTTP and domain then rewrite to your HTTPS server block.
Which file do you put this in? Both /etc/nginx/nginx.conf and /etc/nginx/sites-available/example.com should get read (unless you changed config) so it shouldn't matter, but I personally put these configs in /etc/nginx/sites-available/example.com because I consider it part of the same domain.
file: /etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
...
# your location blocks and redirects here
}

Resources