I’m having trouble getting my containerized Vue3 frontend to communicate with my containerized Express backend through nginx. When I run everything locally without Docker, the setup works perfectly using Vite’s proxy configuration.
Here’s my frontend API call:
try {
fetch('/createTask', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: taskName.value, description: taskDesc.value, completed: false })
})
.then(response => response.json())
.then(data => {
this.taskResult = data.message;
})
.catch(err => {
console.error('Request failed:', err);
});
} catch(e) {
alert('Error occurred: ' + e.message);
}
My vite.config.js proxy setup:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': resolve(__dirname, 'src')
}
},
server: {
proxy: {
'/createTask': {
target: 'http://localhost:8080',
changeOrigin: true,
secure: false
}
}
}
})
Backend endpoint:
app.post('/createTask', (req, res) => {
// process task creation
res.status(200).json({ message: req.body.description });
});
My nginx configuration:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
location /createTask {
proxy_pass http://backend:8080;
}
}
The frontend loads fine but API requests aren’t reaching the backend. Has anyone successfully configured nginx to route requests between separate containerized frontend and backend services?