[Solved]Using the nginx plugin to redirect non-www sites to www

Started by routingMike, May 09, 2019, 04:47:58 PM

Previous topic - Next topic
Hello,

I'm trying to use the nginx plugin web interface to create a redirect from non-www to www domains. I might be missing it, but there doesn't seem to be any options to allow me to redirect a domain in either the http server or location. Nothing fancy, just a return 301 (new url);.

Another option I can do is generate the config files directly on disk, since the plugin has facilities to plop your own nginx server blocks but i hesitate to go that route since nginx currently manages generating the SSL blocks for my HTTPS servers, I'm concerned about keeping the two in sync (the web ui vs the manually generated redirect server blocks).

Does anyone know of a way to have nginx generate http redirects for domains?

Any input here?


I naively thought that was specific for paths. Didn't even consider it might work for domains. I'll try it out.

If you separate the hosts, it will very likely work.

Hi,

I finally got around to trying it, and I don't believe it works, at least not as I need it.

Going through the UI then looking at the generated nginx.conf, I see a

> rewrite example.com https://www.example.com permanent;

However the browser doesn't get redirected. Not does the content show up for what would be under www.example.com

Ideally, what I need is more akin to return 301 https //www.example.com$request_uri, so an http 301 redirect.


Example:

Regex: (.*)
New URL Pattern: https://fabian-franz.eu$1
Flag: Redirect

Result:


curl -v "http://fw.test/test.txt"
> GET /test.txt HTTP/1.1
> Host: fw.test
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx
< Date: Sun, 19 May 2019 16:51:55 GMT
< Content-Type: text/html
< Content-Length: 154
< Connection: keep-alive
< Location: https://fabian-franz.eu/test.txt
<
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

Hello Fabian,

is there a chance to get a real redirect - not a rewrite?

If I use "permanent" I get:
rewrite / https://server.name permanent;

If I use "redirect" I get:
rewrite / https://server.name redirect;

What I need is:
return 301 https://server.name;

At least that is the only option that worked for me. I added it manually, but I guess with next reboot, latest with next config change, it will be reverted to a rewrite.
Intel(R) Xeon(R) Silver 4116 CPU @ 2.10GHz (24 cores)
256 GB RAM, 300GB RAID1, 3x4 10G Chelsio T540-CO-SR


Ah! I think I did it completely wrong then. I was trying to capture on the domain (example.com) to redirect to www.example.com but it makes sense to capture (.*) on the example..com server and do the redirect to www.example.com

I will try that! Thank you.

Quote from: fabian on May 19, 2019, 06:53:26 PM
Example:

Regex: (.*)
New URL Pattern: https://fabian-franz.eu$1
Flag: Redirect

Result:


curl -v "http://fw.test/test.txt"
> GET /test.txt HTTP/1.1
> Host: fw.test
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx
< Date: Sun, 19 May 2019 16:51:55 GMT
< Content-Type: text/html
< Content-Length: 154
< Connection: keep-alive
< Location: https://fabian-franz.eu/test.txt
<
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>


Worked for me! I had misundestood how to properly set this up.
Thank you for the help!!