Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
NGINX Config File for Microservice Subfolder Architecture

Please bear with me as I don't have much experience configuring Nginx.

I want to create micro-services as subfolders. eg: www.example.com/users

USERS is the micro-service so USERS will be the name of the subfolder as well.

The website can be hosted at /var/www/html/ or /var/www/html/example.com/public_html (Assuming this one for this question)

So inside the main folder, /var/www/html/example.com/public_html there are many folders, each representing a micro-service.

/var/www/html/example.com/public_html/users/
/var/www/html/example.com/public_html/students/
/var/www/html/example.com/public_html/teachers/
/var/www/html/example.com/public_html/parents/

Each micro-service will have a latest folder which holds the code to be executed.

/var/www/html/example.com/public_html/users/latest/

Each latest folder has 2 folders - APP which holds the SPA front end code and API which holds the API code

/var/www/html/example.com/public_html/users/latest/app/
/var/www/html/example.com/public_html/users/latest/api/

I would like to add and delete folders as required without making changes to the Nginx config file. If the folder exists, i would like the relevant front end code to be displayed. Else 404

I am aware that I can add the folders as required in Nginx location but that is not what I want.

(DON'T WANT TO DO THE BELOW CODE)

location /users {
  alias /var/www/html/example.com/public_html/users/latest/app
}

location /api/users {
  alias /var/www/html/example.com/public_html/users/latest/api
}

I am trying to figure out if I can configure NGINX to point to the relevant folder based on the url.

This is what I have so far but it just shows as 404 for everything I am trying. I am working towards writing the API in Go-lang but since I have experience using PHP I am taking that route first before switching to Go.

server {

    listen 80;
    listen [::]:80;
    server_name _;

    root /var/www/html/example.com/public_html;
    index index.html;

    location ^/<service>/<additional-optional-parameters-can-go-here>$ {
      root /var/www/html/example.com/public_html/<service>/latest/app;
      try_files $uri $uri/ =404
    }

    location ^/api/<service>/<additional-optional-parameters-can-go-here>$ {
      root /var/www/html/example.com/public_html/<service>/latest/api;
      try_files $uri $uri/ =404

      location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

    }

    location ~ /\.ht {
        deny all;
    }

}

enter image description here

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think you can use regex in the location and server lines.

Perhaps that will help

server_name ~^(?<subdomain>.+)\.domain\.tdl$ ~^(?<subdomain>.+)\.domain2\.tdl$ domain.tdl domain2.tdl;

location ~ ^/sitename/[0-9a-z]+/index.php$ {
    fastcgi_pass phpcgi;
}

location ~ \.php$ {
    return 404;
}

location ~ ^/a/b/(?<myvar>[a-zA-Z]+) {
   # use variable $myvar here
   if ($myvar = "sth") { ... }
}

# Example using the subdomain var created in last steps
sub_filter_once off;
sub_filter 'Site Text' '$subdomain';
sub_filter 'Site Text' '$myvar';

Personally I like to create different conf files for each service, then issue a "service nginx reload". Perhaps you could also automate the conf file generation.

over 4 years ago · Santiago Trujillo Report

0

Just incase anyone else is trying a similar approach. I went on Freelancer.com and hired someone to help me.

location ~ ^/api/([a-z]+)/.*?$ {
    alias $folder_path/$1/$api_path;
    rewrite ^/api/([a-z]+)/.*?$ /$1/$api_path last;
}

location ~ ^/([a-z]+)/(?:(?!\blatest\b).)*$ {
    alias $folder_path/$1/$app_path;
    rewrite ^/([a-z]+)/(?:(?!\blatest\b).)*$ /$1/$app_path last;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!