【发布时间】:2021-03-04 13:19:22
【问题描述】:
我使用 Nuxtjs 作为我的应用程序的前端和 Laravel 框架作为后端。 我将我的应用程序托管在 Apache Centos 服务器上。 在 localhost:3000 上使用 pm2 运行应用程序后,我无法在 Laravel 中获取用户的真实 IP。
我的 Centos 版本:
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1127.10.1.el7.x86_64
Architecture: x86-64
我的 Apache 版本:
Server version: Apache/2.4.43 (Unix)
我在 apache 中的虚拟主机:
<VirtualHost MY_IP:80>
UserDir public_html
DocumentRoot /var/www/html
ServerName example.com
ProxyPreserveHost On
RemoteIPHeader X-Forwarded-For
RemoteIPProxiesHeader X-Forwarded-By
<LocationMatch "/">
allow from all
Satisfy any
AllowOverride All
Require all granted
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</LocationMatch>
</VirtualHost>
我在 Laravel 中 AppServiceProvider 中的 boot 方法
public function boot()
{
Request::setTrustedProxies(['REMOTE_ADDR'], Request::HEADER_X_FORWARDED_FOR);
}
但是当我使用 request()->ip() 或request()->getClientIp() 时,经过这些步骤
在 Laravel 中,它给了我服务器 ip。
如何在我的 Laravel 应用程序中获取真实用户 ip?
【问题讨论】:
标签: laravel apache vue.js nuxt.js reverse-proxy