Express behind Nginx showing original IP demo in 10 minutes
I have a problem with IP when my express app is behind Nginx.
I’ve gone through this guide and it works.
Below I will demo how I manage to get the real IP.
Server setup
I go for AWS EC2 Ubuntu 20.04 quick setup and ssh into it.
Nginx Minimal Setup
Install Nginx.
Edit the config file in /etc/nginx/site-enabled
server {
listen 80;
location /this-is-me/ {
proxy_pass http://localhost:9999/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Express Minimal Setup
Set up your minimal express app npm init --yes
I add type: module
here in package.json
since I want to use import
syntax.
import express from 'express'
const app = express()
app.use((req, res) => {
return res.json({
timestamp: Date.now(),
your_ip: req.ip
})
})
app.set('trust proxy', ['loopback', 'linklocal','uniquelocal'])
app.listen(9999, () => {
console.log('listening on 9999')
})
Run it with node app.js
Check the result locally with curl http://localhost:9999
Testing
curl http://<your_ip_here>/this-is-me
You can check result against Google search of “my ip”
Hope this help!