Last active
November 20, 2024 11:36
-
-
Save caltuntas/2bf1bdf3090aa3c0021f291548bcc072 to your computer and use it in GitHub Desktop.
nginx-client-ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:18-alpine | |
WORKDIR /app | |
COPY package.json . | |
RUN npm install | |
COPY . . | |
EXPOSE 3000 | |
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "backend", | |
"version": "1.0.0", | |
"main": "server.js", | |
"dependencies": { | |
"express": "^4.18.2" | |
}, | |
"scripts": { | |
"start": "node server.js" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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