Junior — Middle
How can the interaction between the frontend and backend be configured using Nginx?
sobes.tech AI
Answer from AI
To configure interaction between the frontend and backend through Nginx, reverse proxying is usually used. Nginx accepts HTTP requests from the client and forwards them to the appropriate backend server, as well as serving static frontend files.
Example of an Nginx configuration where the frontend consists of static files and API requests are proxied to the backend:
server {
listen 80;
server_name example.com;
root /var/www/frontend; # Path to frontend static files
index index.html;
location / {
try_files $uri /index.html; # For SPA
}
location /api/ {
proxy_pass http://localhost:8080/; # Backend address
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Thus, the frontend and backend operate through a single domain, which simplifies CORS and routing.