OPNsense Forum

English Forums => General Discussion => Topic started by: routingMike on May 09, 2019, 04:47:58 pm

Title: [Solved]Using the nginx plugin to redirect non-www sites to www
Post by: routingMike on May 09, 2019, 04:47:58 pm
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?
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: fabian on May 09, 2019, 05:22:54 pm
URL Rewrite is not appropriate?
see screenshot.
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: routingMike on May 11, 2019, 07:20:13 pm
I naively thought that was specific for paths. Didn't even consider it might work for domains. I'll try it out.
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: fabian on May 11, 2019, 07:28:38 pm
If you separate the hosts, it will very likely work.
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: routingMike on May 15, 2019, 02:32:16 am
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.

Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: fabian on May 19, 2019, 06:53:26 pm
Example:

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

Result:

Code: [Select]
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>
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: hbc on May 20, 2019, 10:23:40 am
Hello Fabian,

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

If I use "permanent" I get:
Code: [Select]
rewrite / https://server.name permanent;
If I use "redirect" I get:
Code: [Select]
rewrite / https://server.name redirect;
What I need is:
Code: [Select]
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.
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: fabian on May 20, 2019, 07:12:11 pm
301 works using permanent:

https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

rewrite is just the name of the nginx directive.
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: routingMike on May 21, 2019, 07:00:29 pm
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.
Title: Re: Using the nginx plugin to redirect non-www sites to www
Post by: routingMike on May 22, 2019, 02:14:27 am
Example:

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

Result:

Code: [Select]
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!!