首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nuxt或Laravel未获取访客的公网ip

Nuxt或Laravel未获取访客的公网ip
EN

Stack Overflow用户
提问于 2019-08-16 01:26:30
回答 1查看 341关注 0票数 0

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

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

我用request()->ip()得到了IP地址。

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

这两个应用程序使用相同的服务器。

Nuxt

代码语言:javascript
复制
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;
    }

}

拉威尔

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2019-08-16 04:18:17

如果您有代理,请尝试以下操作:

代码语言:javascript
复制
const ip = req.headers['x-forwarded-for'].split(',').pop() ||
  req.connection.remoteAddress

否则,请执行以下操作:

代码语言:javascript
复制
const ip = req.connection.remoteAddress
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57513688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档