is it possible to cache a local file to Nginx - nginx

I don't know it is relate with Nginx or not.
What I want to do is, my frontend web server is only running Nginx and there is a (Nginx-lua-modules)Lua script which is retrieving some data from backend server and save to a file on frontend web server.
Now I need to cache that file on memory for 60min or 24hr or etc.. but there is only Nginx is running.
So, is it possible to cache from Nginx through Lua script.

Yes you can totally do it, but how you'd go about it totally depends on the size of the file. For something very small, a shared dictionary might be the easiest solution, but for something bigger you should maybe use something like memcached or redis instead.

Related

Moving a unix socket file for nginx and gunicorn

I am trying to update the location of a socket file for my nginx configuration and gunicorn.service files. I am trying to change it from My-Project/backend.sock to home/engineers/My-Project/backend.sock to maintain a consistency across my staging and public servers. I have created the backend.sock file in the desired location. When I try changing proxy_pass in my nginx configuration and/or the bind for my gunicorn.service file, I would get a 502 error when going to the domain. It's probably something simple, but I don't have much prior experience with these tools and I haven't found anything about this sort of thing specifically from all of my time googling. Could someone help me out on this one?

Is it possible and how to use NGINX for upload files?

I use NGINX for serving of static files which (it offloads the backend which serves only "dynamic" requests). But now I need to upload files to server. Is it possible to use NGINX for uploading too? Also it will be good if there is a way to set permissions of uploading files... How to use NGINX for uploading? Which protocol? Module? Some example configuration?
I found only WebDAV but its module looks outdated, also I never used WebDAV before so I am not sure how good is it (I mean performance). What is a typical solution? Do I need to write something, some plugin/module?
There is no built-in module to do the job. There is a third-party upload module.

What is the most optimized way to define server blocks in nginx config?

I have fifteen virtual hosts (servers) with separate log location for it. I am a bit confused about which would be the best option to write nginx config file for each. All server blocks in one file or a separate file for each server?
Which would be a more efficient way?
Nginx reads config once on start (or reload), so do whatever is more appropriate for you.
I would write related server blocks together in one file, and have one bunch of related servers per file.
Or have one file per server.
Or write them all in one file.
Efficiency is not effected by how you define the blocks in nginx. Thus, it would be same in the given case.
If there's some commonality between your virtual hosts configs, such as general SSL settings or denying certain types of requests, you may want to use includes.
I like to keep separate vhosts config files, it's easier to take one domain offline for maintenance for instance.

Accessing an environment variable across nginx w/ Lua and Rails

I'm implementing something like this to let one service allow access to separate upstream service in nginx.
Briefly: A Rails app sets an HMAC cookie, which is then checked by some Lua code thanks to an access_by_lua directive in nginx.
To generate and verify the cookie, both Rails and nginx-Lua must of course share a secret key. I've tried setting this up as an environment variable in /etc/environment.
To make the var available in Rails, I had to fiddle with Unicorn's init script a bit. But at least that script is contained within the project, and just symlinked into place.
Meanwhile, to get at the variable in Lua, I do something like this: os.getenv("MY_HMAC_SECRET"). But in order for Lua to have access to that when running under nginx, it must first be listed using the env directive in the main nginx config.
So now, I'm feeling like my configuration is being spread out all over the place:
in /etc/environment (outside my project)
in /etc/nginx/nginx.conf (outside my project)
in unicorn's init script
in my site's nginx vhost config
It's starting to seem a little ridiculous just to make a simple string accessible in multiple places...
Is there a simpler way to do this? Honestly, the easiest way I can think of is hardcode it in the 2 places I need it, and be done. But that sounds nasty.
Better to put it only in the two places it's actually needed, in the two respective configuration files, than in the global environment where every process has access to it, as you have it now.
I would use init_by_lua directive in your vhost config.
init_by_lua 'HMAC_SECRET = "SECRET-STRING"';
server {
# and so on
}
So you'll have you secret in two places, but both in your project (if I understand correctly and vhost config is in your project).
You can even use init_by_lua_file and make some efforts to read and parse that file in your unicorn init.

Put file at specific port on Localhost

I need to put a file at this address: http://localhost:51547/file.txt
What folder would I put it in on C:/?
Tony
If I understand your question correctly and you're trying to expose a file via the ASP.NET development server at that location (http://localhost:51547/file.txt) I'm afraid the answer is not quite to your liking.
Basically I don't think you can serve files from the root of the ASP.NET development server (i.e right after the localhost:port/ part - the port is automatically selected by ASP but you can also manually configure it). ASP.NET automatically creates a virtual application path right after localhost:por> and so you're most likely going to be limited to serving files from the virtual application folder. So, assuming you web application name is: "testApp", if you put a file called file.txt in the directory where you're storing the source code for "testApp" it will also become available when you're testing at: http://localhost:port/testApp/file.txt (note the testApp in between the host-name & port, and the file-name)
UPDATE
In light of you comment, here's something you could do. You could try to get a simple HTTP server installed on your development computer and have it serve files on a different port (say port 8000). In that case you would serve file.txt using this secondary HTTP server and it would be accessible at: http://localhost:8000/file.txt.
You could try to install Apache or use IIS which comes with Windows. For Apache, the quickest way to get it going would be to install a WAMP environment. You may also try Lighttpd, and also note that MySQL or PHP are not required at all if you're only serving static files.
Any folder you want. The port designator is specified in IIS itself.
http://support.microsoft.com/kb/149605
I suggest you that if you want to do a site(HTTP) is better to use the port 80 or to do a redirection for another port, remember too that localhoost is a loop back and also take a look at PortForward to check some ports that are used by specific applications and types of servers, and please improve your question.
Regards.

Resources