【问题标题】:Nuxt or Laravel does not get visitor's public ipNuxt 或 Laravel 无法获取访问者的公共 ip
【发布时间】:2019-12-22 03:10:17
【问题描述】:

有 Nuxt + Laravel (Apiato) 的基本设置。对于 nuxt,我使用的是带有 nginx 的反向代理,而对于 Laravel,我也使用了 nginx。

当我从 Postman 访问端点时,我得到了我的公共 IP,但是当涉及到实时站点上的 Nuxt 时,IP 是 127.0.0.1。

我使用request()->ip()获取IP。

我已经尝试添加 proxy_set_header,但是要么我做错了,要么根本没有应用。

两个应用都使用同一个服务器。

努克斯

server {
    index index.html;
    server_name example.com

    location / {
        proxy_pass http://localhost:8002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-User-Agent $http_user_agent;
        proxy_set_header X-Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

Laravel

server {
    index index.html;
    server_name api.example.com;
    root /var/www/api.example.com/public;
    index index.php index.html index.htm;
    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }

    error_log /var/log/nginx/api_error.log;
    access_log /var/log/nginx/api_access.log;
}

【问题讨论】:

    标签: laravel vue.js nginx server-side-rendering nuxt.js


    【解决方案1】:

    如果你有代理,试试这个:

    const ip = req.headers['x-forwarded-for'].split(',').pop() ||
      req.connection.remoteAddress
    

    请执行以下操作:

    const ip = req.connection.remoteAddress
    

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 2018-08-23
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多