Concept - mtereschenko/simple_ror_environment GitHub Wiki

Here is the thought I have, for software development, in most cases, we don't need a lot of complicated logic and deep linking of technologies to initialize a new project. In the beginning, we need as simplest as a plausible environment that can handle our application, but with the potential to expand and reuse it by other teammates.

Here is the very basic concept of how the average web application works. Untitled Workspace

Nothing special, but picture if you are trying to create a SPA application and an API for it. In most cases I ever saw here is what an average developer doing:

It's became even worse if API has a several stages before production.

 if (env == local)<br>
   Access-Control-Allow-Origin: *
 else
   Access-Control-Allow-Origin: some.domain // or logic to calculate needed domains
 end

I hate each time configure Access-Control-Allow-Origin headers and be careful each time I try to add the staging server.

Using the pattern offered by the current repository you can use the Nginx server as a proxy server and forward requests to the services. Example:

In this case, our diagram is gonna be like this

Untitled Workspace-2

And nginx.config can be changed to something like this

server {
    listen 80;
    listen [::]:80;
    server_name blog.localhost;
    root /app;

    location /ws {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_pass http://node:8080/ws;
    }

    location / {
        proxy_pass http://node:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /api {
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_pass http://ruby:8081/;
    }

    error_log /var/log/nginx/app_error.log;
    access_log /var/log/nginx/app_access.log;
}

Now Nginx becomes a domain holder and proxy server for browser and SPA requests to API, and we don't need anymore to send Access-Control-Allow-Origin. For SPA and API, internal requests now look like localhost requests.