I have an url ex. www.something.com/cdn/util/sacle/180/180/...... I need to rewrite it to the www.something.com/cdn/_util/scale.php/180/180.... How can I do it in nginx? My solution now:
location /cdn/ {
rewrite ^/util/scale(.*)$ /_util/scale.php/$1 break;
}
But it is not a good solution for me, because I have some other rewrite rule:
location /serve {
rewrite ^/serve(.*)$ /_util/serve.php/$1 break;
}
location /upload {
rewrite ^/upload(.*)$ /_util/upload.php/$1 break;
}
location /delete {
rewrite ^/delete(.*)$ /_util/delete.php/$1 break;
}
location /copy {
rewrite ^/copy(.*)$ /_util/copy.php/$1 break;
}
location /move {
rewrite ^/move(.*)$ /_util/move.php/$1 break;
}
location /util {
rewrite ^/util/placeholder(.*)$ /_util/placeholder.php/$1 break;
rewrite ^/util/thumb(.*)$ /_util/thumb.php/$1 break;
rewrite ^/util/scale(.*)$ /_util/scale.php/$1 break;
rewrite ^/util/marker(.*)$ /_util/marker.php/$1 break;
}
I think it is possible in one line too. And the php got the parameters from the url. Which is now working, if I write the url like this:
http://cdn-staging.something.com/util/scale/180/180/dev-employer-images/iamge.jpg
It is create this:
http://cdn-staging.something.com/_util/scale.php/180/180/dev-employer-images/iamge.jpg
But it is under Apache, and I need to make it to nginx.
Related
I would like to do something like this:
location / {
if($http_x_my_header == 'some-value') {
proxy_pass "https://a/";
}
# else
proxy_pass "https://b/";
}
Because I know that using "IF" in NGINX can be kind of problematic: How would I solve this following "best practices"?
I try to check based on the request URI and OIDC roles in HTTP header if the user has access to a specific directory.
Currently I use statically defined
server {
location /tool/ {
location /tool/abc/ {
if ($http_x_auth_roles !~ '(^|,)MY_DEFINED_ROLE_FOR_abc(,|$)') {
return 403;
}
}
# and so on for every single tool
}
}
I tried to switch to a generic variant but the matching does not work:
map $request_uri $expected_role {
default 'DOES_NOT_MATCH';
'~^/tool/(.+)/' 'MY_DEFINED_ROLE_FOR_$1';
}
server {
location /tool/ {
if ($http_x_auth_roles !~ $expected_role) {
return 403;
}
}
}
I checked the content of $expected_role and it's showing exactly what I want, but it never matches. Is there some kind of work around to achieve what I want?
I've got the following NGINX configuration:
- location /concepts {
auth_basic "off";
if ($http_accept ~ 'application/json') { set $isapirequest "true"; }
if ($http_accept ~ 'application/ld\+json') { set $isapirequest "true"; }
if ($http_accept ~ 'application/hal\+json') { set $isapirequest "true"; }
if ( $isapirequest = "true" ) { proxy_pass http://127.0.0.1:5315/search/concepts/; }
if ( $isapirequest != "true" ) {
rewrite ^/concepts$ /concepts/ redirect;
rewrite ^(.*)$ /blah$1 last;
}
include add_cors_headers_OPTIONS_HEAD_GET_PUT_DELETE;
}
The error that I'm getting is:
\"proxy_pass\" cannot have URI part in location given by regular expression, or inside named location, or inside \"if\" statement, or inside \"limit_except\"
Can you guys think of any way on NGINX to achieve the above without using an "if" statement?
Your last two if statements are mutually exclusive, so one of them can be eliminated, which would remove the error you are getting.
This document indicates which statements should be used inside an if block within a location context. You might consider using a map to replace all but one of the if statements.
For example:
map $http_accept $redirect {
default 1;
~application/json 0;
~application/ld\+json 0;
~application/hal\+json 0;
}
server {
...
location /concepts {
auth_basic "off";
if ($redirect) {
rewrite ^(.*)$ /blah$1 last;
}
proxy_pass http://127.0.0.1:5315/search/concepts;
include add_cors_headers_OPTIONS_HEAD_GET_PUT_DELETE;
}
...
}
The rewrite ^/concepts$ /concepts/ redirect; statement can be moved to the location that processes the /blah/concepts URI, and rewritten as rewrite ^/blah/concepts$ /concepts/ redirect;.
See this document for more.
I am trying to block or redirect to 400 all the requests which does not contain certain strings or keyword in the uri. I am looking for a solution using nginx.
Specifically on "NOT containing strings". If it is not possible with ! (NOT) matching, is there any alternative.
I would recommend using a map:
map $uri $bad_request {
# start by assuming it's a bad request
default 1;
# if any of the following match, clear the $bad-request variable
~a-required-string 0;
~another-required-string 0;
# ...
}
location / {
if ($bad_request) {
return 400;
}
}
Well, you could try something like this:
location / {
if (condition_1) {
break;
}
if (condition_2) {
break;
}
...
if (condition_n) {
break;
}
return 400;
proxy_pass http://backend;
}
If any of condition_1, ... condition_n match, break keyword prevent return 400 and request goes to proxy_pass. If all conditions fail return 400 will make nginx to response 400 Bad Request error.
Conditions could be anything that if supports. E.g. if ($args_pass = 1) or if ($http_user_agent ~ MSIE), etc.
I wrote a custom non-mvc component (because i don't know how to make mvc component and now it's grown is size so changing it will take more time which is not possible for me) 'com_group' for my client.
Everything is fine but when it comes to sef urls it gives me urls like mysite/component/group/home and in non sef urls like mysite/index.php?option=com_group&view=home but client wants to remove component word from urls.
I also made a router for my component which remove every parameter correctly but it does not remove component. I also made a menu item for it but it didn't helped me.
Here is my router.php
<?php //error_reporting(E_ALL);
defined ( '_JEXEC' ) or die ();
jimport('joomla.error.profiler');
function GroupBuildRoute(&$query){
$segments = array();
//$query['Itemid'] = 201;
if( isset($query['view']) )
{
$segments[] = $query['view'];
unset( $query['view'] );
};
if( isset($query['pin']) )
{
$segments[] = $query['pin'];
unset( $query['pin'] );
};
return $segments;
}
function GroupParseRoute($segments){
$vars = array();
$app =& JFactory::getApplication();
$menu =& $app->getMenu();
$item =& $menu->getActive();
$items = $menu->getItems('component', 'com_group');
if (!isset($query['Itemid']))
$query['Itemid'] = 180;//$items->id;
// Count segments
$count = count( $segments );
//Handle View and Identifier
switch( $segments['0'] ){
case 'group_page':
$vars['view'] = 'group-pages';
break;
case 'group':
$vars['view'] = 'home';
break;
case 'folow':
$vars['view'] = 'follow';
break;
case 'start':
$vars['view'] = 'start-group';
break;
case 'group_eventplan':
$vars['view'] = 'group-event-plan';
break;
case 'group_member':
$vars['view'] = 'group-members';
break;
case 'manage_subscription':
$vars['view'] = 'manage-subscription';
break;
case 'group_msg':
$vars['view'] = 'group-message';
break;
case 'group_invite':
$vars['view'] = 'invite-friends';
break;
case 'other_group':
$vars['view'] = 'other-groups';
break;
case 'groupinfo':
$vars['view'] = 'group-info';
break;
case 'home':
$vars['view'] = 'group-home';
break;
}
if (!isset($item)) {
$vars['view'] = $segments[0];
$vars['pin'] = $segments[1];
return $vars;
}
if($count==2){
$vars['view'] = $vars['view'];
$vars['pin'] = $segments[1];
return $vars;
}
return $vars;
}
I also want to replace group_msg to group-message also so that new urls should be component/group/group-message/ not component/group/group_msg
I tried Remove component part from sef url, menu item not completely but didn't helped me.
Finally i managed to solve the question by myself. I am mentioning steps taken to help others. (Please write me if you want source code to use it with your project)
Step 1: add a menu item (Joomla Administrator -> Menus ->Menu Manager->Add new Item)
Step 2: The newly added menu item will be provided an id, add this id to all urls of your component like this.
index.php?option=com_your_component_name&Itemid=newly_generated_id
that's it.
The first option I would try is sh404SEF
Good luck!
EDIT
If SEF extensions are not an option for you, perhaps using .htaccess and redirect, something like the following :
RewriteEngine On
Redirect /component/group/home /something-else