Serve different pages with the same URL - url

I have two HTML pages. a.html is a mobile version of my site, b.html is the desktop version. How can I set these two pages to use the same URL?

It is very easy and I have already implemented this. You can check out wittyfeed.com, it serves different files for mobile and desktop.
It can be done as follow,
http {
....
map $http_user_agent $ind {
"~*mobile" a.html;
default b.html;
}
server {
...
index $ind;
...
location / {
try_files $uri $uri/ $ind;
}
}
}
The above code runs as, it sets value for variable $ind, as a.html if the request comes from mobile or b.html otherwise. Then, accordingly it tries to fetch the file. :)
Edited w.r.t. comment below, *not tested,
http {
...
map $http_user_agent $proxy_addr {
"~*mobile" test-qa.firebaseapp.com/mobile;
default localhost:8080/test/jsp/index.jsp;
}
server {
...
location /test {
proxy_pass $proxy_addr;
}
}
}

Related

JavaScript not executing with non-root nginx proxy_pass location

I'm trying to set up nginx to reference a docker container. Ultimately, I'd like to configure nginx to direct traffic to multiple docker containers, but I'm stuggling to get the location working.
This works:
http {
server {
location / {
proxy_pass http://172.20.0.10:8000/;
}
}
}
When using the above configuration, I can go to http://<server-ip-address>/, and see the website.
However, when I try to modify the location, like so
http {
server {
location /path/ {
proxy_pass http://172.20.0.10:8000/;
}
}
}
I'm able to still able to load the page, but it doesn't appear to be executing the JavaScript. I get a white background, and can see the page HTML, but none of the info that would be populated by JavaScript shows up.
(I do have JavaScript enabled in my browser. I have tried both Chromium and Firefox, and both have the same issue.)

Ruby 6.1 with Nginx - restrict direct access to specific files

I am using Nginx server with a RoR webapplication (version 6.1.4).
I have several audio files around the site and I want to restrict direct access to them.
The page have publicly accessible part and another for registered members. Here they can upload and share mp3's through the platform.
I added the following lines to nginx configuration:
location ~* \.mp3 {
valid_referers server_names;
if ($invalid_referer) {
return 403;
}
}
This one is working fine for the hardcoded audios and prevents direct access.
But if someone logs in and traces the html for the sourcefiles of uploaded audios, it is still accessible for them. I am using ActiveStorage for managing file uploads and it is on a s3 storage.
Appreciate any ideas!
Did not check, but customize this and try:
location ~* \.mp3 { # location for .mp3 files
if (-f $request_filename) { # if file actually exists
return 301 $scheme://$server_name/RoR_APP_URI_with_auth_check/$request_uri;
}
}
This could be a good opportunity to use the Proxy design pattern. You could create a controller/action that handles user authentication and then either redirects to the appropriate url or directly serves the file using send_file. There are pro's and con's to this approach but it would be a way to authenticate requests and restrict access to paywalled content.
Here's an example from a production app I'm working on:
def avatar_proxy
if Rails.env.development?
tmp_file = open(current_user.avatar.path)
else
url = current_user.avatar.url
tmp_file = open(url)
end
send_file tmp_file, :type => current_user.avatar.content_type, disposition: 'inline'
end

Nagios/Icinga - find host by custom variable with cmd/status.cgi

