How to configure multiple directories with one domain name by Nginx

server {
    listen       *:80;
    server_name  admin.hiphop.top;

    location / {
        root   "D:\Office\PHPStudy\PHPTutorial\WWW\MyWork\VhiphopAdmin\dist";
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    location /api {
        root   "D:\Office\PHPStudy\PHPTutorial\WWW\MyWork\VhiphopAdminApi\public";
        index  index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php(.*)$  {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

configuration code as above
access admin.hiphop.top
can match to "D:OfficePHPStudyPHPTutorialWWWMyWorkVhiphopAdmindist"

access admin.hiphop.top/api
cannot match to "D:OfficePHPStudyPHPTutorialWWWMyWorkVhiphopAdminApipublic"

the dist directory is my front-end static file, and the public directory is the entry directory of the php api interface
how to configure it to match the dist directory when accessing admin.hiphop.top/*
and to access the admin.hiphop.top/api first match the public directory of php

it"s okay to configure two domain names because the browser has cross-domain problems, so now you want to use only one domain name

Feb.19,2022

in the case of http://admin.hiphop.top/api/test.html url , $uri is / api/test.html
using root , it is judged whether this file exists in the D:\ Office\ PHPStudy\ PHPTutorial\ WWW\ MyWork\ VhiphopAdminApi\ public\ api directory

.

there are two ways to access the correct file:

  1. root change to alias

    alias "D:\Office\PHPStudy\PHPTutorial\WWW\MyWork\VhiphopAdminApi\public\";
  2. Add a api directory under the public directory, and then configure it unchanged

attachment: the difference between alias and root

The difference between

alias and root lies in how to understand uri

after location .
  • root is root path + location path
  • alias is to replace location path
  • with alias path.

The

problem has been solved. Just add root to the last .php matching rule

    location ~ \.php(.*)$  {
        root   "D:\Office\PHPStudy\PHPTutorial\WWW\MyWork\VhiphopAdminApi\public";
        index  index.php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ee4a-2b7b2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ee4a-2b7b2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?