采用服务器渲染模式
store/index.js
export const state = () => ({
domain: '',
});
export const mutations = {
setDomain(state, domain) {
state.domain = domain;
},
};
// 自动执行
export const actions = {
nuxtServerInit(store, context) {
store.commit('setDomain', context.req.headers.host);
},
};
export const getters = {
domain: (state) => state.domain,
};
nginx 暴露请求头
upstream tyh21 {
server 127.0.0.1:20010; #nuxt项目 监听端口
keepalive 64;
}
server {
listen 80;
server_name tyh21.top;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Nginx-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_pass http://tyh21; #反向代理
}
}
静态渲染
mounted () {
console.log(location.host);
}