use many ssi with fastcgi_cache in nginx - caching

I am using fastcgi_cache in nginx in order to speed up the php, which connects database and select articles.
Here is my flow:
first, I add a ssi-include in my index:
<\!--# include file="/templates/1-5-list.html" -->
then, I add a location route to handle html -> php in nginx-conf
location ~(\d*?)-(\d*?)-list.html
{
try_files $uri /articles/list.php?p1=$1&p2=$2;
}
after that, I apply fastcgi_cache for list.php
# outside the server{}
fastcgi_cache_path /home/cache/articles levels=1 keys_zone=articles_cache:10m max_size=1024m inactive=1h;
fastcgi_cache_key $scheme$host$request_uri$request_method;
# outside the server{}
location ~/list.php$ {
fastcgi_cache articles_cache;
fastcgi_cache_valid 200 2h;
...
}
Everything is ok right now, and the caching function well.
However, If I have two or more ssi in my index:
<\!--# include file="/templates/1-5-list.html" -->
<\!--# include file="/templates/2-5-list.html" -->
The second ssi return exactly the same result as the first one, FAIL!
I search inside the cache directory, And I found that, the KEY using for caching is httplocalhost/articlesGET, which means such two ssi are sharing the same KEY. And I think this is the cause.
My question is how can I modify the fastcgi_cache_key such that they can have different KEY? I've tried adding fastcgi_cache_key inside location{} but fail.

$request_uri in nginx SSI subrequest refers to parent request URI.
Use $uri in included fragment cache key instead:
fastcgi_cache_key $scheme$host$uri$request_method;

Related

Dingo API routing does not pass in GET parameters when routing to controller

I have a Laravel Application and it uses Dingo Router:
$api->get('/cash-flow', 'App\Http\Controllers\ReportController#cashFlowReport');
When my front-end calls this api, it gets 200 responses from OPTIONS & GET. However, it does not successfully pass in the GET variables.
public function cashFlowReport(Request $request)
{
$input = $request->all();
return var_dump($input);
}
The response returns an empty array. I thought it was the Request class dependency but I think it would throw an error when it tries to access the parameter.
I have the Request Dependency:
use Illuminate\Http\Request;
If you are using nginx you will want to make sure the query string is actually making it through the 'rewrite'/'pretty url' process.
From the Laravel installation documentation for Pretty Urls:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
The ?$query_string part is important.
Laravel Docs - Installation - Pretty Urls

Laravel 5 sending information using get request

In my project I use www.example.com/test?prevUrl=test/3/home this is working fine in development when inside my controller I do $request->prevUrl but doesn't work in production(returns null). can someone help?
edit= "The query_string is not being properly set i-e (?prevUrl=something ) and for that reason my paginator is also not working properly
You can do like it...
In web.php:
Route::get('test','YourController#yourMethod');
In Controller:
public function yourMethod(){
//or return $request->query('prevUrl');
return $request->input('prevUrl');
}
I am using ubuntu 16.04 nginx server. The problem was that $_GET session variables were not being set properly. so what I did was inside /etc/nginx/sites-available/default and inside location / block/ directive I changed try_files $uri $uri/ /index.php?$query_string;to try_files $uri $uri/ /index.php?$args;and it worked.

NGINX domain rewrite rules

What I am trying to achieve using nginx is this:
http://domain.com - redirects to http://otherdomain/page.html
http://www.domain.com - redirects to http://otherdomain/page.html
http://domain.com/* - redirects to http://otherdomain/*
Basically only the domain and the www should be redirected to an url link. Everything else should be redirected to another domain, but keeping the url like this:
http://domain.com/subdomain/page.html -> http://otherdomain/subdomain/page.html
If you have questions please let me know. Thank you!
You can use $request_uri
see in http://nginx.org/en/docs/varindex.html
Probably like below
server{
location = /
{
rewrite ^ http://otherdomain/page.html;
}
location /subdomain
{
rewrite ^ http://otherdomain/$request_uri;
}
}

how to process dynamic urls as static pages using nginx?

I want my Nginx serve dynamical urls as static pages, e.g.
given a url "/book?name=ruby_lang&published_at=2014" ,
the nginx will serve a static file (which is generated automatically ) named as:
"book?name=ruby_lang&published_at=2014.html" or:
"book-name-eq-ruby_lang-pblished_at-eq-2014.html"
is this possible?
NOTE:
1.there's no static file named:
"book?name=ruby_lang&published_at=2014.html" nor
"book-name-eq-ruby_lang-pblished_at-eq-2014.html"
however, I can generate them if needed.
2.I can't change the url that give to the consumer. e.g. my consumer could only send request to me via
"/book?name=ruby_lang&published_at=2014"
but not with any other urls.
If you are OK with generating the HTML files yourself, you could simply use nginx's rewrite module. Example:
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
If you need to make sure that name and published_at are valid, you can instead do something like this:
location = /book {
if ($arg_name !~ "^[A-Za-z\d_-]+$") { return 404; }
if ($arg_published_at !~ "^\d{4}$") { return 404; }
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
}
This will make sure that published_at is a valid 4-digits integer, and name is a valid identifier (English alphabets, numbers, underscore, and hyphen).
To make sure a book is only accessible from one URL, you should throw 404 if the URL is the HTML file. Add this before the previous rule:
location ~ /book-(.*).html {
return 404;
}
OK, thanks to #Alon Gubkin's help, finally I solved this problem, (see : http://siwei.me/blog/posts/nginx-try-files-and-rewrite-tips) .here are some tips:
use 'try_files' instead of 'rewrite'
use '-' instead of underscore '_' in your static file names, otherwise nginx would get confused when setting $arg_parameters to your file name. e.g. use "platform-$arg_platform.json' instead of "platform_$arg_platform.json"
take a look at nginx built-in variables.
and this is my nginx config snippet:
server {
listen 100;
charset utf-8;
root /workspace/test_static_files;
index index.html index.htm;
# nginx will first search '/platform-$arg_platform...' file,
# if not found return /defautl.json
location /popup_pages {
try_files /platform-$arg_platform-product-$arg_product.json /default.json;
}
}
also I put my code on github so that someone interested in this issue could take a look:
https://github.com/sg552/server_dynamic_urls_as_static_files

Redirect to other domain if static files do not exist

I would like to configure nginx so that
The requested path is a static file, serve it;
otherwise redirect an external domain: http://example.net/.
I do understand that I need to use the try_files directive, but I do not understand how to implement the fallback mechanism.
Found after many tests. What happens if a static file is not found is defined in the last parameter of try_files. You can set that parameter to an internal location. In my case I used
server {
...
root /srv/example.org/web/static;
index index.html;
try_files $uri #redirect-to-dot-net;
location #redirect-to-dot-net {
rewrite ^ http://example.net redirect;
}
}

Resources