Skip to content

Instantly share code, notes, and snippets.

@caltuntas
Last active November 20, 2024 11:36
Show Gist options
  • Save caltuntas/2bf1bdf3090aa3c0021f291548bcc072 to your computer and use it in GitHub Desktop.
Save caltuntas/2bf1bdf3090aa3c0021f291548bcc072 to your computer and use it in GitHub Desktop.
nginx-client-ip
version: '3.8'
services:
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- backend
backend:
build:
context: ./backend
container_name: backend
ports:
- "3000:3000"
FROM node:18-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
events {}
http {
server {
listen 80;
location / {
proxy_pass http://backend:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
{
"name": "backend",
"version": "1.0.0",
"main": "server.js",
"dependencies": {
"express": "^4.18.2"
},
"scripts": {
"start": "node server.js"
}
}
const express = require('express');
const app = express();
app.get('/', (req, res) => {
const clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(`Client IP: ${clientIp}`);
res.send(`Your IP is: ${clientIp}`);
});
app.listen(3000, () => {
console.log('Backend service is running on port 3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment