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

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;
}

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.)

check website url status using powershell version 2.0

I need to check if URL is working properly or not using "PowerShell Version 2.0"
I found this script over internet, but it is not wokring fine for wrong URL's. It should go in else loop for wrong URL's as well as print website code. And I am not able to pass credentials in this script.
e.g. for www.google.com(correct URL) status code should be 200but
for www.gfjgugy79rt9(Wrong URL) status code should be something like 404
script I found over internet for powershell version 2.0:
# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()
# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
Write-Host "Site is OK!"
}
Else {
Write-Host "The Site may be down, please check!"
}
# Finally, we clean up the http request by closing it.
$HTTP_Response.Close()
In PowerShell higher than 2.0 you should use try ... catch ... finally because this code fire exception when the URI is no conform or when the adress part is not solvable by the DNS :
try {
# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()
# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
Write-Host "Site is OK!"
}
Else {
Write-Host "The Site may be down, please check!"
}
}
catch {
Write-Verbose $_.ScriptStackTrace
Write-Verbose "Ligne $($_.InvocationInfo.ScriptLineNumber) : $($_.exception.message)"
}
finally {
# Finally, we clean up the http request by closing it.
$HTTP_Response.Close()
}
In PowShell 2.0 you just have to put a Trap code at the beginnig of the scope (function, script) where you want to catch these exception :
trap
{
Write-Verbose $_.ScriptStackTrace
Write-Verbose "Ligne $($_.InvocationInfo.ScriptLineNumber) : $($_.exception.message)"
Write-Verbose ([datetime]::Now)
return
}

Serve different pages with the same 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;
}
}
}

How can I achieve a uwsgi_pass with openresty's Lua plugin?

I need to perform some semi-complex logic on an incoming request that matches a particular location. In short, for all urls conforming to location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$", the following needs to take place:
Check if admin cookie is present. If so:
Rewrite URL (/<uuid> -> /mod/<uuid>)
Perform uwsgi_pass
Else:
Perform lookup in postgres for an entry matching the UUID
Search the entry for a usable redirect URL
Redirect the client to the selected URL
All of this is fairly straightforward using content_by_lua_block, except for the uwsgi_pass bit; Google has proven most unhelpful in this endeavor...
How can I perform a uwsgi_pass in a content_by_lua_block?
Although a little late, here is:
location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$" {
set $uuid $1;
rewrite_by_lua '
local new_url
if (ngx.var.cookie_admin) then
new_url = "/modif/" .. ngx.var.uuid
else
--Get the url from DB using uuid
local res = ngx.location.capture("/url/" .. ngx.var.uuid);
if (res.status == 200 and res.body) then
new_url = tostring(res.body) --change the uri to the one from DB
else
return ngx.exec("#notfound") -- Fallback locaton if no url found in DB
end
end
ngx.req.set_uri(new_url) -- rewrite uri to new one
';
echo $uri; #test
#uwsgi_pass upstream_server;
}
It worked fine, with this two locations for testing:
location ~ "^/url/(.+)$" {
internal;
set $uuid $1;
#return 410; # test Uncomment to test #notfound
echo "/url/from/db/$uuid"; #test
#assuming you have postgre module
#postgres_pass database;
#rds_json on;
#postgres_query HEAD GET "SELECT url FROM your_table WHERE uuid='$uuid'";
#postgres_rewrite HEAD GET no_rows 410;
}
location #notfound {
echo "Ping! You got here because you have no cookies!";
}

error "not a valid Socket resource" implementing the phpwebsocket library

I'm trying to work with the phpwebsocket library at http://code.google.com/p/phpwebsocket/
I'm using the version r8 of the server.php file. For testing I just tried with the client.html file, also provided by the website.
When the server is started I get this:
Server Started : 2011-08-29 22:11:23
Master socket : Resource id #4
Listening on : www.midomain.com port 12345
But when I load the client.html file in the browser, the server shows the following errors:
Notice: Undefined variable: key1 in /home/mink500/public_html/test/server.php on line 143
Notice: Undefined variable: key2 in /home/mink500/public_html/test/server.php on line 143
Warning: socket_select(): 5 is not a valid Socket resource in /home/mink500/public_html/test/server.php on line 15
There are two variables that are not defined, and the function socket_select() returns the error "5 is not a valid Socket resource"
In the browser I get the "Disconnected" message as soon as the file is loaded.
I tried to make the server work locally using XAMPP (Apache and PHP) but I got the same errors. I also tried to change ports and follow the instructions in this issue:
http://code.google.com/p/phpwebsocket/issues/detail?id=33
But I still get the error "5 is not a valid Socket resource"
I remember that refreshing the page several times I got it running a few months ago, but now it's impossible. Besides, I need it to work all the time, not just after I refresh the page like 20 times.
I also tried with the websocket.class.php file, but this time I get an error on the client side. The browser now returns "Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing".
So, I can't make it work with old or new files, with remote or local server, with magic or a ouija board!
Any idea?
Thanks
Starting with the newest phpwebsocket client.html and server.php files I replaced both the getheaders() and dohandshake() functions with the code below works for me in newest Chrome. However it currently does not write to the browser nor stay alive after one uer comment in the chat box.
function dohandshake($user, $buffer) {
$key = null;
console("\nRequesting handshake...");
console($buffer);
console("Handshaking...");
preg_match("#Sec-WebSocket-Key: (.*?)\r\n#", $buffer, $match) && $key = $match[1];
$key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
$key = sha1($key);
$key = pack('H*', $key);
$key = base64_encode($key);
$upgrade =
"HTTP/1.1 101 Switching Protocols\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Accept: {$key}\r\n\r\n";
socket_write($user->socket, $upgrade . chr(0), strlen($upgrade . chr(0)));
$user->handshake = true;
console($upgrade);
console("Done handshaking...");
return true;
}
function getheaders($header) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach ($fields as $field) {
if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if (isset($retVal[$match[1]])) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
if (preg_match("/GET (.*) HTTP/", $header, $match)) {
$retVal['GET'] = $match[1];
}
return $retVal;
}

Resources