Purge a resource from referer VARNISH - varnish

I'm trying to PURGE a HTML page if a css file is not in the varnish cache anymore. This is what I'm doing :
if (beresp.status == 404 && req.url ~ "\.css$") {
ban("obj.http.x-url ~ "+ req.http.referer);
}
If I've got a 404 on a CSS file, I would like to ban the referer. The problem is that "req.http.referer" has "http://" in front of the URL. So, it doesn't work. (It works without the "http://".
I tried :
ban(req.http.referer);
but doesn't work.
Any idea please on how to remove the "http://" or how to do that job in a different way ?
Thanks.
EDIT
Find solution to remove the "http://":
ban("obj.http.x-url ~ "+ regsub(req.http.referer, "^http://", ""));
Thank you ;)

Related

Hostinger, node.js, or express issue, routes not working anymore (except homepage / )

Firstly I'd like to thank you and everyone part of this community after years of lurking around I finally asked my first question, hopefully soon I'll be able to provide answers too! :)
So to summarize the problem, I'm getting 404's on my routes (except homepage) configuration is ubuntu20/nginx/nodejs/express app hosted on Hostinger VPS, I've tried hitting it up with their support, but they are clueless and referred me here, not such a bad idea I thought!
What is weird:
It was working perfectly fine (lol)
On localhost works
Believing it was one of the recent updates, after a while of debugging I thought, "screw that, just roll back to the last backup" which was working perfectly fine!
However, to my surprise the working backup, isn't working anymore, I'm 100% sure it was working, since I continued working for days, which couldn't have been done without working routes.
The nginx error log is empty, the access log is showing [05/Dec/2021:17:02:30 +0000] "POST /api/search HTTP/1.1" 404 194 "https://domain.io/" - this is the proper route.
I'm already typing more than most of the questions here, please guide me and I will provide anything possible.
Also I heard express is "dead" should I just switch to something else in hopes it will 'solve' this?
THANK YOU!
Updates:
tried sudo apt-get update, removed node_modules, reinstalled all
tried to rename the homepage route from app.get("/" to app.get("/home" and tried to access domain.io/home and it didn't work! On localhost it works fine
reboot server, restart everything, as I said even restored working backup, this issue is beyond me...
Update 2:
I tried a different file, and installed Fastify and tried a default config of
// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })
// Declare a route
fastify.get('/', async (request, reply) => {
return { hello: 'world' }
})// Declare a route
fastify.get('/test', async (request, reply) => {
return { hello: 'test' }
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
The homepage route works, but /test doesn't (even though it works on local) I'm 99.99% sure this is a hosting issue, what do you think?
UPDATE 3:
FINALLY GOT IT, someone please explain how this is possible, inside my nginx config, this line was causing it, after removing everything works.
try_files $uri $uri/ =404;
Can anyone, please explain to me, how my server worked fine for a week with this 'error' inside the config? Keep in mind I restarted nginx dozens of times, this 'line' worked!
Sorry for venting, but how is this possible?

Get project base url that works in XAMPP and on production, considering .htaccess

I have a project that is not in the root of the XAMPP folder:
htdocs/my-folder/projects/my-project
htdocs/my-folder/projects/my-project/index.php
htdocs/my-folder/projects/my-project/js/
htdocs/my-folder/projects/my-project/css/
that folder contains an index.php file from where I try to call stylesheets and scripts. Initially, I'd just do it like this:
<script src="js/myscript.js"></script>
which worked. However, the project has expanded and now a user can "save" the current page (similar to how JSFiddle does it), and the URL will look different. Upon a first save a random string will be appended as a conf parameter, which results in something like this (locally) and should have a public equivalent:
localhost/my-folder/projects/my-project?conf=abcd # locally
http://mywebsite.com/projects/my-project?conf=abcd # publicly
Upon second save, the URL gets an additional parameters, a version number:
localhost/my-folder/projects/my-project?conf=abcd&v=2 # locally
http://mywebsite.com/projects/my-project?conf=abcd&v=2 # publicly
But, to get a nice URL I use this .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (\w+)[/](\d+)$ ?conf=$1&v=$2 [L,NC]
which will result in something like this:
localhost/my-folder/projects/my-project/abcd/2 # locally
http://mywebsite.com/projects/my-project/abcd/2 # publicly
The thing is, when the URL is changed to some structure as above (without the parameters, but with the rewritten URLs, e.g. localhost/my-folder/projects/my-project/abcd/2) then the initial call to the resources (scripts, styles) in my index file won't be correct any longer.
In other words, if this is the url: localhost/my-folder/projects/my-project/abcd/2 then the server will look for a a script file in localhost/my-folder/projects/my-project/abcd/2/js/myscript.js, which is obviously wrong.
The question, thus, is: how can I get the absolute path to the current file, but that also works in XAMPP (so not __FILE__ or __DIR__ which will dig in the file strucure and return file://) and also on production environments.
You'll have to use the base element in your pages. The usage will be something like:
<base href="/projects/my-project/" />
for the public server and
<base href="/my-folder/projects/my-project/" />
locally.
I hackingly figured it out. By checking the end of the url, we determine if we are in root or if we are in a specific instance (ending with a digit e.g. /1.
<?php
// Is connection secure? Do we need https or http?
// See http://stackoverflow.com/a/16076965/1150683
$isSecure = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$isSecure = true;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'
|| !empty($_SERVER['HTTP_X_FORWARDED_SSL'])
&& $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
$isSecure = true;
}
$REQUEST_PROTOCOL = $isSecure ? 'https' : 'http';
$REQUEST_PROTOCOL .= '://';
// Create path variable to the root of this project
$path_var = $REQUEST_PROTOCOL.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// If URL ends in a slash followed by one or more digits (e.g. http://domain.com/abcde/1),
// returns a cleaned version of the URL, e.g. http://domain.com/
if (preg_match("/\/\d+\/?$/", $path_var)) {
$path_var = preg_replace("/\w+\/\d+\/?$/", '', $path_var);
}
?>
Now we can use it in our PHP file!
<link href="<?php echo $path_var; ?>css/styles.css" rel="stylesheet">

Nginx rewrite rule without changing url

I need rewrite rule for my nginx server without changing the url.
For example :
The following link
http://example.com/old/path/index.php?cid=XXhhHHnmmm
to become :
http://example.com/newpath/index.php?cid=XXhhHHnmmm
and to point in that specific folder (/old/path).
till now I've tried the following which is working if I try opening _http://example.com/newpath:
but not working if I try
_http://example.com/newpath/index.php?cid=XXhhHHnmmm
location ~ /old/path {
rewrite "/old/path" http://www.example.com/newpath$2 break;
}
I've also tried with proxypass :
location /newpath {
proxy_pass http://www.example.com/old/path;
}
but still not working as desired.
Try
location ~ ^/newpath {
rewrite ^/newpath/(.*) /old/path/$1 break;
}
For the reference, since it did not work for a file to file redirect, which is as simple as:
rewrite ^/terms-of-use.html /legal/some-terms-of-use-v3.html break;

Cannot serve static files with express routing and no trailing slash

I want a route called 'main' which will serve static files:
app.use('/main',express.static(__dirname+'/public'));
However when i do:
http://my.site.dev/main
The CSS and JS files won't download because it is trying to get them from
http://my.site.dev/css/styles.css
It should be getting the files from:
http://my.site.dev/main/css/styles.css
However, if I access my site with a trailing slash:
http://my.site.dev/main/
All files come through fine
Any ideas why not having a trailing slash messes up resources like CSS and JS from coming in?
This is an http problem, not just an Express-related challenge. The problem is discussed at:
Relative URLs and trailing slashes
http://www.bizcoder.com/the-mystery-of-the-trailing-slash-and-the-relative-url
If your URL is /main and your relative URL is css/style.css, it will resolve to /css/style.css; but if your URL is /main/, the relative URL resolves to /main/css/style.css.
My strategy for dealing with this is to redirect to add the trailing slash. Like this in Express:
app.all(/^\/main$/, function(req, res) { res.redirect('/main/'); });
app.use('/main/',express.static(__dirname+'/public'));
Or:
app.enable('strict routing');
app.all('/main', function(req, res) { res.redirect('/main/'); });
app.use('/main/',express.static(__dirname+'/public'));
How are the JS/CSS files requested in the HTML? If you're using strings like css/styles.css, then it will try to get them from the current directory. The directory for /main is / (just like /main.html would be), while the one for /main/ is /main/. A quick fix would be to use /main/css/styles.css in your HTML.

CouchDB vhost rewrite to access root API

I wish the following rewrite rule worked:
{
"from": "api/*",
"to": "../../../*"
}
… in a vhost rewrite like the following:
[vhosts]
myapp = /myapp/_design/myapp/_rewrite
Then it would be possible to access the root API in a following manner:
$.couch.urlPrefix = '/api';
var dbs = $.couch.allDbs({
success: function (data) {
console.log(data);
}
})
But unfortunately the request to http://myapp:5984/api/_all_dbs results into:
{"error":"insecure_rewrite_rule","reason":"too many ../.. segments"}
Am I missing something? Is something wrong with the rewrite? Does anyone know how to overcome that?
My Couchdb is 1.1.1.
I'm acquainted to this advice, but don't like any of the suggested ways.
Add
[httpd]
secure_rewrites=false
to your server's local.ini to disable this protection from cross-database rewrites.

Resources