NGINX subdirectories using alias

$ sudo apt-get update 
$ sudo apt-get install nginx -y

Untitled

  • app 1 (index.html)

Untitled

  • app 2 (index.html)

Untitled


config sites-available

  • default
server {
	listen 80;
	listen [::]:80;

	server_name _;
	root /var/www/html;

	access_log /etc/nginx/sites-available/log/access.log;
	error_log /etc/nginx/sites-available/log/error.log;

	location /app1/ {
		alias /var/www/html/app1/;
	}
	location /app2/ {
		alias /var/www/html/app2/;
	}
}

test & restart nginx

sudo nginx -t
sudo nginx -s reload

  • GET /app1

Screen Shot 2564-12-18 at 17.58.52.png

  • GET /app2

Screen Shot 2564-12-18 at 17.59.01.png


PHP-fpm

Untitled

....
location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
...
server {
	listen 80;
	listen [::]:80;

	server_name _;
	root /var/www/html;

	access_log /etc/nginx/sites-available/log/access.log;
	error_log /etc/nginx/sites-available/log/error.log;

	location /app1/ {
		alias /var/www/html/app1/;
		index index.php; # new
	}
	location /app2/ {
		alias /var/www/html/app2/;
		index index.php; # new
	}
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
	}
}

test & restart nginx

sudo nginx -t
sudo nginx -s reload

Screen Shot 2564-12-18 at 18.09.35.png

Screen Shot 2564-12-18 at 18.09.45.png