How to transform (rewrite) part of URL in nginx - regex

I'm trying to redirect traffic to our new server but the old server (Mac OS) used case insensitive file names while the new (embedded) one uses Linux (case sensitive).
My problem is to redirect traffic from:
http://server.com/NEW/variable_url
to
http://server.com/new/variable_url
(note the lower 'new').
I would like to be able to do this for nginx without using perl or lua or other modules as this server is running in an embedded environment.
So far I tried:
location ~* ^/new/ {
access_log /var/log/nginx/new.log combined;
rewrite ^/new/(.*)$ $1 permanent;
}
without success.

Solved it myself. I was missing the root directive which points to the root of the web server. So for anyone interested the solution is:
location ~* ^/NEW/ {
root /etc/nginx/html/;
rewrite /NEW/(.*)$ /new/$1 permanent;
}

Maybe something like this?
location /NEW {
rewrite ^/NEW/(.*)$ /new;
}

If you want to redirect traffic from the specific link /NEW/exampe1, you could try
location =/NEW/example1
{
rewrite ^ /new/example1 last;
}

Related

Nginx change request header value conditionally

How to change request header value conditionally depending on another header value in nginx reverse proxy?
You can use the nginx ngx_http_map_module.
A good example is here - Mapping Headers in Nginx.
Basically you need to map the requested header (I used the from_header in the configuration below)to the new header (to_header in the example) and later use proxy_set_header.
map $http_from_header $to_header {
default a;
value_1 b;
value_2 c;
}
server {
....
location / {
include proxy_params;
proxy_set_header To_Header $to_header;
}
}
Nginx takes any HTTP headers, lower-cases them, and converts dashes to underscores. They become accessible as variables starting with $http_.
That way you should get what you need.
Good luck

nginx location block failing due to rewrite

I'm trying to get the following code to work:
location = /test-site/wp-login.php {
allow 192.168.0.56;
include /etc/nginx/fastcgi_params;
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/test-site.com/html$fastcgi_script_name;
deny all;
}
The problem is, due to the multisite configuration on Wordpress, there are 3 rewrites that need to be performed in order to get to the right subsite. Even with the rewrites, the above block doesn't work because with the rewrite, it's stripping out the /test-site/ part.
Instead, it matches this and uses this nginx configuration:
location = /wp-login.php {
#allow 192.168.0.0/16;
allow 192.168.0.56;
include /etc/nginx/fastcgi_params;
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/test-site.com/html$fastcgi_script_name;
deny all;
}
I've had some success changing the rewrite to ignore /test-site/ and keep it, but then it changes the $fastcgi_script_name variable to be /var/www/site.com/html/test-site/wp-login and the /test-site/ directory doesn't exist because it's actually a subsite.
If I take out the $fastcgi_script_name variable and hardcode /var/www/site.com/html/wp-login.php to be there instead, it gets me to the site, but it doesn't set the variable back to the proper value to find and of the image or style files.
Anyone have any experience with this? I'm basically trying to allow everyone access to wp-login.php on one subsite, but block them from any of the other subsites in my wordpress environment.

MoovWeb: Redirect desktop user to desktop site if they try to visit m.site.com

Using MoovWeb for a large eCommerce client. They want to block desktop users from visiting the mobile site. So basically, if a user is using a desktop browser and tries to visit (m.site.com) they will be redirected to (site.com).
Must do this in Tritium (most likely near the top of main.ts), because by the time JS runs, we'd be loading the site twice (once in m. then once again in www.)
I'm hesitant to go the route of using Regex to check $user_agent, because if we don't match EVERY POSSIBLE mobile agent, and the user goes to m. on their unmatched phone, they will get an endless redirect (m. > www. > m. > www. > m. > ...). I know there are very detailed Regex strings for user agents, however as detailed as they are, the only way we would find out that some phone out there is no longer matched is through loss of sales, which is not an option.
Here was my original Tritium test attempt, which causes redirect for mobile users that don't use Android or iPhone:
match($host,/^m\./) {
match($user_agent) {
not(/(Android|iPhone)/) {
$newHost = $host
$newHost {
replace(/^m\./,"")
}
$redirect = "http://"+$newHost+$path
export("Location",$redirect)
}
}
}
Moovweb provides redirection both client side and server side out-of-the-box. It's recommended that you implement server-side redirection which has the least amount of roundtrips.
Here is the official documentation:
https://moovwebconfluence.atlassian.net/wiki/display/DD/Mobile+Redirection#MobileRedirection-Server-SideRedirection
Best,
Juan C.
match($host,/m./) {
$newHost = $host
$newHost {
replace(/m./,"www.")
}
$redirect = "http://"+$newHost+$path
export("Location",$redirect)
}
Try this. Hope this will work you as this worked for me.

How can I use IIS URL Rewrite to prevent people from linking or leeching images on a website?

I came across the features of IIS and it notes that I can prevent inline linking or leeching. How can I implement such a rule?
You will need to use URL rewrite to point to a resource within a web application that will be able to identify the refferer before displaying the resource to the client which called the application.
IE, rewrite the following
/images/myPhoto.JPG
to
/getResource.PHP?resource=%2Fimages%2FmyPhoto.JPG
Then inside getResource.PHP
forgive me if this isn't quite right, my php is a little sketchy, but you get the idea
header('Content-Type: image/jpeg');
if(isset($_SERVER['HTTP_REFERER'])) {
if( /*test that it fits your criteria*/ true ) {
$imagepath= $_GET['resource'];
} else {
$imagepath= "/images/stopLeaching.JPG";
}
} else {
$imagepath= $_GET['resource'];
}
$image=imagecreatefromjpeg($imagepath);
imagejpeg($image);

How do you hide .git project directories?

Now that I have nginx setup I need to be able to hide my .git directories. What kind of rewrite would I need to stop prying eyes? And where in the server {} or http {} block would it go?
http {
server {
location ~ /\.git {
deny all;
}
}
}
This location directive will deny access to any .git directory in any subdirectory.
Note: This location block must be before your main location block, so that it can be evaluated first.
Hidden directories and files should never be web accessible. The general answer to your question is:
location ~ /\. { return 403; }
This denies access to .git, .svn, .htaccess and similar files in any subdirectory.
With other solutions I could download /.git/config and others, just /.git was protected. This one denies everything starting with a dot, regardless how deep the requested URL is:
location ~ /\.(.*)/?(.*)? {
return 404;
}
This will prevent someone from hitting the http://example.com/.git but if your working in a subdirectory like this http://example.com/example/.git it will fail. You really need:
location ~ .*/\.git {
deny all;
}