使用NGINX使用自定义URL路径服务Web应用

时间:2020-02-05 08:48:26

标签: angularjs angular nginx

我有一个Angular应用程序,我想使用NGINX(在Windows上)在本地提供它。如果我只是转到pip install pyobjc-core,则该应用程序可以运行,但是我想使用http://localhost作为基本路径来提供服务。这是我没有想法的地方。我从这里开始:

http://localhost/my/ui

我将dist文件夹复制到NGINX的html文件夹,以简化操作。 server { listen 80; server_name localhost; location / { root html; try_files $uri $uri/ index.html; } } 文件如下所示:

index.html

我尝试使用<!doctype html> <html> <head> <title>Angular-AngularJS hybrid thingie</title> <base href="/"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico" /> </head> <body ng-cloak> <app></app> <script src="bundle.js"></script> </body> </html> location,将一个或另一个设置为base href,甚至两个都设置,但是我只得到了404。

2 个答案:

答案 0 :(得分:0)

如果您的Angular App只是纯文件,请尝试将它们放入nginx-root子文件夹root / my / ui中。设置<base href=".">而不是“ /”将允许所有指向资源的链接解析为到http://localhost/my/ui的相对路径

答案 1 :(得分:0)

请注意,如果有多个匹配的位置块nginx,则选择前缀最长的位置块。仅包含/的位置块提供了最短的前缀,其长度为1,因此只有在所有其他位置块均不能提供匹配项的情况下,才会使用该块。

您需要更改:

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

对此:

location /my/ui {
        root html;
        try_files $uri $uri/ index.html;
    }
相关问题