带有本地化的Next.js-在带有入口配置的子路径上部署
我想实现的目标 我希望我的next.js应用程序能像这样部署并托管在我的域中- 我还希望它支持本地化,这样当用户进入带有检测到的区域设置的页面时,应用程序将执行如下重定向- 有什么问题 当前的行为是,当用户使用默认区域设置进入页面时,应用程序将正确加载。但是,如果用户将不同的语言环境设置为其首选语言环境,而不是重定向到,则应用程序会重定向到。我还应该提到,当我手动输入时,将加载正确的区域设置,并且一切都按预期工作 我当前的配置是什么 这是我的带有本地化的Next.js-在带有入口配置的子路径上部署,next.js,kubernetes-ingress,nginx-ingress,Next.js,Kubernetes Ingress,Nginx Ingress,我想实现的目标 我希望我的next.js应用程序能像这样部署并托管在我的域中- 我还希望它支持本地化,这样当用户进入带有检测到的区域设置的页面时,应用程序将执行如下重定向- 有什么问题 当前的行为是,当用户使用默认区域设置进入页面时,应用程序将正确加载。但是,如果用户将不同的语言环境设置为其首选语言环境,而不是重定向到,则应用程序会重定向到。我还应该提到,当我手动输入时,将加载正确的区域设置,并且一切都按预期工作 我当前的配置是什么 这是我的next.config.js文件: const isP
next.config.js
文件:
const isProd = process.env.NODE_ENV === 'production';
module.exports = {
// Use the CDN in production and localhost for development.
assetPrefix: isProd ? '/myapp/' : '',
async rewrites() {
if (!isProd) {
return [];
}
return [{ source: '/myapp/_next/:path*', destination: '/_next/:path*' }];
},
images: {
path: isProd ? '/myapp/_next/image' : '/_next/image',
},
i18n: {
locales: ['pl', 'en-US'],
defaultLocale: 'pl',
},
webpack: (config) => {
config.module.rules.push({
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
});
return config;
},
webpackDevMiddleware: (config) => {
return config;
},
};
以下是我的入口配置的相关部分:
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: example-ingress
namespace: default
annotations:
ingress.kubernetes.io/ssl-redirect: 'false'
kubernetes.io/ingress.class: nginx
kubernetes.io/ingress.global-static-ip-name: web-static-ip
kubernetes.io/tls-acme: 'true'
nginx.ingress.kubernetes.io/configuration-snippet: |
add_header X-Frame-Options "SAMEORIGIN";
add_header Content-Security-Policy "frame-ancestors 'none'";
nginx.ingress.kubernetes.io/limit-connections: ‘10’
nginx.ingress.kubernetes.io/limit-rpm: ‘60’
nginx.ingress.kubernetes.io/proxy-body-size: 100m
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- example.com
secretName: my-example-secret
rules:
- host: example.com
http:
paths:
- path: /myapp
pathType: ImplementationSpecific
backend:
serviceName: my-app-svc
servicePort: 3000
- path: /myapp/(.*)
pathType: ImplementationSpecific
backend:
serviceName: my-app-svc
servicePort: 3000
- path: /otherapp/(.*)
pathType: ImplementationSpecific
backend:
serviceName: my-other-app-svc
servicePort: 8080
- path: /(.*)
pathType: ImplementationSpecific
backend:
serviceName: yet-another-app-svc
servicePort: 80
解释
我之所以使用assetPrefix
、rewrites()
和images
配置,而不是使用nextjs提供的可用basePath
配置,是因为当我尝试将其设置为/myapp
时,我的应用程序在
非常感谢您的建议我建议您改用
basePath
。您得到https://example.com/myapp/myapp
可能是由于入口配置,您可能需要修改入口配置。