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?
- When a client come to the website by typing in address bar, the request will be handled by nodejs first.
- Then, nodejs will send a request to laravel for data.
- 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…