I got this line in nginx conf, other line omitted
location /{
uwsgi_cache_key $request_uri;
}
and pure section
location ~ /_purge(/.*$){
uwsgi_cache_purge mycache $1;
}
It works well for url like http://host/test and http://host/_purge/test
but http://host/测试 with cache can't be purged by http://host/_purge/测试 or http://host/_purge/%E6%B5%8B%E8%AF%95
How to purge http://host/测试 cache ?
The var $request_uri in nginx is url encoded actually.
so the key cached is /%E6%B5%8B%E8%AF%95
but whether request to http://host/_purge/测试 or http://host/_purge/%E6%B5%8B%E8%AF%95 is the same. they are urlunescaped after the match /_purge(/.*$). The $1 actually is /测试.
So we can't purge the cache.
Solving:
extra module needed: https://github.com/openresty/set-misc-nginx-module#installation
location /{
set_unescape_uri $key $request_uri;
uwsgi_cache_key $key;
}
Related
All that I found about regsuball - it's description from docs: https://book.varnish-software.com/3.0/VCL_Basics.html#vcl-functions . But it still not clear for me what happens inside regsuball function. How does passed params (str, regex, sub) used? Is there a sandbox to test regsuball function online? Thanks!
Syntax
The regsuball() function will perform a regular expression match on a string and replace all occurences with another pattern.
regsuball(string, regex, sub)
The string argument is your input.
The regex argument is the regular expression you're using to match what you're looking for in the input string.
The sub argument is what the input string will be substituted with.
Example
The following example will look for cookies named original-cookie-123, where the numeric suffix could be any number. It replaces that with modified-cookie-123:
regsuball(req.http.Cookie, "original-cookie-([0-9+])", "modified-cookie-\1");
Imagine using passing the following cookie to Varnish:
Cookie: a=1; b=2; original-cookie-1=bla; c=3; original-cookie-2=test
The result after the find/replace would be:
Cookie: a=1; b=2; modified-cookie-1=bla; c=3; modified-cookie-2=test
Whereas regsub() matches and replaces the first occurrence of the pattern, regsuball() replaces all occurrences.
Sandbox
If you want to test regsuball() in a sandbox, the easiest way is by running varnishtest on a testcase. Here's an example:
varnishtest "Regsuball sandbox"
varnish v1 -vcl+backend {
vcl 4.1;
backend default none;
sub vcl_recv {
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "original-cookie-([0-9+])", "modified-cookie-\1");
return (synth(200, req.http.Cookie));
}
return (synth(400, "No cookie found"));
}
sub vcl_synth {
set resp.http.Content-Type = "text/plain; charset=utf-8";
set resp.body = req.http.Cookie;
return (deliver);
}
} -start
client c1 {
txreq -hdr "Cookie: a=1; b=2; original-cookie-1=bla; c=3; original-cookie-2=test"
rxresp
expect resp.body == "a=1; b=2; modified-cookie-1=bla; c=3; modified-cookie-2=test"
} -run
This test case uses the exact example mentioned earlier. After the find and replace, the VCL code in this test case will synthetically return the resulting value as output. There's no need for a backend server, all output is generated by the VCL code.
Running the test case, assuming it is stored in test.vtc can be done using the following command:
varnishtest test.vtc
You can also run this test case isolated within a Docker container. Just run the following command to bootstrap a Varnish Docker container and run the test:
docker run --rm --name varnishtest -v $(pwd)/test.vtc:/etc/varnish/test.vtc varnish:stable varnishtest /etc/varnish/test.vtc
I am trying to pass nginx variables to the passenger_app_env or passenger_user directives. It seems as though Passenger is not interpolating these as I'm seeing a literal $remote_user getting passed through.
location / {
set $something "something";
...
passenger_app_env $something;
...
}
Is this possible?
I have for example this URL:
www.example.com/folder1/folder2/edit.php?username=nickname
Actually I have this rewrite rules:
location / {
root /var/www;
index index.php index.html index.htm;
# First rewrite rule output: www.example.com/nickname
rewrite ^/([A-Za-z0-9_]+)$ /folder1/folder2/user.php?username=$1;
# Second rewrite rule output: www.example.com/nickname/edit
rewrite ^/([A-Za-z]+)/edit$ /folder1/folder2/edit.php?username=$1;
}
However, that works fine.
But I need to rewrite my URL like this:
www.example.com/nickname/edit/info
It has a couple of parameters:
www.example.com/folder1/folder2/edit.php?username=nickname&info=basic
I tried everything but with no success:
location / {
root /var/www;
index index.php index.html index.htm;
# First rewrite rule output: www.example.com/nickname
rewrite ^/([A-Za-z0-9_]+)$ /folder1/folder2/user.php?username=$1;
# Second rewrite rule output: www.example.com/nickname/edit
rewrite ^/([A-Za-z]+)/edit$ /folder1/folder2/edit.php?username=$1;
# Here where I'm stuck
# www.example.com/nickname/edit/info
rewrite ^/edit/([A-Za-z]+)/info$ /folder1/folder2/edit.php?user=$1&info=$2;
}
Your last attempt seems to have mixed up the location of edit. Also, nothing is passed to info as there is only one capturing group, perhaps
rewrite ^/([A-Za-z]+)/edit/([A-Za-z]+)$ /folder1/folder2/edit.php?username=$1&info=$2;
will work instead?
I'm trying to implement nginx rewrite rules for the following situation
Request:
http://192.168.64.76/Shep.ElicenseWeb/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
Should be redirected to:
http://localhost:82/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
I tried this with no luck:
location /Shep.ElicenseWeb/ {
rewrite ^/Shep.ElicenseWeb/ /$1 last;
proxy_pass http://localhost:82;
}
What is the correct way to perform such a rewrite for nginx ?
Your rewrite statement is wrong.
The $1 on the right refers to a group (indicated by paratheses) in the matching section.
Try:
rewrite ^/Shep.ElicenseWeb/(.*) /$1 break;
You're missing a trailing slash:
location /Shep.ElicenseWeb/ {
proxy_pass http://localhost:82/;
}
This will work without a rewrite.
I want people type in http://www.myweb.com/like/1234456 will redirect to
http://www.myweb.com/item.php?itemid=1234456
I wrote something like this in the config but it doesn't work.
location = ^~/like/ {
rewrite ^1234456 ../likeitem.php?item=1234456break;
return 403;
}
this is just a test. I haven't used the $ matching yet.
I also restart my ngnix server but still.. it doesn't do the redirect.
The code above will not work because of a missing $ and poor use of the return command.
The code below works with Nginx, including version 0.8.54.
Format below is :
DesiredURL
Actual URL
Nginx_Rule
They must be inside location / {}
http://example.com/notes/343
http://example.com/notes.php?id=343
rewrite ^/notes/(.*)$ /notes.php?id=$1 last;
http://example.com/users/BlackBenzKid
http://example.com/user.php?username=BlackBenzKid
rewrite ^/users/(.*)$ /user.php?username=$1 last;
http://example.com/top
http://example.com/top.php
rewrite ^/top?$ /top.php last;
Complex and further
http://example.com/users/BlackBenzKid/gallery
http://example.com/user.php?username=BlackBenzKid&page=gallery
rewrite ^/users/(.*)/gallery$ /user.php?username=$1&page=gallery last;
Try this,
server {
server_name www.myweb.com;
rewrite ^/like/(.*) http://www.myweb.com/item.php?itemid=$1 permanent;
}