Welcome! Log In Create A New Profile

Advanced

Server redirect without rewrite rules in Nginx config

Posted by iBobík 
Server redirect without rewrite rules in Nginx config
November 09, 2015 08:20PM
Hello,
we are planning to use Nginx for static websites hosting generated by Jekyll. Sites are generated and deployed directly from GIT by "not trusted" users. "Not trusted" means they are trusted enough to publish something on their websites, but not enough to allow them change Nginx config of shared webserver.

Nginx has all pros, but only one negative point: Server redirects specified in "not trusted" file in website directory (like .htaccess from Apache).

Part of the discussion has happened yet in jekyll-redirect-from issue: https://github.com/jekyll/jekyll-redirect-from/issues/49 But there is missing the voice of someone who knows well Nginx.

Jekyll-redirect-from is plugin what generates HTML files in place of origin path with <meta http-equiv=refresh content="0; url=/new/path">. This plugin is well adopted in Jekyll community, so preffered solution is to use it (inc. extend it by some feature).

Is there some possibility to configure Nginx to send 301 redirect if he find file with meta refresh?

This will be propably very slow, so is there way to cache it?

Or to add feature to jekyll-redirect-from to generate something what Nginx can parse without everhead on not-redirected paths? (like discussed in the issue)
Re: Server redirect without rewrite rules in Nginx config
November 10, 2015 03:10AM
This can be done with Lua but a better question would be; is this file contents going to change alot and/or if the file exists does it need to continually be tested for its existence?

Any file IO is blocking, a 'one' time test/read and then cache it is not going to be a problem, if this needs to happen with every request users make this will slow things down alot.

Using a timer you could periodically test and update the cache but you would always have some delay after content changes.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Server redirect without rewrite rules in Nginx config
November 10, 2015 05:32AM
try_files might also do the trick but with fallback you might not be able to return contents and a 302, you'd have to experiment with Curl to see what happens, some examples:

http://serverfault.com/questions/323683/try-files-explained
http://serverfault.com/questions/203902/how-to-configure-nginx-to-give-existing-static-files-but-forward-requests-for-no
https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Server redirect without rewrite rules in Nginx config
November 29, 2015 12:59AM
Hello,
thank you for reply.

It should not check files every request. It can cache it.

Or will it be easier to use file with list of redirects in site root (aka .htaccess with Apache) in NginX config syntax? But it have to check if there are only safe rules, because users who can change it are not trusted. Server also should watch this file and reload config if it was changed.

I also prefer solution where I don't have to maintain custom code.
Re: Server redirect without rewrite rules in Nginx config
November 29, 2015 01:10AM
About the validation of config files:

I trust users enough to give them freedom on their site (hostname), but I don't give them permission to affect other sites in the same server.

So if there is some way how to "snadbox" included config file to only one hostname, I will use it. If not, it is enough to validate it if it contains only rewrite rules (if there is not chance to affect other sites by just rewrite rules).
Re: Server redirect without rewrite rules in Nginx config
November 29, 2015 05:14AM
nginx doesn't have dynamic config files other then reload them all, so you would have to resort to Lua for most parts (some can be done with nginx itself).
https://github.com/rstudio/redx is a good place to start
and https://github.com/mindreframer/nginx-lua-stuff for many examples

Store your user configuration items in a database and cache them when read, then use Lua to do the processing.

You can do it all with plain nginx and reloading nginx all the time but that obviously has its drawbacks.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Server redirect without rewrite rules in Nginx config
March 02, 2016 11:27AM
About blocking by reading files, does it matter when webserver loads files to be able to send them to client? Because on first load OS will usually cache the file in RAM, so it is not problem to load it twice.

So in this use-case it should not be slower to run Lua script on each request where it will check if file is redirect and then 301 or let webserver serve it.
Re: Server redirect without rewrite rules in Nginx config
March 02, 2016 03:15PM
Each check does a file IO which is blocking while its active, with thousands of users this can become a problem.
When you have 4 or more workers blocking becomes less of an issue.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Server redirect without rewrite rules in Nginx config
March 03, 2016 04:10AM
Is this blocking the same like regular load of file to send it by http? Am I correct regexp on file contents before it will be send will have no extra IO (or it will be very fast because of file system cache)?
Re: Server redirect without rewrite rules in Nginx config
March 03, 2016 05:07AM
It all depends on size, OS cache response and caching method, for example having files on a ramdrive does not block eventhough its IO (depending on quality of ramdrive software). Anything that does disk IO can block and usually does block, how its managed inside nginx (web caching, multiple workers) and outside (OS, storage speed/access). Regexp should not have a big impact.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Server redirect without rewrite rules in Nginx config
March 03, 2016 08:56AM
Thank you very much. Your replies are very helpful.
Re: Server redirect without rewrite rules in Nginx config
March 05, 2016 06:24AM
I need a lot of help I can't read my messages
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 173
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready