For several days I’ve had a configuration problem with Nginx that I can’t solve.
I’m a beginner, and I’d just like to run my Spring Boot application on the main domain name, and run simple applications (to test, initially simple HTML pages) on sub-domains (of the same domain name).
Preliminary remark:
-
I’ve built my API with Spring Boot, which uses a TomCat web server. Spring Boot APIs run on port 8080 by default.
A Spring Boot application can therefore run without Nginx -
So I don’t have any configuration for my API, which is accessible via the main domain name (which does point to the VPS server’s IP address), which is:
http://musicstoreapi.com:8080/api/products.
What I want to do:
- Run my Spring Boot API only on the main domain name, i.e. musicstoreapi.com, on port 8080. It works.
- When calling the sub-domains api1.musicstoreapi.com, api2.musicstoreapi.com, api3.musicstoreapi.com, these sub-domains must return a simple HTML page.
I therefore set up an Nginx configuration for these sub-domains so that the web server displays the correct HTML page (which is in /var/www/api1 for the sub-domain api1.musicstoreapi.com, in /var/www/api2 for the sub-domain api2.musicstoreapi.com, and in /var/www/api3 for the sub-domain api3.musicstoreapi.com.)
What’s happening now ? What’s the problem ?
- When I call http://musicstoreapi.com:8080/api/products I get the data from my Spring Boot API correctly. - However, I don’t understand why I’m getting my Spring Boot API data for:
http://api1.musicstoreapi.com:8080/api/products or
http://api2.musicstoreapi.com:8080/api/products or
http://api3.musicstoreapi.com:8080/api/products.
I’m supposed to get API datas ONLY for the main domain name not for the sub-domains !
So normally I should expect to get a 404 for :
http://api1.musicstoreapi.com:8080/api/products or
http://api2.musicstoreapi.com:8080/api/products or
http://api3.musicstoreapi.com:8080/api/products. - As for the sub-domains, http://api1.musicstoreapi.com, and http://api2.musicstoreapi.com, the server should display their respective HTML pages.
I’ll try to be as precise as possible so that you can help me solve my configuration problem.
Diagram of my configuration problem:
In case I’ve expressed myself badly, I thought I’d draw a diagram so that you can clearly understand the problem, i.e. what I’m getting versus what I’m supposed to be getting:
Configuration & files
1. DNS
Here’s a screenshot of the DNS configuration for my domain name:
Name : musicstoreapi.com
Type : A
Value : IP VPS
Name : www. musicstoreapi.com
Type : A
Value : IP VPS
Name : api1. musicstoreapi.com
Type : CNAME
Value: musicstoreapi.com
Name : api2. musicstoreapi.com
Type : CNAME
Value: musicstoreapi.com
Name : api3. musicstoreapi.com
Type : CNAME
Value: musicstoreapi.com
2. Jar file (for my Spring Boot Application)
-----@-------: /etc/nginx/sites-enabled# cd /home/applications
-----@-------: /home/applications# ls
spring-boot-ecommerce-0.0.1-SNAPSHOT.jar
Precision:The name of the .jar file appears in red, but it seems to run correctly.
3. Service
To ensure that my Spring Boot application is accessible without interruption, I’ve created a service that appears to be active:
spring-boot-ecommerce.service - Spring Boot Ecommerce Application
Loaded: loaded (/etc/systemd/system/spring-boot-ecommerce.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-02-09 14:13:52 UTC; 2min 16s ago
Main PID: 476505 (java)
Tasks: 40 (limit: 19140)
Memory: 330.0M
CPU: 19.428s
CGroup: /system.slice/spring-boot-ecommerce.service
└─476505 /usr/bin/java -jar /home/applications/spring-boot-ecommerce-0.0.1-SNAPSHOT.jar
Feb 09 14:13:55 --------- java[476505]: 2024-02-09T14:13:55.389Z INFO 476505 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.2.13.Final
Feb 09 14:13:55 --------- java[476505]: 2024-02-09T14:13:55.392Z INFO 476505 --- [ main] org.hibernate.cfg.Environment : HHH000406: Using bytecode reflection optimizer
Feb 09 14:13:55 --------- java[476505]: 2024-02-09T14:13:55.756Z INFO 476505 --- [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
Feb 09 14:13:55 --------- java[476505]: 2024-02-09T14:13:55.825Z WARN 476505 --- [ main] org.hibernate.orm.deprecation : HHH90000025: MySQL8Dialect does not need to be specified explicitly >
Feb 09 14:13:55 --------- java[476505]: 2024-02-09T14:13:55.826Z WARN 476505 --- [ main] org.hibernate.orm.deprecation : HHH90000026: MySQL8Dialect has been deprecated; use org.hibernate.di>
Feb 09 14:13:56 --------- java[476505]: 2024-02-09T14:13:56.866Z INFO 476505 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta>
Feb 09 14:13:56 --------- java[476505]: 2024-02-09T14:13:56.869Z INFO 476505 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Feb 09 14:13:57 --------- java[476505]: 2024-02-09T14:13:57.316Z WARN 476505 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database q>
Feb 09 14:13:58 --------- java[476505]: 2024-02-09T14:13:58.406Z INFO 476505 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
Feb 09 14:13:58 --------- java[476505]: 2024-02-09T14:13:58.426Z INFO 476505 --- [ main] c.l.e.SpringBootEcommerceApplication : Started SpringBootEcommerceApplication in 5.779 seconds (process run>
4. UFW Status
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443 ALLOW Anywhere
8433:8443/tcp ALLOW Anywhere
3306 ALLOW Anywhere
8080 ALLOW Anywhere
88 ALLOW Anywhere
81 ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
8433:8443/tcp (v6) ALLOW Anywhere (v6)
3306 (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
88 (v6) ALLOW Anywhere (v6)
81 (v6) ALLOW Anywhere (v6)
5. Nginx Configuration File
server {
listen 80;
listen [::]:80;
server_name api1.musicstoreapi.com;
root /var/www/api1;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name www.api1.musicstoreapi.com;
root /var/www/api1;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name www.api2.musicstoreapi.com;
root /var/www/api2;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name api2.musicstoreapi.com;
root /var/www/api2;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
6. Symbolic link
In the /etc/nginx/sites-enabled folder, the symbolic link to the configuration file (which is in /etc/nginx/sites-available/musicstoreapi.com) has been made:
-----@-------:/etc/nginx/sites-enabled# cd /etc/nginx/sites-enabled
-----@-------:/etc/nginx/sites-enabled# ls -alh
total 12K
drwxr-xr-x 2 root root 4.0K Jan 27 16:30 .
drwx------ 12 root root 4.0K Feb 9 14:28 ..
-rw-r--r-- 1 root root 192 Nov 17 2022 default.conf
lrwxrwxrwx 1 root root 44 Jan 27 16:30 musicstoreapi.com -> /etc/nginx/sites-available/musicstoreapi.com
7. Directory and file permissions
(Same for api2 and api3)
-----@-------:/var/www/api1# ls -ld /var/www/api1
drwxrwxr-x 2 root root 4096 Feb 9 15:19 /var/www/api1
I hope all these clarifications will help me find the problem. I’ve been using chatGPT but it’s going round in circles and I’ve already done everything it suggested.