Suppose I have a domain example.com and it is located in www/wwwroot/example. I also have created a subdomain api.example.com by creating a CNAME api pointing to example.com. For the usage purposes, I have created a directory named api which is located on www/wwwroot/api which is certainly not in the root direcory of my domain.
I will host different independent project's API codes in the API directory (www/wwwroot/api).
Suppose I have a project named DemoProject and I have created a direcorty along with the necessary API codes inside the directory (www/wwwroot/api/DemoProject).
Now what I want is if I call api.example.com/DemoProject it should point to the directory www/wwwroot/api/DemoProject. How can I do it in NGINX?
Here I need some rules so that I can access any directory inside www/wwwroot/api whenever I call api.example.com/DirectoryName matching with the directory name.
Like:
- api.example.com/Google should point to www/wwwroot/api/Google
- api.example.com/Facebook should point to www/wwwroot/api/Facebook
- api.example.com/Netflix should point to www/wwwroot/api/Netflix
Here is my current example.com domain's NGNIX config file
server
{
listen 80;
listen 443 ssl http2;
server_name example.com api.example.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/panel_ssl_site;
#SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /www/server/panel/vhost/cert/example.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/example.com/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;
#SSL-END
#referenced redirect rule, if commented, the configured redirect rule will be invalid
include /www/server/panel/vhost/nginx/redirect/example.com/*.conf;
#ERROR-PAGE-START Error page configuration, allowed to be commented, deleted or modified
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP reference configuration, allowed to be commented, deleted or modified
#SECURITY-START Hotlink protection configuration
location ~ .*\.(jpg|jpeg|gif|png|js|css)$
{
expires 30d;
access_log off;
valid_referers example.com;
if ($invalid_referer){
return 404;
}
}
#SECURITY-END
include enable-php-00.conf;
#PHP-INFO-END
#REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
include /www/server/panel/vhost/rewrite/example.com.conf;
#REWRITE-END
# Forbidden files or directories
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
# Directory verification related settings for one-click application for SSL certificate
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log /dev/null;
access_log off;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log /dev/null;
access_log off;
}
access_log /www/wwwlogs/example.com.log;
error_log /www/wwwlogs/example.com.error.log;
}
I am very very new to the administritive task. I am doing it for few of my personal projects. I am having difficulties to face this. I apologize in advance if this is a simple problem. Also thanks in advance.
Edit
In the config, in the line
server_name example.com api.example.com;
I have both domain and subdomain because I have enabled redirection. If someone calls only api.example.com then it will be redirected to example.com.