在使用 nginx 部署应用程序时访问 URL 路由时如何修复 404 not found?

use*_*827 5 nginx docker kubernetes

我正在尝试将基于角度的 web 应用程序部署到 kube,我正在尝试使用 nginx 将其部署为静态内容。
问题是 URL 不可访问;这意味着如果我尝试访问https://website.company.com/route1,我会收到一个404 not found错误,我可以访问https://website.company.com然后route1从主页点击它会带我到https://website .company.com/route1

关于我在这里缺少什么的任何指导?

以下是我的Dockerfilenginx.conf.erb

Dockerfile

FROM nginx
EXPOSE 80
COPY www/* /usr/share/nginx/html
COPY nginx.conf.erb /etc/nginx/nginx.conf.erb
Run Code Online (Sandbox Code Playgroud)

nginx.conf.erb

worker_processes 1;
error_log stderr;
pid /app/.nginx-tmp/pid;
daemon off;

events {
        worker_connections 768;
}

http {
        include mime.types;
        client_body_temp_path /app/.nginx-tmp/body    1 2;
        proxy_temp_path       /app/.nginx-tmp/cache   1 2;
        fastcgi_temp_path     /app/.nginx-tmp/fastcgi 1 2;
        uwsgi_temp_path       /app/.nginx-tmp/uwsgi   1 2;
        scgi_temp_path        /app/.nginx-tmp/scgi    1 2;
        access_log off;
        server {
                listen <%= ENV['PORT'] %>;
                server_name _;
                root /app/www;
                index index.html;
                location = /__health {
                        return 200 'OK';
                        add_header Content-Type text/plain;
                }

                location / {
                        try_files $uri $uri/ /index.html;
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

更新:

Dockerfile:

FROM nginx
RUN mkdir -p /app/.nginx-tmp
COPY www/* /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
Run Code Online (Sandbox Code Playgroud)

配置文件

worker_processes 1;
error_log stderr;
pid /app/.nginx-tmp/pid;
events {
        worker_connections 768;
}
http {
        include mime.types;
        client_body_temp_path /app/.nginx-tmp/body    1 2;
        proxy_temp_path       /app/.nginx-tmp/cache   1 2;
        fastcgi_temp_path     /app/.nginx-tmp/fastcgi 1 2;
        uwsgi_temp_path       /app/.nginx-tmp/uwsgi   1 2;
        scgi_temp_path        /app/.nginx-tmp/scgi    1 2;
        access_log off;
        server {
                listen 80;
                server_name _;
                root /app/www;
                index index.html;
                location = /__health {
                        return 200 'OK';
                        add_header Content-Type text/plain;
                }
                location / {
                        try_files $uri $uri/ /index.html;
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

错误:

worker_processes 1;
error_log stderr;
pid /app/.nginx-tmp/pid;
events {
        worker_connections 768;
}
http {
        include mime.types;
        client_body_temp_path /app/.nginx-tmp/body    1 2;
        proxy_temp_path       /app/.nginx-tmp/cache   1 2;
        fastcgi_temp_path     /app/.nginx-tmp/fastcgi 1 2;
        uwsgi_temp_path       /app/.nginx-tmp/uwsgi   1 2;
        scgi_temp_path        /app/.nginx-tmp/scgi    1 2;
        access_log off;
        server {
                listen 80;
                server_name _;
                root /app/www;
                index index.html;
                location = /__health {
                        return 200 'OK';
                        add_header Content-Type text/plain;
                }
                location / {
                        try_files $uri $uri/ /index.html;
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

β.ε*_*.βε 5

事实上,NGINX 不会理解nginx.conf.erb文件。
看起来Passenger会允许这种类型的文件类型,但“vanilla” NGINX 图像不会。

所以你会希望你的文件是一个简单的.conf文件,并被复制到现有的/etc/nginx/nginx.conf文件中。

FROM nginx
EXPOSE 80
COPY www/* /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf 
## off course, you'll have to rename your host file to nginx.conf too.
Run Code Online (Sandbox Code Playgroud)

请注意,为此,您还必须在配置文件中修复这一行:

listen <%= ENV['PORT'] %>;
Run Code Online (Sandbox Code Playgroud)

因为这是一个 ruby​​ erb 语法,它不会被 NGINX 接受或解释。

而且,因为您containerPort明确指定它公开3000容器的端口,所以我希望您可以安全地使用:

listen 3000;
Run Code Online (Sandbox Code Playgroud)

但是如果不是这种情况,可以使用docker镜像文档中提示的解决方法

开箱即用的 nginx 不支持大多数配置块内的环境变量。但是这个镜像有一个功能,会在nginx启动前提取环境变量。

这是一个使用 docker-compose.yml 的示例:

web:
  image: nginx
  volumes:
   - ./templates:/etc/nginx/templates
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80
Run Code Online (Sandbox Code Playgroud)

默认情况下,该函数读取模板文件/etc/nginx/templates/*.template并将执行结果输出envsubst/etc/nginx/conf.d.

因此,如果您放置templates/default.conf.template包含如下变量引用的文件:

listen       ${NGINX_PORT};
Run Code Online (Sandbox Code Playgroud)

输出/etc/nginx/conf.d/default.conf像这样:

listen       80;
Run Code Online (Sandbox Code Playgroud)

来源:https : //hub.docker.com/_/nginx