Running several sites off the same code except a config - nginx

I have the same code which will be used for several sites. In the Nginx config I wanted to have all the sites point to the same code folder.
I think this should work. The only catch is that I want each site to use a different config file.
How could something like this be achieved? Surely I wouldn't need to duplicate all the websites code just to have each one have a different config?

What language are you scripting in? Most languages will have a way to examine the incoming request. From this you could extract the domain name from the request and base which conf file you load based on the name using an if or switch statement.
You could also use a get variable for example www.domain.com/index.html?conf=conf1.conf. Then in your controller you'd need to look at that git variable to determine which conf file to load.
Either of these solutions should be easy to find in the docs for you scripting language.

Related

Change nginx Root Directory at Scheduled Time

Let me quickly preface this by saying that I'm not a server administrator, so it's quite possible that I may be asking the "wrong" questions here.
I have a situation where we have a domain that will serve static files (HTML, images, etc.) that are configured and built by an existing, separate application. At certain scheduled datetimes, we will need the content of the sites to change to a different set of static files.
Since the files will all be prepared ahead of time, I was wondering if it was possible for nginx to be able to "switch" the root directory to direct traffic to the appropriate place based on these scheduled datetimes.
So if there were a series of directories maybe like this:
/www.example.com-20160701000000/content/public
/www.example.com-20160708000000/content/public
/www.example.com-20160801120000/content/public
And then the configuration would say that from 1 July 2016 00:00:00 through 7 July 2016 23:59:59, the site root for www.example.com would be /www.example.com-20160701000000/content/public, and so forth.
Some other things I've looked into:
Some form of middleware like PHP, but I want to avoid this for portability.
SSI doesn't really seem like an option. It seems like I'd have one root directory and in the index.html, the contents would be something like <!--# include file="www.example.com-$datestamp/content/public/index.html" --> but it seems like I'd have to do this for every page maybe? I'm also not sure how it would work if the page names are different between versions.
A cron job or something else that either moves files or edits a file at the appropriate time, this just seems like a really bad potential failure point.
So tl;dr, can nginx be configured in some way to have root directories that are active for a domain at different scheduled times? Or is there a better approach to this problem that I'm not aware of?
nginx has variables called $time_iso8601 and $time_local which you could use to construct a dynamic root. See this document for details.
One approach would be to construct your rules as a map and set the root directive appropriately using the mapped variable or named captures. See this document for details.
I tested the concept using this:
map $time_iso8601 $root {
default /usr/local/www/test;
~^2016-06-2[0-9] /usr/local/www/test/test-20160625;
}
server {
root $root;
...
}

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.

Organise files in SonataAdminBundle: what goes where?

I have regrouped the following information from a few examples in the SonataAdminBundle documentation. Please correct me if there are some errors, but here is what I get in the case of a BlogBundle:
As you can see, in general, each bundle contains both frontend and backend classes.
It seems very messy to mix both frontend and backend in the same folders somtetimes (see Controllers), but to be honest I can't think of an other way...
I actually started handling backend in a separate bundle but then realised that it was also too messy.
So in practice, do people really follow this architecture? Is this the only/best way to handle backend when using SonataAdminBundle?
This beautiful post here is using a different approach...any ideas what I should do to make sure the code doesn't get too messy.
Simple: use folders within locations of mixed content. I put frontend components directly in their respective folders, and add Admin folders for backend files.
You can refer to e.g. a controller in the Admin subfolder like this BlogBundle:Admin\Concert:index, essentially the same works for templates.
On configuration, you could create a config-frontend.yml and a config-backend.yml file, then include it in the original config.yml file. I don't do that though.

Local Coldfusion server for multiple domains / URLs

I want to get CF9 with IIS 7 setup locally to run with multiple domains.
I have read this one but it doesn't say anything about the actual setup.
Need help with multiple URL setup on local CF9/Jrun install
I setup IIS so that I can start 127.0.0.1/domain1/index.cfm The page loads properly
but all subsequent links fail with
Could not find the included template: /_/definesession.cfm
But I see the file when typing in file:///C:/InetPub/wwwroot/domain1/_/DefineSession.cfm
The files are there but apparently the server is only reading the directory correctly
If I test http://127.0.0.1/domain1/_/BrowserDetect.cfm with no includes just a self contained file it executes properly.
The path in IIS is set to C:\InetPub\wwwroot\domain1
The bindings hostname is just domain1 no TLD
Also the second instance 127.0.0.1/domain2/index.cfm is working correctly. And here as well including subdirectories is failing.
ADDITIONAL NOTES: (added 1/3/12)
I guess it has to do with the CF mapping. I now moved the code to c:\coldfusion9\wwwroot\domain1_... and it sort of works.
In other words I start the program here: C:\inetpub\wwwroot\domain1\index.cfm Inside that index is for instance
But it executes the file located here: c:\coldfusion9\wwwroot\domain1_\definesession.cfm Just couldn't find anything in the web about mapping a local CF9 to that situation. Any idea??? –
You might have a ColdFusion mapping for "/" that needs to be adjusted.
OK I fixed it. There were multiple issues:
For whatever reason there were some issues with IIS and I had to reinstall it.
I had to make sure 9.0.1 was installed
I had to run Webserver Configuration Tool multiple times to actually get the Handler Mappings in order.
http://127.0.0.1/domain1/ was wrong - it must be http://domain1/ etc.
I forgot to add the domains to the host file on the machine - stupid me
I had to redesign my mapping to avoid overlaps between domains (i.e. mapping CFCs to /_/cfc/ on all domains needed to have different mapping names.
Now I have several different domains on my local machine and they work just fine.

How to MapPath in an IIS7 Rewrite Provider?

I am using the rather excellent IIS7 Rewrite module (V2), and want to create a custom RewriteProvider that rewrites differently depeneding on whether the physical file exists.
I have successfully created a provider, as in this tutorial:
http://learn.iis.net/page.aspx/804/developing-a-custom-rewrite-provider-for-url-rewrite-module/
However, really need to be able to map the url to a physical path - I would normally do this via HttpContext.Current.Server.MapPath() or HostingEnvironment.MapPath(), but it looks like the HttpContext has not been initialised (at least within the current App Domain - since the ReWrite module is native code, I'm having difficulty working out where I can get this information).
I don't really want to have to resort to creating my own rewrite module to get around this problem - anyone have any clues for me?
Thanks!
Mark.
You will not be able to get to it using those APIs since the code runs in a different AppDomain than the one ASP.NET is using.
The only way I can think to make this work is to pass the right Server Variable that includes the physical path to your extension and do a Path.Combine() yourself.
So assuming you have an extension called YourProvider that you are calling somehow like this:
{YourProvder:{URL}}
You can do:
{YourProvder:{APPL_PHYSICAL_PATH}|{URL}}
You can now get the physical path and the URL separated by a pipe | , make sure to pass the Physical Path first since the URL is in the control of external users you do not want them to trick you into getting a different physical path.

Resources