静态应用的 NGINX 服务器配置

NGINX server configuration for static apps

我有一个网站,它是一个包含其他应用程序的应用程序。 在主应用程序中,我将使用 iframe 加载其他应用程序。

主应用程序在域根目录中提供:http://example.com 其他应用程序在 /apps 路径下提供:

此外,应用程序是在 Polymer 中开发的,因此有客户端导航。我确保客户端没有使用 /apps 路径来正确访问 NGINX 服务器,但这也意味着对于 url 例如 http://example.com/view1 it should redirect to the index.html of the main app. And for url like http://example.com/apps/app-b/view1 它应该重定向到应用程序的 index.html -b.

我正在尝试配置 NGINX 来为这些静态应用程序提供服务。

server {
        listen 80;
        server_name example.com;

        root /var/www/example.com/main-app;
        index index.html;

        try_files $uri $uri/index.html /index.html;

        location ~ /apps/([a-z-]+) {
                alias /var/www/example.com/apps/;
        }
}

通过上述配置,我的主应用程序可以正确重定向到 index.html,例如 /view1 路径。

但是我的子应用程序出现 403 禁止错误。

directory index of "/var/www/example.com/apps/app-b" is forbidden, client: 127.0.0.1, server: example.com, request: "GET /apps/app-b/

我尝试了其他配置但没有成功(无限重定向导致 /index.html/index.html/index.html...)。

我确定文件系统权限所有目录都是 755,文件是 644。

不知道为什么要做目录索引。 感谢任何帮助。

alias 与正则表达式 location 一起使用时,您需要构建文件的完整路径。目前,您只捕获第二个路径元素。

话虽如此,您实际上不需要在此处使用 alias,因为可以使用 root 指令。

location /apps/ {
    root /var/www/example.com;
}

详情见this document