用于清洁 url 的 Nginx 配置

Nginx configuration for clean urls

我有一个具有以下目录结构的网站:

.
├── assets
├── config
├── html
├── spec
└── vendor

我将其用作 运行 集成测试的开发设置,我需要一个服务器实例。我需要站点根目录位于 html 中,但我需要能够 link 访问资产中的资产。到目前为止,这就是我所拥有的,但它不起作用:

server {
    listen       9001;
    server_name  localhost;
    root /Users/hugo/src/feedback/www;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root /Users/hugo/src/feedback/www;
        index  index.html index.htm;
    }

    location /assets/ {
        root /Users/hugo/src/feedback/assets;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    # ... all the comments in the default config
}

正在提供页面,但找不到任何资产。它正在这里寻找:

http://localhost:9001/assets/bootstrap/dist/css/bootstrap.min.css

更新

您对目录结构的假设是正确的。 我将配置更改为以下结果相同:

server {
    listen       9001;
    server_name  localhost;
    root /Users/hugo/src/feedback/www/html;
    index  index.html index.htm;

    location / {
    }

    location /assets/ {
        # I also tried using 'root' instead of 'alias' but same results
        alias /Users/hugo/src/feedback/www/assets;
    }
}

我看到您设置了 root /Users/hugo/src/feedback/www,这对我来说意味着目录结构如下所示:

www
├── assets
├── config
├── html
├── spec
└── vendor

如果那是正确的,那么您可能需要将您的资产位置块修改为此(已添加 www):

location /assets/ {
    root /Users/hugo/src/feedback/www/assets;
}

假设您确实像 Blake Frederick 认为的那样设置了 www,那么您可以查看以下内容:

server {
    listen 9001;
    server_name  localhost;
    root /Users/hugo/src/feedback/www/html;
    index  index.html index.htm;

    location / {
        # The root and index directives should typically 
        # not be in locations blocks
        # Minimum really is the server block.
        # Usually even better in http block.
    }
    location /assets/ {
        # What you need here is the alias directive
        alias /Users/hugo/src/feedback/www/assets/;
    }
}

在尝试了更多之后,我尝试了这个并且成功了。

server {
    listen       9001;
    server_name  localhost;
    root /Users/hugo/src/feedback/www/html;
    index  index.html index.htm;

    location /assets/ {
        root /Users/hugo/src/feedback/www;
    }
}

我非常喜欢 Dayo 的回答,因为在阅读文档后我觉得别名更适合我正在做的事情,但不幸的是它没有用。