I'm trying to run node and nginx each inside their own docker containers and proxy from nginx to node. I tried the configuration below without docker at first and it worked. However when using docker it's not working and gives Status Code:502 Bad Gateway when trying to connect to http://localhost/.
node server
var http = require('http');
http.createServer(function (req, res) {
res.setHeader('content-type', 'text/html');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Headers', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
res.end('<html>Hey</html>');
}).listen(3001);
docker-compose.yml
version: '3'
services:
app:
build: ./servers
nginx:
build: ./nginx
depends_on:
- app
links:
- app
volumes:
- ./nginx/conf:/etc/nginx/conf.d:ro
ports:
- 80:80
nginx conf
server {
listen 80;
listen [::]:80;
location / {
proxy_pass http://app;
}
}
app dockerfile
FROM node:alpine
RUN mkdir /app/
COPY ./server.js /app
EXPOSE 3001
WORKDIR /app
CMD node server
nginx dockerfile
FROM nginx:alpine
RUN rm /etc/nginx/conf.d/*
via Ced
No comments:
Post a Comment