I deployed my angular front end and my laravel backend to an ubuntu VM onto an nginx web server.
Now the front end works fine and is accessible through my URL, however the backend API does not work – I get 404 Errors about it.
here is my nginx config:
server {
listen 443 ssl;
server_name my_ip myurl.de www.myurl.de;
root /var/www/html/dist;
index index.html;
ssl_certificate /etc/nginx/cert.cer;
ssl_certificate_key /etc/nginx/prvt.key;
location / {
try_files $uri $uri/ /index.html;
}
location /3rdpartylicenses.txt {
alias /var/www/html/dist/3rdpartylicenses.txt;
}
location /assets {
alias /var/www/html/dist/assets;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /index.nginx-debian.html {
log_not_found off;
access_log off;
}
location ^~ /api {
root /backend/LeagueOf5v5Backend/public;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Adjust the PHP-FPM socket path if needed
}
location ~ /.ht {
deny all;
}
error_page 404 /index.html;
# Additional Nginx configuration settings can go here.
}
I tried testing it through https://nginx.viraptor.info which returned it tested for the /api prefix.
However using postman and the URL I get the 404 error. But when I am at my local machine and test it with post man through localhost:8000/api/… it works just fine.
Any help appreciated!
EDIT: This is a config file for both angular and laravel in one
2
Answers
This is my docker compose gninx config:
Of course, you are using a socket, I am not, so disregard that diffence, but then I think you have a wrong
location /
, you use/index.html
when it should be like me.Also see you are using
index index.html
, it should beindex index.php
, because you are serving Laravel.@Frevelman the thing, I understand from your
virtual host
code is that you want to call the backendLaravel
service when you hit<your-domain>/api
.According to your instruction, your route will be following
<your-domain>/api/api/
if I’m not wrong and you had defined all routes in the api.php route file.Plz try it, I think that will do work for you.
To run Laravel on Port:
you can write both codes in a single file as you write in the above example.You will need to update your configuration file with it.
By this configuration. You will
myurl.de/
it will serve theAngular
application and when you hitmyurl.de:8001/
it will serveLaravel
application’s routes inweb.php
and when you hitmyurl.de:8001/api
it will serve Laravelapi.php
routes file.Advantages
The same SSL certificate will be used for both applications because the domain/sub-domain for both apps is the same. We are just using port forwarding to redirect requests to Angular or Laravel.
When the request comes to
Angular
then php-fpm will not serve it and load on memory will be reduced.