Rails Sidekiq configuration for micro services on reverse proxy.

Content posted here with the permission of the author Rahul Ojha, who is currently employed at Josh Software. Original post available here.

This blog is intended to describe how to configure multiple sidekiq webs for API only apps using reverse proxy server.

Background

We have 4 API services running on ECS. We used a single domain for all the services and segregated them with the namespace in routing. For namespace routing and pointing to Docker containers, we have used Traefik. We can do the same thing using Nginx and some other reverse proxy too.

Example:

www.domain.com/rails-api-1/REST_ROUTES
www.domain.com/rails-api-2/REST_ROUTES
www.domain.com/rails-api-3/REST_ROUTES
www.domain.com/rails-api-4/REST_ROUTES

Problem Faced

Now the problem was when we try to access sidekiq webs with these namespace routes. It does not load rails assets because namespace path ‘rails-api-1 ’ is not considered in the root path, and sidekiq tries to find assets in the path `www.domain.com/sidekiq/` and it doesn’t exist actually.

Solution

Initially, we mounted sidekiq path in config/routes.rb like this.

mount Sidekiq::Web => '/sidekiq'

Later, we changed it to

mount Sidekiq::Web => '/rails-api-1-sidekiq'

Configuration for Traefik:

You need to add a new rule in docker labels

Key => traefik.sidekiq.frontend.rule 
Value => Host:www.domain.com;PathPrefix:/rails-ap1-1-sidekiq

Configuration for Nginx:

If you are using Nginx as a reverse proxy then you need to add these configuration for each service.

location /rails-ap1-1-sidekiq/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://your_domain/rails-ap1-1-sidekiq/;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
  }location /rails-ap1-2-sidekiq/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://your_domain/rails-ap1-2-sidekiq/;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
  }

Now you can access your sidekiq web at

https://www.domain.com/rails-api-1-sidekiq/retries

After that Sidekiq will look for assets in path /rails-api-1-sidekiq/. Here as per docker label rails-api-1-sidekiq indicates that it will point to the container rails-api-1 because it is configured forrails-api-1task and within that container, it will look for the assets in /rails-api-1-sidekiq/.path, You can see in below image

Similarly, you can setup multiple sidekiq on your reverse proxy.

Thank you!

This entry was posted in General, Ruby on Rails. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s