我正在尝试将基于角度的 web 应用程序部署到 kube,我正在尝试使用 nginx 将其部署为静态内容。
问题是 URL 不可访问;意思是如果我尝试访问 https://website.company.com/route1 , 我得到一个 404 not found
错误,我可以访问 https://website.company.com然后点击 route1
从主页它带我到 https://website.company.com/route1
关于我在这里缺少什么的任何指导?
以下是我的 Dockerfile 和 nginx.conf.erb
Dockerfile:
FROM nginx
EXPOSE 80
COPY www/* /usr/share/nginx/html
COPY nginx.conf.erb /etc/nginx/nginx.conf.erb
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;
}
}
}
更新:Dockerfile:
FROM nginx
RUN mkdir -p /app/.nginx-tmp
COPY www/* /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
配置文件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;
}
}
}
错误:2020/12/18 01:43:53 [error] 31#31: *2 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 172.17.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost:8080" 2020/12/18 01:43:53 [error] 31#31: *1 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 172.17.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
最佳答案
确实,NGINX 不会理解 nginx.conf.erb
文件。
它看起来像 Passenger将允许这种类型的文件类型,但“ Vanilla ”NGINX image惯于。
所以你会希望你的文件是一个简单的 .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.
请注意,为此,您还必须在配置文件中修复这一行:listen <%= ENV['PORT'] %>;
因为这是一个 ruby erb 语法,它不会被 NGINX 接受或解释。而且,因为您的
containerPort
明确指定它公开端口 3000
的容器,我希望你会安全:listen 3000;
但是如果不是这种情况,可以使用docker镜像文档中提示的解决方法Out-of-the-box, nginx doesn't support environment variables inside most configuration blocks. But this image has a function, which will extract environment variables before nginx starts.
Here is an example using docker-compose.yml:
web: image: nginx volumes: - ./templates:/etc/nginx/templates ports: - "8080:80" environment: - NGINX_HOST=foobar.com - NGINX_PORT=80
By default, this function reads template files in
/etc/nginx/templates/*.template
and outputs the result of executingenvsubst
to/etc/nginx/conf.d
.So if you place
templates/default.conf.template
file, which contains variable references like this:listen ${NGINX_PORT};
outputs to
/etc/nginx/conf.d/default.conf
like this:listen 80;
来源:https://hub.docker.com/_/nginx
关于docker - 在使用 nginx 部署应用程序时访问 URL 路由时如何修复 404 not found?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65240209/