使用nginx服务的Docker中的Flutter Web应用程序

krcsximq  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(151)

我使用的是Go Router,它在开发模式下运行得非常好。在/下启动Web应用程序或使用/abc/def?var刷新也可以。当我使用flutter build web构建应用程序并在docker容器中启动nginx时,当我请求根目录以外的目录时,会得到404。
main.dart:

final GoRouter _router = GoRouter(
  initialLocation: '/me',
  errorBuilder: (context, state) {
    return const Page404();
  },
  routes: <RouteBase>[
    GoRoute(
      name: 'root',
      path: '/:tabid',
      builder: (BuildContext context, GoRouterState state) {
        return NavBarPage(tabId: state.params['tabid']!);
      },
      routes: <RouteBase>[
        GoRoute(
          name: 'blog',
          path: ':blogid/:title',
          builder: (BuildContext context, GoRouterState state) {
            return BlogDefault(blogId: state.params['blogid']!);
          },
        ),
      ],
    ),
  ],
);

停靠文件:

FROM ubuntu:22.10 as builder
# Setup standard utils
RUN apt-get update && apt-get install -y unzip xz-utils git openssh-client curl && apt-get upgrade -y && rm -rf /var/cache/apt
# Setup flutter & configure web sdk
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
RUN flutter config --enable-web
# release build of web app
WORKDIR /usr/src/app
COPY . ./
RUN flutter pub get
RUN flutter build web

FROM nginx:1.23.3-alpine as runtime
COPY --from=builder usr/src/app/build/web /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

user nginx;
worker_processes auto;
pid /run/nginx.pid;

http {
    server {
        listen 80;
        root /usr/share/nginx/html;

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}
hgb9j2n6

hgb9j2n61#

我把nginx.conf复制到了错误的文件夹中。感谢这个问题,我在这里问了我的问题后才发现:try_files in nginx.conf not working
下列default.conf在位置/etc/nginx/conf.d/中工作

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

相关问题