Nginx Rewrite Querystring to Path with file - nginx

I am creating a rule for S3, with Rewrite Querystring to Path with file
Requests from the browser to the server can be
http://localhost:8088/bucket-media/1111111_0.jpg?size=large
"size" is a directory in the s3 bucket, it needs to be proxied to S3
https://s3-storage/bucket-media/large/1111111_0.jpg
My config
set $bucket "s3-storage";
location /bucket-media/ {
#set $size $arg_size;
#rewrite ^ /$size$uri break;
if ($args ~* "size=(.*)") {
set $w1 $1;
rewrite .* /bucket-media/$w1/$uri break;
}
proxy_intercept_errors on;
resolver 8.8.8.8;
proxy_pass https://$bucket$uri;
error_page 404 = #fallback;
}
location #fallback {
proxy_intercept_errors on;
proxy_pass http://other-backend/internal/images/missing/?file_name=$uri;
}
It doesn't work right. No filename when proxying.
https://storage/bucket-media/large/

Related

Nginx proxy_pass to directory in if statement

what i want to do is reverse proxy b.com/c/d/1.jpg directory with a.com/1.jpg
location / {
proxy_ssl_server_name on;
if (!-e $request_filename) {
# below will cause error: cannot have URI part in location given by regular expression, or inside named location, or inside \"if\" statement,
# proxy_pass https://b.com/c/d;
proxy_pass https://b.com;
}
}
can anyone help? how to do this on right way. THX.
Replace if (!-e $request_filename) with a try_files statement, and place the proxy_pass into a named location. Use rewrite...break to adjust the URI before it's passed upstream.
For example:
location / {
try_files $uri $uri/ #proxy;
}
location #proxy {
rewrite ^/(.*)$ /c/d/$1 break;
proxy_ssl_server_name on;
proxy_pass https://b.com;
}

Nginx rewrite not redirecting

I would like to rebuild a URL and redirect from https://test.com/info/schoolName/detail to https://test.com/school-info?name=schoolName with Nginx.
I have tried
location ~ ^/(school-info)(?:/(.*))?$ {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
proxy_set_header Host $backend_netlify_main;
proxy_ssl_server_name on;
proxy_pass https://$backend_netlify_main/$1/$2;
}
...
...
location ~* ^/(info|info/)$ {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
rewrite ^/info/(.?)/(.*)$ /school-info?school=$1 permanent;
proxy_pass $backend_cms;
}
however, if I visit https://test.com/info/byu/detail it's not doing a redirect at all.
EDIT: The /detail at the end is not important at all, so regardless of what is at the end of the URL the /schoolName/ is the most important part to be passed as a query parameter.
I think you need something like
location / { # "default" location
# do redirection for '/info/...' URIs
rewrite ^/info/([^/])* /school-info/$1 permanent;
# otherwise pass request to the default backend
proxy_pass $backend_cms;
}
location /school-info {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
proxy_set_header Host $backend_netlify_main;
proxy_ssl_server_name on;
proxy_pass https://$backend_netlify_main;
}
if you need to pass a request as /school-info/schoolName, or
location / { # "default" location
# do redirection for '/info/...' URIs
rewrite ^/info/([^/])* /school-info?name=$1 permanent;
# otherwise pass request to the default backend
proxy_pass $backend_cms;
}
location /school-info {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
proxy_set_header Host $backend_netlify_main;
proxy_ssl_server_name on;
proxy_pass https://$backend_netlify_main/school-info$is_args$args;
}
if you need to pass a request as /school-info?name=schoolName.

Resizing Image Nginx on the Fly with Backend S3

I've already installed nginx with module http image filter module and all dependencies. my nginx version 1.8.0.
here's my script :
server {
listen 80;
server_name my.domain.com;
location ~* / {
proxy_pass http://mydomain.s3-website-ap-southeast-1.amazonaws.com;
}
location ~* ^/resize/([\d\-]+)x([\d\-]+)/(.+)$ {
alias http://mydomain.s3-website-ap-southeast-1.amazonaws.com/$3;
image_filter resize $1 $2;
image_filter_buffer 2M;
error_page 415 = /empty;
}
location ~* ^/crop/([\d\-]+)x([\d\-]+)/(.+)$ {
alias http://mydomain.s3-website-ap-southeast-1.amazonaws.com/$3;
image_filter crop $1 $2;
image_filter_buffer 2M;
error_page 415 = /empty;
}
location = /empty {
empty_gif;
}
}
when i request http://my.domain.com/some_image.jpg, it's return no error
but when i try with http://my.domain.com/resize/300x300/some_image.jpg, it return 404 or not found
Update#1
I've change :
location ~* / {
to :
location / {
it's fix the error on my config.

Convert 3 redirect rules into one

A proxy app proxies to other apps with some regex in the location blocks of the nginx.conf. For example to a signup app called proxy-signup-app.
This works, but it has 3 location blocks:
location ~* ^/signup/(.*)/$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$1;
}
location ~* ^/signup/(.*)$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$1;
}
location ~* ^/signup$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$1;
}
It should proxy
/signup
/signup/
/signup/rainyday
/signup/rainyday/
Where the proxy app would receive on proxy_regex:
$1 = ''
$1 = ''
$1 = 'rainyday'
$1 = 'rainyday' (not /rainyday/, rainyday/ or /rainyday)
It should not redirect
/signups
How to merge these location blocks into one?
not sure if it's the best way, but it should work
location ~* ^/signup(/(.*?)/?|)$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$2;
}

config nginx to access index and spec_runner pages

index.html page could access, but not spec_runner.html.I want to access two different pages, one for app, another for tests. Could you help me? thanks, :).My configure is following
location ^~ /p/login { rewrite .* /index.html last; }
location ^~ /specs { index spec_runner.html; }
location ~* ^/(users|books) {
proxy_pass http://api;
}
location / {
root /home/user/project;
proxy_cache off;
expires -1;
add_header Cache-Control no-cache;
add_header Cache-Control private;
}

Resources