Processing redirection string inside the NGINX script, is possible? - redirect

I am looking for a "full NGINX solution", withou intermediary redirection... But I need some external processing by "my Name-Resolver", as illustred with this execute fantasy:
server {
server_name resolver.mydomain.com;
execute xx = http://localhos:123456/myNameResolver/$request_uri;
rewrite ^ http://www.adifferentdomain.com$xx? permanent;
}
So, is possible to do something like this? perhaps using a kind of fastcgi_pass but only to return a string, not to bypass all HTTP resolution.

Well, you can use HttpLuaModule, which can execute commands and store them in variables if needed.
location / {
server_name resolver.mydomain.com;
# Get response via lua script.
set_by_lua_file $xx 'resolver-script.lua' $request_uri;
rewrite ^ http://www.adifferentdomain.com$xx? permanent;
}
You just need a Lua script to do your request for you, try something like this using your $request_uri as arg[1], because it is being considered as a command line argument

Related

Nginx folder redirection without changing the url

I want to redirect an old path to a new one without changing the url and keeping the suffix of the url for the redirection:
dev-osm.blah.com/NYC/v2/site/links?key=a12345
redirect to
dev-osm.blah.com/NYC/v1/site/links2?key=a12345
server {
server_name dev-osm.blah.com
.
.
location ^~ /NYC/v2/site/links {
rewrite ^ /NYC/v1/site/links2
}
Use something like the following in your server config
location /NYC/v2
proxy_pass: /NYC/v1/

Nginx - How to redirect a directory to a file that its path contains the directory?

I'm new to Nginx and I'm trying to redirect a directory to a file, here is basically what I'm trying to do:
When entering this link:
http://localhost:8889/dir
go to this link instead:
http://localhost:8889/dir/path/to/the/file/index.html
As you can see the directory is part of the file path which I'm trying to redirect to. Plus there is already a location block in the config file to redirect that directory to the proper location:
location /dir {
root /opt/dir;
}
My first attempt was to use the rewrite directive as I saw from this blog(https://jeffsebring.com/2012/nginx-301-redirects/):
if ( $request_filename ~ dir ) {
rewrite ^ http://localhost:8889/dir/path/to/the/file/index.html permanent;
}
But the page says it has a redirect loop, which I believe is the conflict with the old location block.
Then I tried to add another location block as I saw from here(nginx rewrite virtual directory to file):
location /dir {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
But after reloading config file, Nginx told me there is already a location block with the same directory exists.
So my question is is there any way I can do this? or it is not even possible?
Thanks!!
Found the solution.
I used "return" to return the full hard-coded url instead rewrite the current one:
location ~* /dir$ {
return http://localhost:8889/dir/path/to/the/file/index.html;
}
And also this rewrite solution works as well:
location ~* /dir$ {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
The problem I had was actually caused by cache. I was actually reloading the page from cache so I could not see the result after changing nginx config file, and I resolved this by checking the "Disable cache" option in developer tools in Chrome.

Nginx: How can I provide variable for paths in nginx configuration?

I have a web application which I test locally and deploy on EC2 instance
I am using local nginx configuration which looks like as
location /static/ { alias /home/me/code/p/python/myapp/static/;
# if asset versioning is used
if ($query_string) {
expires max;
}
} location /templates/ { alias /home/me/code/p/python/app/templates/;
# if asset versioning is used
if ($query_string) {
expires max;
}
}
On EC2 instance, the only thing that would change is the path, e.g.
/home/me/code/p/python/myapp/static/ to /User/ubuntu/code/p/python/myapp/static/
To make this happen I change the configuration to look like
~/code/p/python/myapp/static/
but this didn't work, it shows the path
/etc/nginx/~/code/p/python/myapp/static/
which is not right
Question
- Is it possible to include environment variables in nginx conf?
What I want
- Nginx conf, which can read variables on specific machines to create paths, so that I don't have to change it per machine and code is reusable
Thank you
Two ways of doing this:
As suggested above, symlinking is a really good way of making paths match on machines, while keeping code in one place. A symbolic link basically is an alias; if /link is a symlink for /file, when you ask for /link, you'll get /file.
ln -s /file /link
Using include statements. In nginx, you can include variables.conf;. E.g.
nginx.conf:
include variables.conf
...
http {
listen $port;
...
}
variables.conf:
set $foo "Something";
set $bar "Else";
set $port 80;

In Nginx, how do you configure a split redirect?

I want to configure Nginx so that this URL:
http://www.example.com/x/...
redirects here:
http://x.example.com/...
and everything else (not http://www.example.com/x/...)
http://www.example.com/...
remains as-is:
http://www.example.com/...
How do you configure Nginx to do this?
you do os by setting up 2 location blocks inside your server config: 1 with the redirect, and 1 with the default stuff that matches anything else like so:
location / {
# directives needed to serve the regular stuff
}
location /x/ {
rewrite ^/x/(.*)$ $1 last;
}

Nginx location, alias, rewrite, root

I'm serving /foo/bar/ by way of proxypass and want to continue doing so. However, I would like to serve /foo/bar/baz.swf statically from say /var/www/mystatic/baz.swf and so forth.
I was hoping that I could do something like
location /foo/bar/(.*) {
alias /var/www/mystatic/;
}
location / {
proxy_pass ....;
...
}
And /foo/bar/ would go to the application server while /foo/bar/(.*) would be served statically.
the docs say that I can't do this and need to use a combination of root and rewrite: http://wiki.nginx.org/NginxHttpCoreModule
Adding to the complication, I would like to continue using the ancient, unsupported 0.5.33. Any help would b greatly appreciated.
Edit: moving forward, someone suggested using root instead of alias. But, it doesn't seem that I can use any regex on the location directive with my version? Here, /foo/bar/baz.swf is served by the proxy_pass! I have the file at /var/www/foo/bar/baz.swf.
location /foo/bar/(.+) {
root /var/www/;
}
You can do this; but it's slightly esoteric. Try using:
location ^~ /foo/bar {
alias /var/www/mystatic/;
}
location / {
proxy_pass ....;
}
These options are documented on the Wiki http://wiki.nginx.org/NginxHttpCoreModule#location
location = /foo/bar/baz.swf {}
will clear clear any options set to /foo/bar/baz.swf. So you can leave it
where it is as the proxy options will not be used.
You can:
# mkdir /var/www/foo
# mv /var/www/mystatic /var/www/foo/bar
then use this config:
location ~ ^/foo/bar/(.+) {
root /var/www/;
}