I'm looking for solution how to get a hostname from Nagios/Icinga by searching it by custom variable with cmd/status.cgi.
I have a custom variable with unique specific IDs on every host. I have to get the hostname by searching on ID. There is a documentation for CGI commands but I could not find the needed functionality: https://icinga.com/docs/icinga1/latest/en/cgiparams.html
UPD: I am using python for CGI requests. Maybe there is also a library to do that.
Does anyone know, if it is possible?
For Nagios at least, this is possible. You can call the host details on the objectjson.cgi for a hostgroup and in your result.json(), you would have the custom_variables for each of the hosts. With that, you can map an ID to the hostname.
make your request to https://<your_url>/nagios/cgi-bin/objectjson.cgi?query=hostlist&details=true&hostgroup=<your_hostgroup>
{...
"data": {
"hostlist": {
"<host1>": {
....
"custom_variables": {
<custom host variables dict>
},
"<host2>": {
....
}
}
}
}
untested! using python's requests module:
hostlist = result.json().get('data').get('hostlist')
id_map = {hostlist.get(host).get('custom_variables').get('your_id_key'):host for host in hostlist.keys()}

Changing swagger-ui server variable at runtime

Using an openapi v3 configuration I have a server variable called 'hostname' that is used to build the url, like:
...
servers:
- url: http://{hostname}/api
variables:
hostname:
"default": "some default here"
....
At runtime I'd like to be able to change the 'hostname' server variable. I've found the UI element on the page,
<input type="text" value="some default here" data-variable="hostname">
Changing the variable by editing the input field works fine, but changing the input field via jQuery isn't working, even when triggering the "change" event after setting the value, the value reverts when expanding one of the api sections. I also tried triggering the keyup/keydown and focusin/focusout events to better simulate how a user would change the field.
Is there a more swagger-ui approach to changing this value through an exposed call? I've looked through window.ui but its kind of cryptic.
I have an api.yaml file hosted on different IoT devices. Each device will have a different hostname based on its configuration. When the page is loaded I'm trying to use javascript to set the 'hostname' server variable to be window.location.hostname, for example via javascript.
You can simply specify a relative server url – it will be resolved relative to the location of the OpenAPI definition file.
For example, if you have
servers:
- url: /api
and the API definition file is at
http://foobar/spec/api.yaml
then the base url for API calls will be resolved to
http://foobar/api
You can alter the spec json before it render by writing a plugin
const ui = SwaggerUI({
// ...
plugins: [
// add a plugin to alter spec before it rendered
{
statePlugins: {
spec: {
wrapActions: {
updateJsonSpec: function(oriAction, system) {
return (spec) => {
// change spec.servers here to add new entry, use concat to put it as the first & default one
spec.servers = [{url: someDynamicUrlInRuntime}].concat(spec.servers || [])
return oriAction(spec)
}
}
}
}
}
}
]
// ...
})
Adapting the answer from nevermind for use with SwaggerUIBundle as used in the index.html from swagger-ui:
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
// Custom plugin that replaces the server list with the current url
function() {
return {
statePlugins: {
spec: {
wrapActions: {
updateJsonSpec: function(oriAction, system) {
return (spec) => {
spec.servers = [{url: window.location.origin}]
return oriAction(spec)
}
}
}
}
}
}
}
],
This way the index.html can be served from the backend service itself and will refer only on its own url.

Laravel, Image get from storage display correctly but console return status code 404

I'm having this problem where by image get from storage shows in page correctly but console shows status code 404 as shown in the image below
I have noticed something weird..
This method works fine.
Route::get('/image/view','ImageController#display');
public function display(Request $request){
$path = $request->path;
$cacheimage = Image::cache(function($image)use($path){
$file = Storage::disk('akl')->get($path);
$ret = $image->make($file);
return $ret;
},10); // cache for 10 minutes
return Response::make($cacheimage,200)->header('Content-Type','image/jpeg');
}
<img src="/image/view?path={{ $img1->path }}">
But this method hits error.
Route::get('/photo/{params?}','ImageController#get')->where('params','.*');
public function get(Request $request){
$url = $request->fullUrl();
# remove domain name & photo string
$path = substr($url,strlen(url().'/photo'),strlen($url));
#works fine
$file = Storage::disk('akl')->get($path);
return Response::make($file,200)->header('Content-Type','image/jpeg');
}
<img src="/photo{{ $img1->path }}">
even the code as simple as like this.
Route::get('/storage/{disk}/{params?}',function($disk,$params){
$path = public_path('storage/') . $params;
$file = \Intervention\Image\Facades\Image::make($path);
return $file->response('jpg');
})->where('params','.*');
Found myself a solution although its not my preferred way...
Route::get('/storage/{disk}/{params?}/something',function($disk,$params){
$path = public_path('storage/') . $params;
$file = \Intervention\Image\Facades\Image::make($path);
return $file->response('jpg');
})->where('params','.*');
so the url looks like this...
http://domain.com/storage/disk/picture.jpg/something
If any out there have other solution pls do share to me.TQ!
I got this problem too. Wasted much time on the changing and testing the laravel code.
The problem lies in the webserver configuration. In my case nginx. Check your web server configuration. After I apply following location block into my nginx config, no 404 anymore.
#protected resources to be handled by laravel
location ^~ /storage {
try_files $uri $uri/ /index.php$is_args$args;
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
}

Resources