Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Created July 31, 2018 10:22
Show Gist options
  • Save ultim8k/6bbedae79bad9a0a3c38f21bcc1b6d91 to your computer and use it in GitHub Desktop.
Save ultim8k/6bbedae79bad9a0a3c38f21bcc1b6d91 to your computer and use it in GitHub Desktop.
Docker setup for node.js with nginx proxy

Docker setup for node.js with nginx proxy

File strcuture

_
|
|-- docker-compose.yml
|
|-- nginx
|     |
|     \_ default.conf
|
|-- nodejs
|     |
|     \_ index.js
|
\__ static
      |
      \_ test.txt
  
version: "3.3"
services:
nginx:
image: nginx:alpine
ports:
- "8000:80"
- "443:443"
volumes:
- ./static:/srv/www/static
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- nodejs
nodejs:
image: node:alpine
environment:
NODE_ENV: production
working_dir: /home/app
restart: always
volumes:
- ./nodejs:/home/app
command: ["node", "index"]
server {
listen 80;
root /srv/www;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
try_files $uri @nodejs;
}
location @nodejs {
proxy_pass http://nodejs:3000;
}
}
const http = require('http');
const port = 3000;
const requestHandler = (request, response) => {
console.log(request.url);
response.end('Hello Node.js Server!');
}
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err);
}
console.log(`server is listening on ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment