如何在 Nuxt.js 中获取客户端 IP 地址?
How can I get client IP address in Nuxt.js?
我想知道如何在 Nuxt.js 中获取客户端 IP 地址。
我在客户端使用 Nuxt.js,在服务器端使用 Express。
我以为当服务器端收到请求时,我可以通过 req.connection.remoteAddress
或 req.headers['x-forwarded-for'].split(',').pop()
在服务器端获取客户端 ip 地址。
但是,通过上面的代码,我只能得到 ::ffff:127.0.0.1
.
我google发现如果我用的是nginx
,我可以通过设置文件设置x-forwarded-for
,但是我没有用nginx
。
我可能正在使用 @nuxtjs/proxy
(但我不确定。对不起,我不太了解代理。)
modules: [
'@nuxtjs/axios',
'@nuxtjs/pwa',
'@nuxtjs/proxy',
'@nuxtjs/auth',
],
proxy: {
'/api/': {
target: process.browser ? '' : 'http://localhost:3001',
pathRewrite: { '^/api/': '/' },
},
}
(nuxt.config.js
的一部分。)
看了@nuxtjs/proxy的文档,好像可以设置'x-forwarded-for'
header,但是我不知道怎么获取自己的客户端ip地址,我也设置不了header.
作为另一种方式,我尝试将客户端 ip 地址设置为参数,并使用以下代码。
client side
if (process.browser) {
const ip = await this.$axios.$get('/ip/')
}
// send an request with this ip as a parameter.
nuxt.config.js
proxy: {
'/api/': {
target: process.browser ? '' : 'http://localhost:3001',
pathRewrite: { '^/api/': '/' },
},
'/ip/': {
target: process.browser ? 'http://icanhazip.com' : 'http://icanhazip.com',
pathRewrite: { '^/ip/': '/' },
},
},
虽然我使用了 process.browser
.
但我只能得到相同的服务器端全局 IP 地址
请帮我获取 Nuxt.js 中的客户端 ip 地址。
谢谢。
对于 Nuxt,请查看本讨论中描述的代码 -
https://github.com/nuxt/nuxt.js/issues/2914
基本上,他们的想法是将客户端 IP 存储在代理收到的请求中
export default ({ req, store }) => {
if (process.server) {
// https://github.com/nuxt/nuxt.js/issues/2914
const ip = req.connection.remoteAddress || req.socket.remoteAddress
store.commit('SET_IP', ip)
}
if (process.client) {
localStorage.setItem('ip', store.getters.ip)
}
}
我想知道如何在 Nuxt.js 中获取客户端 IP 地址。
我在客户端使用 Nuxt.js,在服务器端使用 Express。
我以为当服务器端收到请求时,我可以通过 req.connection.remoteAddress
或 req.headers['x-forwarded-for'].split(',').pop()
在服务器端获取客户端 ip 地址。
但是,通过上面的代码,我只能得到 ::ffff:127.0.0.1
.
我google发现如果我用的是nginx
,我可以通过设置文件设置x-forwarded-for
,但是我没有用nginx
。
我可能正在使用 @nuxtjs/proxy
(但我不确定。对不起,我不太了解代理。)
modules: [
'@nuxtjs/axios',
'@nuxtjs/pwa',
'@nuxtjs/proxy',
'@nuxtjs/auth',
],
proxy: {
'/api/': {
target: process.browser ? '' : 'http://localhost:3001',
pathRewrite: { '^/api/': '/' },
},
}
(nuxt.config.js
的一部分。)
看了@nuxtjs/proxy的文档,好像可以设置'x-forwarded-for'
header,但是我不知道怎么获取自己的客户端ip地址,我也设置不了header.
作为另一种方式,我尝试将客户端 ip 地址设置为参数,并使用以下代码。
client side
if (process.browser) {
const ip = await this.$axios.$get('/ip/')
}
// send an request with this ip as a parameter.
nuxt.config.js
proxy: {
'/api/': {
target: process.browser ? '' : 'http://localhost:3001',
pathRewrite: { '^/api/': '/' },
},
'/ip/': {
target: process.browser ? 'http://icanhazip.com' : 'http://icanhazip.com',
pathRewrite: { '^/ip/': '/' },
},
},
虽然我使用了 process.browser
.
请帮我获取 Nuxt.js 中的客户端 ip 地址。 谢谢。
对于 Nuxt,请查看本讨论中描述的代码 - https://github.com/nuxt/nuxt.js/issues/2914
基本上,他们的想法是将客户端 IP 存储在代理收到的请求中
export default ({ req, store }) => {
if (process.server) {
// https://github.com/nuxt/nuxt.js/issues/2914
const ip = req.connection.remoteAddress || req.socket.remoteAddress
store.commit('SET_IP', ip)
}
if (process.client) {
localStorage.setItem('ip', store.getters.ip)
}
}