0

I want to set up NGINX to serve OpenUI5 applications on development machine. I never used NGINX before, and experience troubles with rewriting URLs.

As I can see from trace log, it is applied twice, and thus request results in 404 error.

"^/(?<name>[^/]+)(/(?<something>.*)|/?)$" matches "/HelloWorld/"
rewritten data: "/HelloWorld/WebContent/"

"^/(?<name>[^/]+)(/(?<something>.*)|/?)$" matches "/HelloWorld/WebContent/index.html"
rewritten data: "/HelloWorld/WebContent/WebContent/index.html"

Here is a pice of my NGINX config:

location / {
  root     projects;
  index    index.html index.htm;
  rewrite  ^/(?<name>[^/]+)(/(?<something>.*)|/?)$ /$name/WebContent/$something break;
}

location ~ ^/([^/]+)/resources/(.*)$ {
  resolver 8.8.8.8;
  add_header Access-Control-Allow-Origin "*";
  proxy_pass https://openui5.hana.ondemand.com/1.34.9/resources/$2;
  proxy_redirect off;
  access_log on;
}

Let me clarify the task I'm trying to solve. Basically project looks like this:

+ nginx_root
|-+ projects
  |-+ HelloWorld
    |-+ WebContent
      |-- index.html
      |-+ main
        |-- main.view.xml
        |-- main.controller.js

There are three requirements to make this HelloWorld work:

  1. We should hide WebContent folder, i.e. make its contents available from http://localhost/HelloWorld and not http://localhost/HelloWorld/WebContent

  2. Each XHR from the application should be mapped according to 1st requirement. For example, if application requires http://localhost/HelloWorld/main/main.view.xml, NGINX should serve it from HelloWorld/WebContent

  3. Application attempts to load OpenUI5 library from ./resources folder, i.e. http://localhost/HelloWorld/resources/sap-ui-core.js`. The library itself should be delivered from CDN: https://openui5.hana.ondemand.com/1.34.9/resources/

Please, excuse me for my English and let know if I could clarify the question. Thanks!

1

This is my solution based on what I understand from your question.

First of all, you should have your root defined outside of your location blocks, since nginx behaves in somewhat unintuitive way when root is defined there.

Then, the location block to solve points 1 and 2:

location ~ ^/(<?project>[^/]+)/(<?folder>.*) {
    alias /path/to/nginx_root/$project/WebContent/$folder;
}

Here, we capture the name of the project and the path inside the project to $project and $folder variables, and then use these variables to construct the real path inside the filesystem where the files for this particular project exist, and tell nginx to fetch files from this path using the alias directive.

Then, for point 3, I suggest that you build a feature inside your application, where you can directly build URLs that point to your CDN. You will miss many benefits of CDN if you point the URLs to your existing web server, and then redirect requests to CDN.

Even worse is the proxy_pass you used in your solution. In that case, a request is made to your nginx server, which then requests the resource from CDN, and then sends the resource to user.

  • Thanks a lot, it works like a charm! Unfortunately I can't avoid use of proxy_pass, because I can't change application code to satisfy development-only need. When application is deployed to production server, it will handle delivering resources itself. SAPUI5 library isn't available for download (as OpenUI5 does) and the only option is to grab it on the fly from live server. – Nikolai Kim Mar 18 '16 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.