目前我对 NGINX 有疑问。我喜欢为 HUGO 生成的静态 html 网站提供服务。该网站是多语言(英语、德语)并存储在不同的文件夹中。
└─ /usr/share/nginx/html/my-app/
├─ en/
└─ fr/
所以我设置了一个 NGINX 配置来处理不同位置的 en 和 de 内容请求。
http {
...
server {
listen 8080;
server_name localhost;
index index.html;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html/my-app/en
index index.html;
try_files $uri $uri/index.html =404;
}
location /de/ {
root /usr/share/nginx/html/my-app/de
index index.html;
try_files $uri $uri/index.html =404;
}
location /en-us/ {
root /usr/share/nginx/html/my-app/en
index index.html;
try_files $uri $uri/index.html =404;
}
...
}
此时只有
http://localhost:8080
由 NGINX 提供服务。所以提供了 EN 内容。
但是如果我请求
http://localhost:8080/de
或http://localhost:8080/de/
总是有404错误。我不明白为什么找不到 DE 内容。有 1:1 位置 - 文件夹匹配?!
美德路径
http://localhost:8080/any-lang
或http://localhost:8080/any-lang/
也不起作用。我的想法是,如果请求路径any-lang
,NGINX 将使用 en 文件夹来提供内容。
我会很高兴每一个建议或想法!
让我从一个例子开始
server {
location /images/ {
root /data;
}
}
当你向 /images/ 地址发送请求时,nginx 会在这个路径中寻找文件来为它提供服务。
/data/images/
你有2个选择
1。 Richard在评论中给出的解决方案(但请记住,文件夹名称必须与地址相同 ->
/en-us
文件夹必须是/usr/share/nginx/html/my-app/en-us
2。使用
alias
而不是 root
location / {
root /usr/share/nginx/html/my-app/en;
index index.html;
try_files $uri $uri/index.html =404;
}
location /de {
alias /usr/share/nginx/html/my-app/de;
index index.html;
try_files $uri $uri/index.html =404;
}
location /en-us {
alias /usr/share/nginx/html/my-app/en;
index index.html;
try_files $uri $uri/index.html =404;
}