Nginx User IP Passthrough

This article introduces Nginx user IP passthrough, including its advantages, implementation details, and relevant resources. These steps can help improve efficiency when implementing Nginx user IP passthrough.

May 27, 2018 · 1 min read · 123 Words · -Views -Comments · Programming
Deployment Operations 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

Recently developing WEB, involved IP login, backend needs to get user access IP. Since backend is ExpressJS, req.ip can get it, but after actual deployment, found req.ip is always 127.0.0.1 Thought about WEB deployment using Nginx back then, suddenly understood this point, this is Nginx’s fault. Because Nginx acts as reverse proxy, in other words for our WEB backend, the requester is Nginx, so IP always being 127.0.0.1 makes sense.

IP Passthrough

Googled it, solution to this problem is doing Nginx IP passthrough, configuration as follows


location / {
           proxy_pass http://127.0.0.1:3001/;
           proxy_set_header      Host $host;
           proxy_set_header        X-Real-IP       $remote_addr;
           proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}

After modification OK, execute following commands

# Test if configuration is correct
$ nginx -t
# Reload configuration
$ nginx -s reload
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover