1
votes

I am doing pretty nasty things with nginx. Here is the birds-eye view.

nginx plays the role of a proxy between myriads of S3 buckets. In order to determine the correct bucket, I use Lua scripts on top of OpenResty platform.

So, firstly request enters / location and got processed by the Lua script.

location / {
  listen 8001;
  server_name $hostname;
  access_by_lua_file '/srv/hostingrouter/live/hostingrouter/storerouter.lua';
  proxy_pass http://$remote;
}

location ~ /s3/(?<s3_key>.+) {
  internal;
  proxy_pass $s3;
  rewrite .* /$s3_key break;
}

I cannot show you full Lua script because of NDA(hope you killaz understand me), but it does the next call somewhere between conditions:

return ngx.exec(
  '/s3/' .. s3_key
)

The tricky part is that Lua script does some other redirects too. But only in that particular case that showed above, I need to rewrite URL from HTTP to HTTPS.

I believe rewrite http:// https:// break; wouldn't work because matched path doesn't have scheme(I also tried it and it didn't work). Moreover, I tried to do a conditional redirect:

if ($scheme = http) {
  return 301 https://$host$request_uri;
}

but it resulted in a loop redirect.

How to redirect from HTTP to HTTPS from internal location?

1

1 Answers

0
votes

Ok, the answer is to make an ngx.redirect BEFORE the ngx.exec(i.e. in the Lua file). No need for special processing inside nginx.conf.