Nginx网址重写仅适用于/

时间:2020-02-05 07:08:59

标签: angular docker nginx

将我的角度应用程序移植到nginx

我的URL重写配置仅适用于/位置。如果我在浏览器中转到localhost:5000/login,则会收到404错误。

这是我的nginx.conf

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 {
        listen              80;
        root                /usr/share/nginx/html;

        location / {
            root               /usr/share/nginx/html;
            index              index.html;

            try_files          $uri $uri/ /usr/share/nginx/html/index.html;
        }


    }
}

日志

2020/02/05 06:44:29 [error] 6#6: *1 open() "/usr/share/nginx/html/login" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /login HTTP/1.1", host: "localhost:5000"
172.17.0.1 - - [05/Feb/2020:06:44:29 +0000] "GET /login HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" "-"

我想念什么?

3 个答案:

答案 0 :(得分:2)

您需要使用应用程序根文件设置try_files

try_files $uri $uri/ /usr/share/nginx/html/index.html;

参考:https://angular.io/guide/deployment

答案 1 :(得分:0)

您必须在位置块上使用正则表达式,如果不是,nginx只会将此位置应用于完全匹配。

location ~* /.* {
            root               /usr/share/nginx/html;
            index              index.html;

            try_files          $uri $uri/ /usr/share/nginx/html;
        }

您可以看到一些示例和更多信息here

答案 2 :(得分:0)

问题在于,默认的conf文件包含以下行

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

在达到我的location区块之前,这是匹配的规则。删除此行即可使其正常工作。