Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

nginx - Laravel TrustProxies can not get real ip for Throttle

I am running a project which combines both nodejs and php, and the part of nodejs is a SSR nuxt (sth like next).

and requests for /api/* will be handled by php which constructed by laravel, requests for /* will be handled by nodejs which is running on 3000 port.

The key part of nginx configs is below:

    location /api/ {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location / {
        proxy_pass http://127.0.0.1:3000;
    }

How it works?

  1. When a client come to the website by typing in address bar, the request will be handled by nodejs first.
  2. Then, nodejs will send a request to laravel for data.
  3. Finally, the nodejs will send the html which had already been rendered with data from laravel to the client.

So, here is the problem:

I am using a Throttle in laravel, which means laravel needs the real ip.

Every time when a new user come to the website by typing in address bar, there is a request sent from nodejs, and the laravel will considered its ip is 127.0.0.1, a 429 Too Many Requests response will be got by nodejs even the real requests are sent from different ips.


How I try to solve:

I configed the configs/trustedproxy.php:

<?php
return [
    'proxies' => '127.0.0.1',
    'headers' => IlluminateHttpRequest::HEADER_X_FORWARDED_ALL,
];

added proxy_set_header X-Forwarded-For $remote_addr; in nginx config:

    proxy_set_header X-Forwarded-For $remote_addr;
    location /api/ {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location / {
        proxy_pass http://127.0.0.1:3000;
    }

I am sure I registered the TrustProxies::class as middleware in appHttpKernel.php, also restarted nginx, but it still not work. Laravel still can't get the real ip.

I am using Laravel 8.12.

How can I solve it? I googled it but nothing helped.

Thanks a lot! I am not good at English, sorry for grammatical errors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally, I work out this bug.

Strictly speaking, it's not a proxy which you can't handle it as same as proxy exactly.

The nodejs will be considered as a client by Laravel, so some javascript have to be modified:

When a client come to the website by typing in address bar, nodejs should get the real ip in server side(not the php nor client's browser), then make a request to laravel with that ip which will be added as X-Forwarded-For

so, in a SSR Nuxt project:

plugins/axios.js

import axios from 'axios';
export default ({ req }) => {
    axios.interceptors.request.use(request => {
        ...
        if (process.server) {
            request.headers.common['X-Forwarded-For'] = req.headers['x-forwarded-for'];
        }
        return request;
    });
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...