繁体   English   中英

如何始终重定向到 Nginx Docker 中的 index.html?

[英]How to always redirect to index.html in Nginx Docker?

我正在使用 Docker 的nginx:1.16.0-alpine图像作为服务反应应用程序(之前构建的),并且我想在任何情况下都重定向到index.html页面(在获取的 URL 中)

nginx.conf文件有以下内容:

user  nginx; worker_processes  auto;

error_log  /var/log/nginx/error.log warn; pid       
/var/run/nginx.pid;

events {
    worker_connections  1024; }

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

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

        location ~ ^/$ {
            rewrite  ^.*$  /index.html  last;
        }
    }
}

实际上添加了server部分,其他行是默认的!

Dockerfile内容也如下:

FROM nginx:1.16.0-alpine

COPY ./build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

为了确保,在从镜像构建容器并进入其中的 shell 之后,文件/etc/nginx/nginx.conf具有上述内容。

但问题是:当我浏览 url http://localhost:3000/login (例如)时,它不会重定向到http://localhost:3000/index.html 表明:

404 未找到 nginx/1.16.0

(Docker 容器运行在输出端口 3000 和本地端口 80)

有谁知道为什么它不起作用!?
(我也搜索过类似的方法,但没有成功!)

更新:
页面React-router 和 nginx没有解决问题。

注释以下行

# include /etc/nginx/conf.d/*.conf;

为什么? 由于线路

include /etc/nginx/conf.d/*.conf;

加载 default.conf 并忽略您的服务器配置。

此外,您需要在服务器中包含根信息(以前由 default.conf 提供)


如何繁殖

将以下2个文件放在同一个文件夹中并执行

docker build -t test . && docker run --rm -p 8080:80 test

文件

FROM nginx:1.16.0-alpine

# COPY ./build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

配置文件

user nginx; worker_processes auto;

error_log /var/log/nginx/error.log warn; pid
/var/run/nginx.pid;

events { worker_connections 1024; }

http { include /etc/nginx/mime.types; default_type application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

#include /etc/nginx/conf.d/*.conf;

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

    location ~ ^/$ {
        rewrite  ^.*$  /index.html  last;
    }
    listen       80;
    server_name  localhost;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM