nginx rewrite rules - url-rewriting

I'm trying to implement nginx rewrite rules for the following situtation :
- Request /testfa/styles/style.css should be redirected to /testfa/templates/styles/style.css
I've enabled rewrite logging for my server, created following rules:
location /testfa {
rewrite ^/styles/(.+)?$ /testfa/templates/styles/$1 last;
}
but when I'm trying to perform request, I'm getting 404 error from server, and log file doesn't contain any info about rewriting, only following message:
open() "......../testfa/styles/style.css" failed (2: No such file or directory)
What is the correct way to perform such a rewrite for nginx ?

location /testfa/ {
rewrite ^/testfa/styles/(.+)$ /testfa/templates/styles/$1 last;
}
does this work for you ?
my tested virtual >
server {
listen ...ip...:80;
server_name sub.domain.com;
root /usr/local/www/test;
error_log /usr/local/www/test/error_debug.log debug;
rewrite_log on;
location /testfa/ {
rewrite ^/testfa/styles/(.+)$ /testfa/templates/styles/$1 last;
}
}
this works. even log has reported :
2011/11/25 01:06:52 [notice] 35208#0: *456705 rewritten data: "/testfa/templates/styles/test.css", args: "", client: IP, server: sub.domain.com, request: "GET /testfa/styles/test.css HTTP/1.1", host: "sub.domain.com"

Related

index_not_found_exception - Elasticsearch

In image #1, as you can see, I am getting a valid ES response on firing a GET request. However, if I try doing the same things through the NGINX reverse proxy that I have created and hit myip/elasticsearch, it returns me the error (image #2). Can someone help me with this?
server {
listen 80;
server_name myip;
location /elasticsearch/ {
proxy_pass http://127.0.0.1:9200;
}
location /kibana/ {
proxy_pass http://127.0.0.1:5601;
}
}
The right way is to specify both of those slashes. Slash after 127.0.0.1:9000 is essential, without it your request /elasticsearch/some/route would be passed as-is while with that slash it would be passed as /some/route. In nginx terms it means that you specified an URI after the backend name. That is, an URI prefix specified in a location directive (/elasticsearch/) stripped from an original URI (we having some/route at this stage) and an URI specified after the backend name (/) prepended to it resulting in / + some/route = /some/route. You can specify any path in a proxy_pass directive, for example, with proxy_pass http://127.0.0.1:9200/prefix/ that request would be passed to the backend as /prefix/some/route. Now if you understand all being said, you can see that specifying location /elasticsearch { ... } instead of location /elasticsearch/ { ... } would give you //some/route instead of /some/route. I'm not sure it is exactly the cause of your problem however configurations like
location /elasticsearch/ {
proxy_pass http://127.0.0.1:9200/;
}
are more correct.
Now may I ask you what you get with exactly this configuration in response to curl -i http://localhost:9200/ and curl -i http://localhost/? I want to see all the headers (of cause except those containing private information).
The problem is the path. Nginx is passing it unmodified.
Add a slash at the proxy_pass urls.
server {
listen 80;
server_name myip;
location /elasticsearch/ {
proxy_pass http://127.0.0.1:9200/;
}
location /kibana/ {
proxy_pass http://127.0.0.1:5601/;
}
}
From the documentation:
Note that in the first example above, the address of the proxied server is followed by a URI, /link/. If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. For example, here the request with the /some/path/page.html URI will be proxied to http://www.example.com/link/page.html. If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

nginx conf for tonic rest with multiple sites on mac

i try to config nginx with tonic rest for multiple sites on my mac.
My nginx runs as localhost on my mac.
My root is /Users/thorsten/Sites
In root i habe some projects e.g. /project1, /project2
Each project has the tonic rest folder /standard/rest...
In nginx.conf i try
location /rest/ {
fastcgi_pass_header Authorization; # Pass the http authorization parameter to the PHP script
if (!-e $request_filename) {
rewrite ^/(.*)$ /rest/dispatch.php?/$1 last;
break;
}
}
Nothing happend.
Do i need a config for each project or can i have a global config for all project e.g. $project/rest/...?
This config works for me
location ~ ^/(?<project>.+)/standard/rest/ {
fastcgi_pass_header Authorization;
include /usr/local/etc/nginx/conf.d/php-fpm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /$project/standard/rest/dispatch.php?/$1 last;
break;
}
}

Nginx Load Balancing

I want to load balance my website with nginx.
The load balancing in nginx wiki is proxy, so the actual file being downloaded from the frontend server. (http://wiki.nginx.org/LoadBalanceExample)
This is how I need the balancing:
user request file:
http:// site.com/image1.jpg
nginx redirect user to one of the servers (with Location header):
http:// s1.site.com/image1.jpg
http:// s1.site.com/image1.jpg
http:// s3.site.com/image1.jpg
Is this possible with nginx?
http {
split_clients "${remote_addr}" $server_id {
33.3% 1;
33.3% 2;
33.4% 3;
}
server {
location ~* \.(gif|jpg|jpeg)$ {
return 301 "${scheme}://s${server_id}.site.com${request_uri}";
}
}

Nginx Joomla Internationalization URL rewriting

I'm using Joomla in combination with Nginx, and I'm currently trying to achieve some URL rewriting for a website that has several langages supported (italian, french, chinese, and deutch)
The urls have the country code after the domain name, like so :
http://www.example.com/fr/test/test.html
or
http://www.example.com/de/test/test.html
I'm looking to rewrite the urls so the country code is part of the subdomain :
so
http://www.example.com/fr/test/test.html
becomes
http://fr.example.com/test/test.html
Is there a way to achieve this with Nginx or should I look into a third party extension for Joomla (not my favorite choice).
Thanks !!
Update :
I wasn't clear enough : I wanted the redirection from the rewrited URL to be transparent. Here is what I came up with, thanks to VBart help :
server {
server_name ~^(?<lang>.+)\.example\.com$;
location / {
rewrite /(.*)$ /$lang/$1 break;
proxy_pass http://www.example.com;
proxy_redirect http://www.example.com http://$lang.example.com/$request_uri;
}
}
Now, is there a way for Nginx to modify links on the fly in the served content ? ie: I want all the link in the generated page to look like http://fr... instead of http://.../fr/... ?
server {
server_name ~^(?<lang>.+)\.example\.com$;
...
}
server {
server_name www.example.com;
rewrite ^/(?<lang>[a-z]+)(?<rest>.+)$ http://$lang.example.com$rest? permanent;
}
opposite example:
server {
server_name ~^(?<lang>.+)\.example\.com$;
return 301 http://www.example.com/$lang$request_uri;
}
server {
server_name www.example.com;
...
}

nginx rewrite question

I need to do a rewrite with nginx from /blah/.../3275 to /id/3275 if the second file exists, otherwise I want to hand it off to apache. Here is my (feeble) attempt
(...) represents irrelevant stuff
if ($request_filename ~^/.../([0-9]+)/$) {
if (-d /id/$1) {
rewrite ^/.../[0-9]+/([0-9]+)/$ /id/$1;
}
}
Does anyone have any ideas
Best to do this with internal rewrites:
set $original_uri $uri;
location /blah/irrelevant_stuff {
error_page 404 = #apache;
rewrite ^/blah/irrelevant_stuff/([0-9]+)$ /id/$1;
}
location #apache {
proxy_pass http://upstream$original_uri;
}
The above answer from wulong I couldn't get to work for some reason but I did get it to work by using
if (!-e $request_filename) {
proxy_pass http://apache$original_uri;
break;
}
rather than the error_page directive. Same idea basically

Resources