actually my first question on stackoverflow, because case is not so easy.
I want nginx to rewrite all requests to static file appending requested url, and i achieved this easily with this rule
location / {
index index.html;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /index.html#!$1 break;
}
BUT this is resulting in 404 and in log i see exactly how this should work, but it isn't.
2016/11/21 11:53:33 [error] 5220#6856: *1 "C:/Projects/ProCongress/web/index.html#!/index.html" is not found (3: The system cannot find the path specified), client: 127.0.0.1, server: congress, request: "GET / HTTP/1.1", host: "congress"
web/index.html#!/index.html - this part is acutally absolultley right, and should work correctly, but isn't...
Related
I can't get my head round why my nginx config keeps looking for files in the wrong directory.
I'm trying to map the /files location to /home/user/toto. For that I'm using a regex capture group to remove the /files prefix and search for the files in the directory specified in the root directive.
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
root /home/user/toto;
try_files /$1/ /$1 ;
autoindex off;
}
}
The problem is I only get 404s. And that's pretty normal because here is what we can find in error.log :
2022/07/17 11:27:49 [error] 40358#0: *40 open() "/usr/local/nginx/html/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
As we can see the root directive seems to be completely ignored.
I tried an alternative configuration such as :
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
# root /home/user/toto;
try_files /home/user/toto/$1/ /home/user/toto/$1 ;
autoindex off;
}
}
But the problem is still the same. The only change is that the path now appears in the logs as a prefix for the file name :
2022/07/17 11:12:02 [error] 40288#0: *36 open() "/usr/local/nginx/html/home/user/toto/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
What am I missing ?
Your missing the last parameter from the try_files directive, so that the /$1 is being interpreted as an internal redirect instead of a file term.
Use:
try_files /$1/ /$1 =404;
See this document for details.
I am trying to configure nginx to serve up error pages from an s3 bucket.
To that end my configuration looks like this:
location / {
error_page 404 = #fallback;
}
location #fallback {
rewrite ^ /my-s3-bucket/404.html;
proxy_pass https://s3.ap-northeast-2.amazonaws.com;
}
My expectation is that anything that hits the website and is not found is then sent to the #fallback location. I then want to rewrite the URL with the actual location of my 404 page and send on to the s3 bucket. I don't want to just 302 redirect to the 404 page.
The problem is that the proxy_pass directive is not executed. Instead, it just looks for my rewritten URL locally.
See my access logs below:
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/sdfd" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/my-s3-bucket/404.html" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
I made a request to www.dev.mywebsite.com.au/sdfd which wasn't found. 'sdfs' was rewritten to 'my-s3-bucket/404.html' but instead of then proxy passing that to https://s3.ap-northeast-2.amazonaws.com it looks for it in the local /etc/nginx/html directory.
My nginx version is 1.15.2
Use rewrite...break if you want the rewritten URI to be processed within the same location block. See this document for more.
For example:
location #fallback {
rewrite ^ /error/404.html break;
proxy_pass https://example.com;
}
I have the following block in my nginx config:
location ~ /$ {
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
}
location /cat {
try_files /index.php?url=$uri =404;
}
What I expect this to do: Any request sent to http://www.example.com/cat<whatever> will be sent to index.php with the GET variable url set to the request uri. What happens instead based on the rewrite log is that the location /cat block is never hit at all. Here is a relevant excerpt from the rewrite log when I request http://www.example.com/cart/testing/:
2017/03/21 00:32:49 [error] 25711#0: *20566477 "/var/www/vhosts/example.com/httpdocs/cat/testing/index.html" is not found (2: No such file or directory), client: <ip redacted>, server: example.com, request: "GET /cat/testing/ HTTP/1.1", host: "www.example.com"
There are no other entries related to the request.
It was my understanding that nginx processes any prefix location blocks (i.e. location /cat) BEFORE any location blocks involving regex (i.e. location ~ /$). So I'm stumped by this behavior.
Your understanding is incorrect. See this document for details.
Also, your try_files directive is incorrect. Either /index.php?url=$uri or =404 should be the last element in the statement. See this document for details.
You probably want:
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
location /cat {
try_files $uri $uri/ /index.php?url=$uri;
}
Not sure of the purpose of the other location block, as the index directive is already going to process any URI with a trailing /. See this document for details.
I use nginx with php-fpm for my webserver and I have a little trouble with the rewrite system.
So when I want to access to page like /dashboard or /api/info it works correctly. But when I want to access to page with dash in URL like /dashboard/global-infos, I get an error 502 (Bad Gateway).
My configuration for rewriting is:
rewrite ^/index\.php/?(.*)$ /$1 permanent;
location / {
index index.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /index.php/$1 last;
}
rewrite_log on;
When I read log, I do not have log on rewriting, only :
recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 176.190.XXX.XXX, server: domain.com, request: "GET /dashboard/global-infos
Hope you can help me, have a good day !
I'm trying to set up nginx with proxy_pass when a unique URL is visited:
location ~ /proxy/(?<var>.+) {
proxy_pass $var;
}
When I visit http://example.com/proxy/http://google.com I get a 500 Internal Server Error, and the error log has the below entry:
invalid URL prefix in "http:/google.com", client: 2.33.214.165,
server: example.com, request: "GET /proxy/http://google.com HTTP/1.1",
host: "example.com"
Is there any reason why nginx doesn't include the second / in the URL?
try this: merge_slashes
though the context is server wide, so if you still need to merge slashes elsewhere you will have to improve your regexp in the location splitting the proxy target into schema and host+uri. something like this:^/proxy/(?<schema>.+)://(?<rest>) and then proxy_pass $schema://$rest may work.